C# 如何使用 .NET 压缩目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2236025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I Compress a directory with .NET?
提问by masoud ramezani
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#?
我有一个包含多个文件的目录。我想将此文件夹压缩为 zip 或 tar.gz 文件。我怎样才能在 C# 中完成他的工作?
采纳答案by Giorgi
You can use DotNetZip Library. It has quite rich and useful features.
您可以使用DotNetZip 库。它具有相当丰富和有用的功能。
EDIT:
编辑:
string[] MainDirs = Directory.GetDirectories(DirString);
for (int i = 0; i < MainDirs.Length; i++)
{
using (ZipFile zip = new ZipFile())
{
zip.UseUnicodeAsNecessary = true;
zip.AddDirectory(MainDirs[i]);
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
zip.Save(string.Format("test{0}.zip", i));
}
}
回答by Natrium
回答by Daniel May
Look into using SharpZipLib. It supports both GZip and ZIP compression in C#.
研究使用SharpZipLib。它支持 C# 中的 GZip 和 ZIP 压缩。
There is an excellent tutorial hereoutlining what you need to do to zip a directory with SharpZipLib.
这里有一个很棒的教程,概述了使用 SharpZipLib 压缩目录所需的操作。
回答by Chathuranga Chandrasekara
This is a good discussionthat discusses the possibility of doing this without any third party libraries. I think you should have a look on it.
这是一个很好的讨论,讨论了在没有任何第三方库的情况下执行此操作的可能性。我想你应该看看它。
Here is a large repositoryof sample codes that can help you in your work. Good Luck..
回答by Christian Casutt
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..
我使用 .NET Framework 3.5 引入的 System.IO.Packaging 命名空间。我决定使用那个,因为它基于 .NET Framework 基类并且不需要第 3 方代码,这会增加代码的大小。
here'sanother post on Stackoverflow regarding this Question
这是关于这个问题的关于 Stackoverflow的另一篇文章
And here'sthe Namespace and ZipPackage declaration / explanation @MSDN
而这里的命名空间和ZipPackage声明/说明@MSDN
hope that helps
希望有帮助
Christian
基督教
回答by ata
GZip is part of Microsoft Framework 2.0 onward. Its called GZipStreamunder System.IO.Compression namespace.
GZip 是 Microsoft Framework 2.0 以后的一部分。它在 System.IO.Compression 命名空间下称为GZipStream。
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.
要使用此类压缩目录,您必须创建一个包含文件集合的可序列化类(例如 Directory)。
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.
Files 类将包含文件名和文件流以从文件中读取字节。一旦你在目录上应用了 GZip,它就会一个一个地读取文件并将它们写入 GZipStream。
Check this link: http://www.vwd-cms.com/forum/forums.aspx?topic=18
回答by Peter Stuer
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 ;-).
另一个 3.5 之前的选项是使用 J# 中的 zip 实用程序。毕竟,.Net 并不关心代码最初是用什么语言编写的 ;-)。
Articles on how to do this:
有关如何执行此操作的文章:
回答by Philm
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
and System.IO.Compression.ZipArchive
namespace classes.
自 2012 年底以来的最佳答案是:使用 .NET 4.5 和包含的System.IO.Compression
和 System.IO.Compression.ZipArchive
命名空间类。
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
如果您在互联网上搜索,您会收到许多示例链接之一: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.
自 2014/2015 年以来:使用 Roslyn,整个框架库作为开源发布,所以 AFAI 理解它,您可以自由地从 4.5 类中提取代码(因为它不应该是真正特定于系统的)并将其用作用于早期 .NET 框架的库。也许这会给使用其他类带来一些许可优势 - 但这必须由您进行分析。
回答by amr ras
You can zip the directory in pure .NET 3.0.
您可以在纯 .NET 3.0 中压缩该目录。
First, you will need a reference to WindowsBase.dll.
首先,您需要对 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.
此代码将打开或创建一个 zip 文件,在其中创建一个目录,并将文件放在该目录中。如果您想压缩一个文件夹,可能包含子目录,您可以遍历目录中的文件并为每个文件调用此方法。然后,您可以深度优先搜索文件的子目录,为每个文件调用方法并传入路径以在 zip 文件中创建该层次结构。
public void AddFileToZip(string zipFilename, string fileToAdd, string destDir)
{
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = "." + destDir + "\" + 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);
}
}
}
}
destDir could be an empty string, which would place the file directly in the 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
https://weblogs.asp.net/albertpascual/creating-a-folder-inside-the-zip-file-with-system-io-packaging