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;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…