C# 压缩/解压缩文件夹和文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/145/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 10:28:58  来源:igfitidea点击:

Compressing / Decompressing Folders & Files

提问by DylanJ

Does anyone know of a good way to compress or decompress files and folders in C# quickly? Handling large files might be necessary.

有谁知道在 C# 中快速压缩或解压缩文件和文件夹的好方法?可能需要处理大文件。

回答by alumb

As of .Net 1.1 the only available method is reaching into the java libraries.

从 .Net 1.1 开始,唯一可用的方法是访问 java 库。

Using the Zip Classes in the J# Class Libraries to Compress Files and Data with C#

使用 J# 类库中的 Zip 类通过 C# 压缩文件和数据

Not sure if this has changed in recent versions.

不确定这在最近的版本中是否有所改变。

回答by Tom Grochowicz

I've always used the SharpZip Library.

我一直在使用 SharpZip 库。

Here's a link

这是一个链接

回答by Tom Grochowicz

You can use a 3rd-party library such as SharpZipas Tom pointed out.

正如汤姆指出的那样,您可以使用第 3 方库,例如 SharpZip

Another way (without going 3rd-party) is to use the Windows Shell API. You'll need to set a reference to the Microsoft Shell Controls and Automation COM library in your C# project. Gerald Gibson has an example at:

另一种方法(不参加第 3 方)是使用 Windows Shell API。您需要在 C# 项目中设置对 Microsoft Shell 控件和自动化 COM 库的引用。Gerald Gibson 有一个例子:

Internet Archive's copy of the dead page

互联网档案馆的死页副本

回答by shsteimer

This is very easy to do in java, and as stated above you can reach into the java.util.zip libraries from C#. For references see:

这在 Java 中很容易实现,如上所述,您可以从 C# 访问 java.util.zip 库。参考资料见:

java.util.zip javadocs
sample code

java.util.zip javadocs
示例代码

I used this a while ago to do a deep (recursive) zip of a folder structure, but I don't think I ever used the unzipping. If I'm so motivated I may pull that code out and edit it into here later.

不久前我使用它对文件夹结构进行了深度(递归)压缩,但我认为我从未使用过解压缩。如果我有动力,我可能会拉出该代码并稍后将其编辑到此处。

回答by Dave Anderson

The .Net 2.0 framework namespace System.IO.Compressionsupports GZip and Deflate algorithms. Here are two methods that compress and decompress a byte stream which you can get from your file object. You can substitute GZipStreamfor DefaultStreamin the methods below to use that algorithm. This still leaves the problem of handling files compressed with different algorithms though.

.Net 2.0 框架命名空间System.IO.Compression支持 GZip 和 Deflate 算法。这里有两种方法可以压缩和解压缩可以从文件对象中获取的字节流。您可以替换GZipStreamDefaultStream在下面的方法使用该算法。尽管如此,这仍然存在处理使用不同算法压缩的文件的问题。

public static byte[] Compress(byte[] data)
{
    MemoryStream output = new MemoryStream();

    GZipStream gzip = new GZipStream(output, CompressionMode.Compress, true);
    gzip.Write(data, 0, data.Length);
    gzip.Close();

    return output.ToArray();
}

public static byte[] Decompress(byte[] data)
{
    MemoryStream input = new MemoryStream();
    input.Write(data, 0, data.Length);
    input.Position = 0;

    GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);

    MemoryStream output = new MemoryStream();

    byte[] buff = new byte[64];
    int read = -1;

    read = gzip.Read(buff, 0, buff.Length);

    while (read > 0)
    {
        output.Write(buff, 0, read);
        read = gzip.Read(buff, 0, buff.Length);
    }

    gzip.Close();

    return output.ToArray();
}

回答by Rok

Another good alternative is also DotNetZip.

另一个不错的选择也是DotNetZip

回答by Darryl

GZipStreamis a really good utility to use.

GZipStream是一个非常好的实用工具。

回答by Amit Joki

My answer would be close your eyes and opt for DotNetZip. It's been tested by a large community.

我的答案是闭上眼睛并选择DotNetZip。它已经过大型社区的测试。

回答by mohsen_1687

You can create zip file with this method:

您可以使用此方法创建 zip 文件:

public async Task<string> CreateZipFile(string sourceDirectoryPath, string name)
{
    var path = HostingEnvironment.MapPath(TempPath) + name;
    await Task.Run(() =>
    {
        if (File.Exists(path)) File.Delete(path);
        ZipFile.CreateFromDirectory(sourceDirectoryPath, path);
    });
    return path;
}

and then you can unzip zip file with this methods:

然后您可以使用以下方法解压缩 zip 文件:

1- This method work with zip file path

1- 此方法适用于 zip 文件路径

public async Task ExtractZipFile(string filePath, string destinationDirectoryName)
{
    await Task.Run(() =>
    {
        var archive = ZipFile.Open(filePath, ZipArchiveMode.Read);
        foreach (var entry in archive.Entries)
        {
            entry.ExtractToFile(Path.Combine(destinationDirectoryName, entry.FullName), true);
        }
        archive.Dispose();
    });
}

2- This method work with zip file stream

2- 此方法适用于 zip 文件流

public async Task ExtractZipFile(Stream zipFile, string destinationDirectoryName)
{
    string filePath = HostingEnvironment.MapPath(TempPath) + Utility.GetRandomNumber(1, int.MaxValue);
    using (FileStream output = new FileStream(filePath, FileMode.Create))
    {
        await zipFile.CopyToAsync(output);
    }
    await Task.Run(() => ZipFile.ExtractToDirectory(filePath, destinationDirectoryName));
    await Task.Run(() => File.Delete(filePath));
}