[英]File compression in .net framework 4.0 c#
Are there any built-in classes/examples for version 4.0 to compress specific files from a directory? 是否有任何用于版本4.0的内置类/示例来压缩目录中的特定文件? I found an example on MSDN which uses the compression class but it is only for version 4.5 & above.
我在MSDN上找到了一个使用压缩类的示例,但它仅适用于4.5及更高版本。
You can use GZipStream
and DeflateStream
classes which includes also .NET Framework 4. 您可以使用包含.NET Framework 4的
GZipStream
和DeflateStream
类。
Check How to: Compress Files
from MSDN. 检查
How to: Compress Files
MSDN中的How to: Compress Files
。
Use the System.IO.Compression.GZipStream class to compress and decompress data.
使用System.IO.Compression.GZipStream类来压缩和解压缩数据。 You can also use the System.IO.Compression.DeflateStream class, which uses the same compression algorithm;
您还可以使用System.IO.Compression.DeflateStream类,该类使用相同的压缩算法; however, compressed GZipStream objects written to a file that has an extension of .gz can be decompressed using many common compression tools.
但是,可以使用许多常用的压缩工具解压缩写入扩展名为.gz的文件的压缩GZipStream对象。
An example from here : 这里的一个例子:
FileStream sourceFileStream = File.OpenRead("sitemap.xml");
FileStream destFileStream = File.Create("sitemap.xml.gz");
GZipStream compressingStream = new GZipStream(destFileStream,
CompressionMode.Compress);
byte[] bytes = new byte[2048];
int bytesRead;
while ((bytesRead = sourceFileStream.Read(bytes, 0, bytes.Length)) != 0)
{
compressingStream.Write(bytes, 0, bytesRead);
}
sourceFileStream.Close();
compressingStream.Close();
destFileStream.Close();
FileStream sourceFileStream = File.OpenRead("sitemap.xml.gz");
FileStream destFileStream = File.Create("sitemap.xml");
GZipStream decompressingStream = new GZipStream(sourceFileStream,
CompressionMode.Decompress);
int byteRead;
while((byteRead = decompressingStream.ReadByte()) != -1)
{
destFileStream.WriteByte((byte)byteRead);
}
decompressingStream.Close();
sourceFileStream.Close();
destFileStream.Close();
I've done a lot of file compression work over the years and by far the best option I found is to use DotNetZip 多年来我做了很多文件压缩工作,到目前为止我找到的最好的选择是使用DotNetZip
http://dotnetzip.codeplex.com http://dotnetzip.codeplex.com
Better than GZipStream
and the other BCL offerings. 比
GZipStream
和其他BCL产品更好。 It has a friendly API and provides significant functionality. 它具有友好的API并提供重要的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.