如何使用C#4.0从文件夹中解压缩所有.Zip文件,而不使用任何OpenSource Dll

本文关键字:文件 Dll OpenSource 任何 Zip C#4 何使用 文件夹 解压缩 | 更新日期: 2023-09-27 18:09:30

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

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

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

如何使用C#4.0从文件夹中解压缩所有.Zip文件,而不使用任何OpenSource Dll

以下是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 framework 4.5及以上版本。

我也有同样的问题,找到了一篇非常简单的文章来解决这个问题。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((.

中找到

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

虽然与上面提到的.NET 4.5方法的简单性相去甚远,但它看起来可以随心所欲。

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

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

当调用extract Function时,您可以通过保存返回的folderItems

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

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

我也遇到了同样的问题,并通过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"文件中尝试过。希望这能有所帮助。