0

我正在使用 DotNetZIP(Ionic 实用程序)压缩我的文件。但我的客户对它说“不”。他们希望我使用 WinZip 压缩文件。我正在使用 MS 提供的 GZIP(对客户端来说可以),如下所示:

using(GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))

如何保存压缩流?VS2008/.Net3.5似乎没有可用的方法。

在网上搜索但没有任何合适的链接或解决方案。有人可以帮忙吗?

4

1 回答 1

0

You can follow this link http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream(v=vs.90).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());
                        }
                    }
                }
            }
        }
于 2013-05-24T09:13:02.430 回答