保存gzip压缩流到目标c#.Net 3.5
本文关键字:Net 目标 gzip 压缩 保存 | 更新日期: 2023-09-27 18:14:57
我使用DotNetZIP(Ionic实用程序)来压缩我的文件。但我的委托人拒绝了。他们想让我用WinZip压缩文件。我使用MS提供的GZIP(它的客户端OK)像这样:
using(GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
如何保存压缩流?似乎没有方法可用于VS2008/. net 3.5。
在网上搜索,但没有找到任何合适的链接或解决方案。有人能帮帮忙吗?
您可以点击此链接http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream (v =应用程序). aspx
public static void Compress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and already compressed files.
if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Extension != ".gz")
{
// Create the compressed file.
using (FileStream outFile = File.Create(fi.FullName + ".gz"))
{
using (GZipStream Compress = new GZipStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into the compression stream.
byte[] buffer = new byte[4096];
int numRead;
while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
{
Compress.Write(buffer, 0, numRead);
}
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fi.Name, fi.Length.ToString(), outFile.Length.ToString());
}
}
}
}
}