如何将zip文件内容提取到.NET 4.5中的文件夹中 [英] How to extract zip file contents into a folder in .NET 4.5

查看:114
本文介绍了如何将zip文件内容提取到.NET 4.5中的文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题的答案似乎概述了如何使用System.IO.Commpression.ZipFile.ExtractToDirectory方法调用来提取文件。添加对System.IO.Compression的引用时,.NET 4.5中似乎不存在 ZipFile。如何从.NET 4.5中的* .zip文件中提取文件?





编辑:



请注意7z.exe(来自7zip程序包)如何不起作用。 .NET和7zip必须存在冲突。 ZipFile现在似乎可以正常工作。

  private void extract_Click(对象发送者,EventArgs e)
{
字符串exePath = System.Reflection.Assembly.GetExecutingAssembly()。Location;
exePath = @ C:\test; //故障排除期间的路径

//// var cmd1 = cd \ + exePath + \;
//// ExecuteCommand(cmd1,100,exePath);

// var cmd2 = \ + exePath + \\7z.exe\ x \ + exePath + \\source.zip\ ;
// ExecuteCommand(cmd2,100,exePath);

字符串zipPath = exePath + \\source.zip;
字符串extractPath = exePath;

//需要显式引用System.IO.Compression.FileSystem
ZipFile.ExtractToDirectory(zipPath,extractPath);


}

private static int ExecuteCommand(字符串命令,int超时,字符串dir)
{
var processInfo = new ProcessStartInfo( cmd.exe, / C +命令)
{
CreateNoWindow = true,
UseShellExecute = false,
WorkingDirectory = dir,
};

var process = Process.Start(processInfo);
process.WaitForExit(timeout);
var exitCode = process.ExitCode;
process.Close();
返回exitCode;
}


解决方案

您需要添加一个引用 System.IO.Compression.FileSystem 程序集。



每个库类都有一个MSDN页面。这是 ZipFile



在顶部注意命名空间和程序集的规范。


The following question's answer seems to outline how to extract files using the System.IO.Commpression.ZipFile.ExtractToDirectory method invocation. "ZipFile" doesn't seem to exist in .NET 4.5, when adding a reference to System.IO.Compression. How can I extract files from a *.zip file in .NET 4.5?

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

This seems to show how to compress files. But I'm looking for the reverse.

Zipping files in .NET 4.5

Even this question references "ZipFile" in the source code. But I can't seem to find this class.

How to extract just the specific directory from a zip archive in C# .NET 4.5?

EDIT:

Notice how 7z.exe (from 7zip package) didn't work. There must be a conflict with .NET and 7zip. ZipFile now seems to work fine.

private void extract_Click(object sender, EventArgs e)
{
    string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
    exePath = @"C:\test";  // path during troubleshooting

    ////var cmd1 = "cd \"" + exePath + "\"";
    ////ExecuteCommand(cmd1, 100, exePath);

    //var cmd2 = "\"" + exePath + "\\7z.exe\" x \"" + exePath + "\\source.zip\"";
    //ExecuteCommand(cmd2, 100, exePath);

    string zipPath = exePath + "\\source.zip";
    string extractPath = exePath;

    // needed explicit reference to System.IO.Compression.FileSystem
    ZipFile.ExtractToDirectory(zipPath, extractPath);


}

private static int ExecuteCommand(string command, int timeout, string dir)
{
    var processInfo = new ProcessStartInfo("cmd.exe", " /C " + command)
    {
        CreateNoWindow = true,
        UseShellExecute = false,
        WorkingDirectory = dir,
    };

    var process = Process.Start(processInfo);
    process.WaitForExit(timeout);
    var exitCode = process.ExitCode;
    process.Close();
    return exitCode;
}

解决方案

You will need to add a reference to the System.IO.Compression.FileSystem assembly.

Every library class has an MSDN page. This is the one for ZipFile.

Notice the specification of the namespace and the assembly in the top section.

这篇关于如何将zip文件内容提取到.NET 4.5中的文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆