24

我有一个包含.ZIP 文件的文件夹。现在,我想使用 C# 将 ZIP 文件提取到特定文件夹,但不使用任何外部程序集或 .Net Framework 4.5。

我已经搜索过,但没有找到使用 Framework 4.0 或更低版本解压缩 *.zip 文件的任何解决方案。

我试过 GZipStream,但它只支持 .gz 而不是 .zip 文件。

4

6 回答 6

34

这是来自msdn的示例。System.IO.Compression.ZipFile就是为此而生的:

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

编辑:抱歉,我错过了您对 .NET 4.0 及以下版本的兴趣。需要 .NET 框架 4.5 及更高版本。

于 2013-04-17T06:19:03.870 回答
16

我有同样的问题,并找到了一篇很棒且非常简单的文章来解决这个问题。 http://www.fluxbytes.com/csharp/unzipping-files-using-shell32-in-c/

您需要参考名为 Microsoft Shell Controls And Automation (Interop.Shell32.dll) 的 COM 库

代码(未从文章中获取,只是为了让您了解它是多么简单):

public static void UnZip(string zipFile, string folderPath)
{
    if (!File.Exists(zipFile))
        throw new FileNotFoundException();

    if (!Directory.Exists(folderPath))
        Directory.CreateDirectory(folderPath);

    Shell32.Shell objShell = new Shell32.Shell();
    Shell32.Folder destinationFolder = objShell.NameSpace(folderPath);
    Shell32.Folder sourceFile = objShell.NameSpace(zipFile);

    foreach (var file in sourceFile.Items())
    {
        destinationFolder.CopyHere(file, 4 | 16);
    }
}

强烈推荐阅读这篇文章——他对标志 4|16 进行了解释

编辑:在我使用它的应用程序运行几年后,我收到了两个用户的投诉,称该应用程序突然停止工作。CopyHere 函数似乎创建了临时文件/文件夹,并且这些文件/文件夹从未被删除,这导致了问题。这些文件的位置可以在 System.IO.Path.GetTempPath() 中找到。

于 2014-11-03T14:07:25.233 回答
0

在 .net 4.0 Deflate 和 GZip 无法处理 Zip 文件,但您可以使用 shell 功能解压缩文件。

public FolderItems Extract()
{
 var shell = new Shell();
 var sf = shell.NameSpace(_zipFile.Path);
 return sf.Items();
}

调用提取函数时,您可以通过以下方式保存返回的文件夹项

    FolderItems Fits = Extract();
    foreach( var s in Fits)
    {
       shell.Namespace("TargetFolder").copyhere(s); 

    }
于 2013-09-12T12:45:07.123 回答
0

我有同样的问题,并通过 C# 代码中的 cmd shell 调用 7-zip 可执行文件来解决它,如下所示,

string zipped_path = "xxx.7z";
string unzipped_path = "yyy";
string arguments = "e " + zipped_path + " -o" + unzipped_path;

System.Diagnostics.Process process
         = Launch_in_Shell("C:\\Program Files (x86)\\7-Zip\\","7z.exe", arguments);

if (!(process.ExitCode == 0))
     throw new Exception("Unable to decompress file: " + zipped_path);

其中Launch_in_Shell(...)定义为,

public static System.Diagnostics.Process Launch_in_Shell(string WorkingDirectory,
                                                         string FileName, 
                                                         string Arguments)
{
       System.Diagnostics.ProcessStartInfo processInfo 
                                         = new System.Diagnostics.ProcessStartInfo();

        processInfo.WorkingDirectory = WorkingDirectory;
        processInfo.FileName = FileName;
        processInfo.Arguments = Arguments;
        processInfo.UseShellExecute = true;
        System.Diagnostics.Process process 
                                      = System.Diagnostics.Process.Start(processInfo);

        return process;
}

缺点:您需要在您的机器上安装 7zip,而我只尝试使用“.7z”文件。希望这可以帮助。

于 2014-11-06T17:46:51.327 回答
0

.NET 3.5 为此提供了 DeflateStream。您必须为目录等信息创建结构,但 PKWare 已发布此信息。我编写了一个解压缩实用程序,一旦为它创建结构,它就不会特别繁重。

于 2014-06-06T18:13:09.560 回答
0

ZipPackage可能是一个开始的地方。它在System.IO.Packaging中,在 .NET 4.0 中可用

远不及上面提到的 .NET 4.5 方法的简单性,但它看起来可以做你想做的事。

于 2013-04-17T06:21:34.613 回答