解决方法
您不必再使用外部库了。 System.IO.Packaging具有可用于将内容放入zip文件的类。不过不是很简单。
Here’s a blog post with an example(它的最后;挖它)。
链接不稳定,所以这里是Jon在帖子中提供的例子。
using System; using System.IO; using System.IO.Packaging; namespace ZipSample { class Program { static void Main(string[] args) { AddFileToZip("Output.zip",@"C:\Windows\Notepad.exe"); AddFileToZip("Output.zip",@"C:\Windows\System32\Calc.exe"); } private const long BUFFER_SIZE = 4096; private static void AddFileToZip(string zipFilename,string fileToAdd) { using (Package zip = System.IO.Packaging.Package.Open(zipFilename,FileMode.OpenOrCreate)) { string destFilename = ".\\" + Path.GetFileName(fileToAdd); Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename,UriKind.Relative)); if (zip.PartExists(uri)) { zip.DeletePart(uri); } PackagePart part = zip.CreatePart(uri,"",CompressionOption.Normal); using (FileStream fileStream = new FileStream(fileToAdd,FileMode.Open,FileAccess.Read)) { using (Stream dest = part.GetStream()) { CopyStream(fileStream,dest); } } } } private static void CopyStream(System.IO.FileStream inputStream,System.IO.Stream outputStream) { long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE; byte[] buffer = new byte[bufferSize]; int bytesRead = 0; long bytesWritten = 0; while ((bytesRead = inputStream.Read(buffer,buffer.Length)) != 0) { outputStream.Write(buffer,bytesRead); bytesWritten += bytesRead; } } } }