How to extract zip file contents into a folder in .NET 4.5

☆樱花仙子☆ 提交于 2019-11-28 12:03:57

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/34773371/how-to-extract-zip-file-contents-into-a-folder-in-net-4-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!