如何使用 .NET 压缩目录?

发布于 2024-08-20 21:35:48 字数 65 浏览 3 评论 0原文

我有一个包含多个文件的目录。我想将此文件夹压缩为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

转瞬即逝 2024-08-27 21:35:48

您可以使用DotNetZip 库。它有相当丰富和有用的功能。


编辑:

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));   
    }
}

You can use DotNetZip Library. It has quite rich and useful features.


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));   
    }
}
深海夜未眠 2024-08-27 21:35:48

考虑使用 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.

楠木可依 2024-08-27 21:35:48

在 C# 命令行中使用 7zip --> LZMA SDK支持C#,包内有代码示例

use 7zip from commandline in C# --> LZMA SDK supports C#, and there are codesamples in the package

暮凉 2024-08-27 21:35:48

我使用 .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

风吹短裙飘 2024-08-27 21:35:48

这个问题很老了,答案也很老了。

自 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 and System.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.

香橙ぽ 2024-08-27 21:35:48

在我之前的工作中,我们使用了 #ziplib

At my previous job we used #ziplib.

七七 2024-08-27 21:35:48

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..

戏蝶舞 2024-08-27 21:35:48

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

墨小墨 2024-08-27 21:35:48

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:

心舞飞扬 2024-08-27 21:35:48

您可以在纯 .NET 3.0 中压缩该目录。

首先,您需要对 WindowsBase.dll 的引用。

此代码将打开或创建一个 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 可以是一个空字符串,它将文件直接放在 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.

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.

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

盛装女皇 2024-08-27 21:35:48

我发现使用 .Net 4.0 中提供的 System.IO.Compression 的最简单的解决方案:

System.IO.Compression.ZipFile.CreateFromDirectory(directoryToArchivePath, archiveDestinationPath);

The most simple solution that I found using System.IO.Compression available from .Net 4.0:

System.IO.Compression.ZipFile.CreateFromDirectory(directoryToArchivePath, archiveDestinationPath);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文