解决方案:
从.Net 1.1开始,唯一可用的方法是进入Java库。
在JClass库中使用Zip类使用C压缩文件和数据
不确定在最新版本中是否已更改。
我一直使用SharpZip库。
另一种方法(无需第三方)是使用Windows Shell API。我们需要在Cproject中设置对Microsoft Shell控件和自动化COM库的引用。
另外, .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();
}
在C#中快速压缩或者解压缩文件和文件夹有什么好方法吗?
日期:2020-03-23 14:39:35 来源:oir作者:oir
