问题
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