C#中的Zip文件夹

什么是如何在C#中压缩文件夹的示例(简单代码)?


更新:

我没有看到命名空间ICSharpCode 。 我下载了ICSharpCode.SharpZipLib.dll但我不知道在哪里复制该DLL文件。 我需要做什么才能看到这个命名空间?

你有链接的MSDN示例压缩文件夹,因为我读了所有的MSDN,但我什么都找不到。


好,但我需要下一个信息。

我应该在哪里复制ICSharpCode.SharpZipLib.dll在Visual Studio中查看该命名空间?

这个答案改变了.NET 4.5。 创build一个zip文件变得非常容易 。 不需要第三方库。

 string startPath = @"c:\example\start"; string zipPath = @"c:\example\result.zip"; string extractPath = @"c:\example\extract"; ZipFile.CreateFromDirectory(startPath, zipPath); ZipFile.ExtractToDirectory(zipPath, extractPath); 

从DotNetZip帮助文件http://dotnetzip.codeplex.com/releases/

 using (ZipFile zip = new ZipFile()) { zip.UseUnicodeAsNecessary= true; // utf-8 zip.AddDirectory(@"MyDocuments\ProjectX"); zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G") ; zip.Save(pathToSaveZipFile); } 

BCL中没有任何东西可以为你做这件事,但是.NET有两个伟大的库支持这个function。

  • SharpZipLib
  • DotNetZip

我已经使用了两个,可以说这两个是非常完整的,并有精心devise的API,所以它主要是个人喜好的问题。

我不确定他们是否明确支持添加文件夹,而不仅仅是单个文件来压缩文件,但是使用DirectoryInfoFileInfo类创build迭代遍历目录及其子目录的东西应该很容易。

System.IO.Packaging命名空间中有一个ZipPackage类,内置于.NET 3,3.5和4.0中。

http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx

这里是一个如何使用它的例子。 http://www.codeproject.com/KB/files/ZipUnZipTool.aspx?display=Print

在.NET 4.5中,ZipFile.CreateFromDirectory(startPath,zipPath); 方法不包括您希望压缩多个文件和子文件夹而不必将其放入文件夹中的情况。 当您希望解压缩将文件直接放在当前文件夹内时,这是有效的。

这段代码适用于我:

 public static class FileExtensions { public static IEnumerable<FileSystemInfo> AllFilesAndFolders(this DirectoryInfo dir) { foreach (var f in dir.GetFiles()) yield return f; foreach (var d in dir.GetDirectories()) { yield return d; foreach (var o in AllFilesAndFolders(d)) yield return o; } } } void Test() { DirectoryInfo from = new DirectoryInfo(@"C:\Test"); using (FileStream zipToOpen = new FileStream(@"Test.zip", FileMode.Create)) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create)) { foreach (FileInfo file in from.AllFilesAndFolders().Where(o => o is FileInfo).Cast<FileInfo>()) { var relPath = file.FullName.Substring(from.FullName.Length+1); ZipArchiveEntry readmeEntry = archive.CreateEntryFromFile(file.FullName, relPath); } } } } 

文件夹不需要在zip-archive中“创build”。 CreateEntryFromFile中的第二个参数“entryName”应该是一个相对path,当解压zip文件时,将会检测并创build相对path的目录。

MSDN上有一篇文章,它有一个纯粹用C#压缩和解压缩文件和文件夹的示例应用程序。 我已经使用了很长时间的一些成功的类。 该代码是在Microsoft许可许可下发布的,如果你需要知道这种事情的话。

编辑:感谢切索指出,我有点落后于时代。 我指出的MSDN例子实际上是使用DotNetZip,而且现在真的非常全面。 根据我以前版本的经验,我很乐意推荐它。

SharpZipLib也是一个相当成熟的库,受到人们的高度评价,可以在GPL许可下使用。 这真的取决于你的压缩需求,以及你如何查看每个许可条款。

丰富

"Where should I copy ICSharpCode.SharpZipLib.dll to see that namespace in Visual Studio?"

您需要在项目中添加该dll文件作为参考。 在解决scheme资源pipe理器 – >添加引用 – >浏览右键单击引用,然后select该DLL。

最后,你需要把它作为一个using语句添加到你想要使用的任何文件中。

以下代码使用Rebex中的第三方ZIP组件 :

 // add content of the local directory C:\Data\ // to the root directory in the ZIP archive // (ZIP archive C:\archive.zip doesn't have to exist) Rebex.IO.Compression.ZipArchive.Add(@"C:\archive.zip", @"C:\Data\*", ""); 

或者,如果您想添加更多的文件夹而无需多次打开和closures存档:

 using Rebex.IO.Compression; ... // open the ZIP archive from an existing file ZipArchive zip = new ZipArchive(@"C:\archive.zip", ArchiveOpenMode.OpenOrCreate); // add first folder zip.Add(@"c:\first\folder\*","\first\folder"); // add second folder zip.Add(@"c:\second\folder\*","\second\folder"); // close the archive zip.Close(ArchiveSaveAction.Auto); 

你可以在这里下载ZIP组件 。

使用免费的LGPL许可SharpZipLib是一个常见的select。

免责声明:我为Rebex工作

ComponentPro ZIP可以帮助你实现这个任务。 以下代码片段可以压缩文件夹中的文件和目录。 你也可以使用威尔卡面具。

 using ComponentPro.Compression; using ComponentPro.IO; ... // Create a new instance. Zip zip = new Zip(); // Create a new zip file. zip.Create("test.zip"); zip.Add(@"D:\Temp\Abc"); // Add entire D:\Temp\Abc folder to the archive. // Add all files and subdirectories from 'c:\test' to the archive. zip.AddFiles(@"c:\test"); // Add all files and subdirectories from 'c:\my folder' to the archive. zip.AddFiles(@"c:\my folder", ""); // Add all files and subdirectories from 'c:\my folder' to '22' folder within the archive. zip.AddFiles(@"c:\my folder2", "22"); // Add all .dat files from 'c:\my folder' to '22' folder within the archive. zip.AddFiles(@"c:\my folder2", "22", "*.dat"); // Or simply use this to add all .dat files from 'c:\my folder' to '22' folder within the archive. zip.AddFiles(@"c:\my folder2\*.dat", "22"); // Add *.dat and *.exe files from 'c:\my folder' to '22' folder within the archive. zip.AddFiles(@"c:\my folder2\*.dat;*.exe", "22"); TransferOptions opt = new TransferOptions(); // Donot add empty directories. opt.CreateEmptyDirectories = false; zip.AddFiles(@"c:\abc", "/", opt); // Close the zip file. zip.Close(); 

http://www.componentpro.com/doc/zip有更多的例子;