如何使用 .NET 压缩目录?
我有一个包含多个文件的目录。我想将此文件夹压缩为 zip 或 tar.gz 文件。我怎样才能用 C# 完成他的工作?
I have a directory that contains several files. I want compress this folder to a zip or tar.gz file. How can I do his work in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以使用DotNetZip 库。它有相当丰富和有用的功能。
编辑:
You can use DotNetZip Library. It has quite rich and useful features.
EDIT:
考虑使用 SharpZipLib。它支持 C# 中的 GZip 和 ZIP 压缩。
有一个很棒的教程 此处概述了使用 SharpZipLib 压缩目录所需执行的操作。
Look into using SharpZipLib. It supports both GZip and ZIP compression in C#.
There is an excellent tutorial here outlining what you need to do to zip a directory with SharpZipLib.
在 C# 命令行中使用 7zip --> LZMA SDK支持C#,包内有代码示例
use 7zip from commandline in C# --> LZMA SDK supports C#, and there are codesamples in the package
我使用 .NET Framework 3.5 中引入的 System.IO.Packaging 命名空间。我决定使用它,因为它基于 .NET Framework 基类,并且不需要第 3 方代码,这会增加代码的大小。
这是 Stackoverflow 上关于此问题的另一篇文章
以及这里是命名空间和ZipPackage声明/解释@MSDN
希望对
Christian有帮助
i use the System.IO.Packaging Namespace which was introduced with .NET Framework 3.5. I decided to use that one because it's based on .NET Framework Base classes and no 3rd party code is required which blows up the size of the code..
here's another post on Stackoverflow regarding this Question
And here's the Namespace and ZipPackage declaration / explanation @MSDN
hope that helps
Christian
这个问题很老了,答案也很老了。
自 2012 年底以来的最佳答案是:使用 .NET 4.5 和包含的 System.IO.Compression 和 System.IO.Compression.ZipArchive 命名空间类。
如果您在互联网上搜索,您会收到许多示例链接之一:
http://www.codeproject.com/Articles/381661 /Creating-Zip-Files-Easily-in-NET
自 2014/2015 ff.:
Roslyn 的整个框架库作为开源发布,因此 AFAI 理解它,您可以自由地从 4.5 类中提取代码(因为它不应该是真正特定于系统的)并将其用作早期 .NET 框架的库。也许这会比使用其他类提供一些许可证优势 - 但这必须由您进行分析。
The question is quite old and so are the answers.
Best answer since end of 2012 is: Use .NET 4.5 and the contained
System.IO.Compression
andSystem.IO.Compression.ZipArchive
namespace classes.One of many example links you receive if you search in the internet:
http://www.codeproject.com/Articles/381661/Creating-Zip-Files-Easily-in-NET
Since 2014/2015 ff.:
With Roslyn the whole framework library was published as Open Source, so AFAI understand it, you are free to extract the code from the 4.5 classes (as it should be not really system specific) and use it as a library for the earlier .NET frameworks. Maybe this would give some license advantages over using the other classes- but this has to be analyzed by you.
在我之前的工作中,我们使用了 #ziplib。
At my previous job we used #ziplib.
这是一个很好的讨论< /a> 讨论了在没有任何第三方库的情况下执行此操作的可能性。我想你应该看一下。
这是一个大型存储库的示例代码,可以帮助您完成工作。祝你好运..
This is a good discussion that discusses the possibility of doing this without any third party libraries. I think you should have a look on it.
Here is a large repository of sample codes that can help you in your work. Good Luck..
GZip 是 Microsoft Framework 2.0 及以上版本的一部分。
它在 System.IO 下称为 GZipStream。压缩命名空间。
要使用此类压缩目录,您必须创建一个包含文件集合的可序列化类(例如目录)。
Files 类将包含文件名和文件流以从文件中读取字节。
一旦你在目录上应用了 GZip,它就会一一读取文件并将它们写入 GZipStream。
检查此链接:http://www.vwd-cms.com/论坛/forums.aspx?topic=18
GZip is part of Microsoft Framework 2.0 onward.
Its called GZipStream under System.IO.Compression namespace.
To compress a directory with this class, you'd have to create a serializable class (for e.g. Directory) which contains a collection of Files.
The Files class would contain file-name and file-stream to read bytes from file.
Once you do apply GZip on the Directory, it'll read Files one by one and write them to GZipStream.
Check this link: http://www.vwd-cms.com/forum/forums.aspx?topic=18
3.5 之前的另一个选项是使用 J# 中的 zip 实用程序。毕竟,.Net 并不关心代码最初是用什么语言编写的;-)。
有关如何执行此操作的文章:
Another pre-3.5 option is to use the zip utilities from J#. After all, .Net doesn't care what language the code was originally written in ;-).
Articles on how to do this:
您可以在纯 .NET 3.0 中压缩该目录。
首先,您需要对 WindowsBase.dll 的引用。
此代码将打开或创建一个 zip 文件,在其中创建一个目录,并将文件放入该目录中。如果要压缩可能包含子目录的文件夹,您可以循环遍历目录中的文件并为每个文件调用此方法。然后,您可以深度优先搜索子目录中的文件,为每个子目录调用方法并传入路径以在 zip 文件中创建该层次结构。
destDir 可以是一个空字符串,它将文件直接放在 zip 中。
资料来源:
https:// weblogs.asp.net/jongalloway/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib
https://weblogs.asp.net/albertpascual/creating-a-folder-inside-the -zip-file-with-system-io-packaging
You can zip the directory in pure .NET 3.0.
First, you will need a reference to WindowsBase.dll.
This code will open or create a zip file, create a directory inside, and place the file in that directory. If you want to zip a folder, possibly containing sub-directories, you could loop through the files in the directory and call this method for each file. Then, you could depth-first search the sub-directories for files, call the method for each of those and pass in the path to create that hierarchy within the zip file.
destDir could be an empty string, which would place the file directly in the zip.
Sources:
https://weblogs.asp.net/jongalloway/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib
https://weblogs.asp.net/albertpascual/creating-a-folder-inside-the-zip-file-with-system-io-packaging
我发现使用 .Net 4.0 中提供的 System.IO.Compression 的最简单的解决方案:
The most simple solution that I found using System.IO.Compression available from .Net 4.0: