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

查看:35
本文介绍了如何将 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(object sender, EventArgs e){字符串 exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;exePath = @"C:	est";//故障排除期间的路径////var cmd1 = "cd "" + exePath + """;////执行命令(cmd1, 100, exePath);//var cmd2 = """ + exePath + "\7z.exe" x "" + exePath + "\source.zip"";//执行命令(cmd2, 100, exePath);字符串 zipPath = exePath + "\source.zip";字符串提取路径 = exePath;//需要显式引用 System.IO.Compression.FileSystemZipFile.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,工作目录=目录,};var process = Process.Start(processInfo);process.WaitForExit(超时);var exitCode = process.ExitCode;进程.关闭();返回退出代码;}

解决方案

您将需要添加对 System.IO.Compression.FileSystem 程序集的引用.

每个库类都有一个 MSDN 页面.这是 压缩文件.

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

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:	est";  // 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天全站免登陆