如何在不使用任何开源 Dll 的情况下使用 C# 4.0 从文件夹中解压缩所有 .Zip 文件?

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

How to Unzip all .Zip file from Folder using C# 4.0 and without using any OpenSource Dll?

c#unzipzipfile

提问by SHEKHAR SHETE

I have a folder containing .ZIP files. Now, I want to Extract the ZIP Files to specific folders using C#, but without using any external assembly or the .Net Framework 4.5.

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

I have searched, but not found any solution for unpacking *.zip files using Framework 4.0 or below.

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

I tried GZipStream, but it only supports .gz and not .zip files.

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

回答by Denys Denysenko

Here is example from msdn. System.IO.Compression.ZipFileis made just for that:

这是来自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);
        }
    }
}

Edit:sorry, I missed that you're interests in .NET 4.0 and below. Required .NET framework 4.5 and above.

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

回答by Alex

ZipPackagemight be a place to start. It's inside System.IO.Packagingand available in .NET 4.0

ZipPackage可能是一个起点。它位于System.IO.Packaging 中,可在 .NET 4.0 中使用

Not anywhere near the simplicity of the .NET 4.5 method mentioned above, but it looks like it can do what you want.

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

回答by ajaybhasy

In .net 4.0 Deflate and GZip cannot handle Zip files, but you can use shell function for Unzipping files.

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

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

When extract Function is called you can save the returned folderItems by

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

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

    }

回答by Michael Blake

.NET 3.5 has a DeflateStream for this. You must create structs for the information on the directories and such, but PKWare has published this information. I have written an unzip utility and it is not particularly onerous once you create the structs for it.

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

回答by TBD

I had the same question and found a great and very simple article that solves the problem. http://www.fluxbytes.com/csharp/unzipping-files-using-shell32-in-c/

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

you'll need to reference the COM library called Microsoft Shell Controls And Automation (Interop.Shell32.dll)

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

The code (taken untouched from the article just so you see how simple it is):

代码(从文章中未改动,只是为了让您看到它是多么简单):

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

highly recommend reading the article- he brings an explenation for the flags 4|16

强烈推荐阅读这篇文章 - 他为标志 4|16 带来了解释

EDIT:after several years that my app, which uses this, has been running, I got complaints from two users that all of the sudden the app stopped working. it appears that the CopyHere function creates temp files/folders and these were never deleted which caused problems. The location of these files can be found in System.IO.Path.GetTempPath().

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

回答by josuapop

I had the same problem and solved it by calling 7-zip executable through cmd shell from C# code, as follows,

我遇到了同样的问题,并通过从 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);

And where Launch_in_Shell(...)is defined as,

其中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;
}

Drawbacks: You need to have 7zip installed in your machine and I only tried it with ".7z" files. Hope this helps.

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