Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

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

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?

enter image description here

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...