有谁知道一种很好的方式来快速压缩或解压缩C#文件和文件夹吗?处理大文件可能是必要的。压缩/解压缩文件夹和文件
回答
从.net 1.1开始,唯一可用的方法是进入java库。
Using the Zip Classes in the J# Class Libraries to Compress Files and Data with C#
不确定这是否在最近的版本中发生了变化。
我一直使用SharpZip库。
可以使用3rd-party library such as SharpZip汤姆指出。
另一种方式(不用第三方)是使用Windows Shell API。您需要在C#项目中设置对Microsoft Shell Controls和Automation COM库的引用。杰拉尔德·吉布森有一个例子:
http://geraldgibson.net/dnn/Home/CZipFileCompression/tabid/148/Default.aspx
这是很容易在java做的,和上面说你可以深入到从C#的java.util.zip库。对于参考文献,参见:
java.util.zip javadocs
sample code
我用这个前一阵子做了文件夹结构的深(递归)拉链,但我不认为我用过的解压缩。如果我非常积极主动,我可以将这些代码提取出来,并在稍后将其编辑到这里。
.Net 2.0框架命名空间System.IO.Compression
支持GZip和Deflate算法。下面是两种压缩和解压缩字节流的方法,您可以从文件对象中获取这些字节流。您可以在下面的方法中使用GZipStream
代替DefaultStream
以使用该算法。这仍然会导致处理使用不同算法压缩的文件的问题。
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output, CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}
public static byte[] Decompress(byte[] data)
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);
MemoryStream output = new MemoryStream();
byte[] buff = new byte[64];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
return output.ToArray();
}
另一个不错的选择是DotNetZip。
GZipStream是一个非常好用的工具。
我的答案会闭上你的眼睛,并选择DotNetZip。它已经过大型社区的测试。
你可以用这个方法来创建压缩文件:
public async Task<string> CreateZipFile(string sourceDirectoryPath, string name)
{
var path = HostingEnvironment.MapPath(TempPath) + name;
await Task.Run(() =>
{
if (File.Exists(path)) File.Delete(path);
ZipFile.CreateFromDirectory(sourceDirectoryPath, path);
});
return path;
}
,然后你可以解压缩zip文件,这个方法:用zip文件路径
1 - 这种方法工作
public async Task ExtractZipFile(string filePath, string destinationDirectoryName)
{
await Task.Run(() =>
{
var archive = ZipFile.Open(filePath, ZipArchiveMode.Read);
foreach (var entry in archive.Entries)
{
entry.ExtractToFile(Path.Combine(destinationDirectoryName, entry.FullName), true);
}
archive.Dispose();
});
}
2 - 此方法使用zip文件流
public async Task ExtractZipFile(Stream zipFile, string destinationDirectoryName)
{
string filePath = HostingEnvironment.MapPath(TempPath) + Utility.GetRandomNumber(1, int.MaxValue);
using (FileStream output = new FileStream(filePath, FileMode.Create))
{
await zipFile.CopyToAsync(output);
}
await Task.Run(() => ZipFile.ExtractToDirectory(filePath, destinationDirectoryName));
await Task.Run(() => File.Delete(filePath));
}
- 1. 压缩和解压缩文件夹
- 2. Node.js - 压缩/解压缩文件夹
- 3. 压缩和解压缩文件和文件夹
- 4. Magick ++压缩和解压缩文件
- 5. png文件的压缩和解压缩
- 6. Android - 解压缩文件夹?
- 7. 如何在Silverlight中压缩和解压缩文件夹及其子文件夹?
- 8. 压缩文件未解压
- 9. 在C#中压缩和解压缩文件夹#
- 10. 压缩/压缩文件夹,同时排除特定文件夹
- 11. 在yii中压缩/解压缩文件
- 12. 压缩解压缩的exe文件
- 13. 解压缩(解压缩)由.net压缩(解压缩)的文件.net system.io.compression.gzipstream
- 14. 解压缩文件
- 15. 解压缩文件
- 16. tomcat和解压缩文件
- 17. Powershell,WMI和压缩文件/文件夹
- 18. 解压缩文件并读取解压缩文件
- 19. 解压缩文件夹并从解压缩文件中获取图像
- 20. 如何将未压缩文件夹视为压缩文件?
- 21. Windows压缩文件夹没有完全解压缩zip
- 22. 解压缩压缩文件夹,ASP.NET MVC 5
- 23. Java解压缩文件夹压缩存档FileNotFound异常
- 24. 如何使用rubyzip解压缩压缩文件夹
- 25. 如何在asp.net框架4.0中压缩/解压缩文件夹
- 26. 如何只压缩文件夹内的文件而不压缩子文件夹?
- 27. 无法将文件从压缩文件夹复制到非压缩文件夹
- 28. ApplScript压缩文件夹?
- 29. android压缩文件夹
- 30. 搜索压缩文件夹
注:我发现* int over多年前SharpZip代码中的流*错误,导致它在随机文件上失败,这些文件恰好具有正确的值组合。不知道这个问题是否得到解决,但是从内存中,我在SharpZip源代码中将一个`int`变量改为`long`,以避免溢出。 *我将不得不寻找我的旧固定SharpZip代码,并检查它是否符合最新的*。 – 2014-03-17 16:41:40