我有一个包含.ZIP文件的文件夹.现在,我想使用C#将ZIP文件解压缩到特定文件夹,但不使用任何外部程序集或.Net Framework 4.5.
我搜索过,但没有找到任何使用Framework 4.0或更低版本解压缩*.zip文件的解决方案.
我尝试过GZipStream,但它只支持.gz而不支持.zip文件.
Den*_*nko 32
这是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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:对不起,我错过了你对.NET 4.0及以下版本的兴趣.必需的.NET framework 4.5及更高版本.
TBD*_*TBD 14
我有同样的问题,发现了一篇非常简单的文章解决了这个问题. 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);
}
}
Run Code Online (Sandbox Code Playgroud)
强烈建议阅读这篇文章 - 他为旗帜4 | 16带来了一个表达
编辑:几年后,我的应用程序,使用它,已经运行,我收到两个用户的投诉,突然之间应用程序停止工作.似乎CopyHere函数创建了临时文件/文件夹,但这些文件/文件夹从未被删除而导致出现问题.可以在System.IO.Path.GetTempPath()中找到这些文件的位置.
| 归档时间: |
|
| 查看次数: |
70738 次 |
| 最近记录: |