问题
I'm trying to unzip a file from a winform application. I'm using this code :
string dezarhiverPath = @AppDomain.CurrentDomain.BaseDirectory + "\\7z.exe";
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = dezarhiverPath;
pro.Arguments = @" e c:\TEST.ZIP";
Process x = Process.Start(pro);
x.WaitForExit();
The code doesn't return error but doesn't anything. I tried this command also from cmd :
K:\>"C:\Test\7z.exe" e "c:\TEST.ZIP"
but in cmd ,I receive this error message :
7-Zip cannot find the code that works with archives.
Can somebody help me to unzip some files from c# ?
Thanks!
回答1:
Why would you bother trying to use the 7z.exe application externally? That is a very kludgy way of doing it. Instead use one of the many libraries at your disposal.
If this is a new application, and you are targeting .NET 4.5, The new System.IO.Compression namespace has a ZipFile class.
Alternatively, SharpZipLib is a GPL library for file compression in .NET. There are online samples.
Also available is DotNetZip which is Ms-PL licensed.
回答2:
Refer Following Code:
using System.IO.Compression;
string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
string extractPath = @"c:\example\extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
Referance Link:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/849c4969-24b1-4650-88a5-5169727e527f/
回答3:
You can use SevenZipSharp library
using (var input = File.OpenRead(lstFiles[0]))
{
using (var ds = new SevenZipExtractor(input))
{
//ds.ExtractionFinished += DsOnExtractionFinished;
var mem = new MemoryStream();
ds.ExtractFile(0, mem);
using (var sr = new StreamReader(mem))
{
var iCount = 0;
String line;
mem.Position = 0;
while ((line = sr.ReadLine()) != null && iCount < 100)
{
iCount++;
LstOutput.Items.Add(line);
}
}
}
}
回答4:
Try this
string fileZip = @"c:\example\result.zip";
string fileZipPathExtactx= @"c:\example\";
ProcessStartInfo p = new ProcessStartInfo();
p.WindowStyle = ProcessWindowStyle.Hidden;
p.FileName = dezarhiverPath ;
p.Arguments = "x \"" + fileZip + "\" -o" + fileZipPathExtact;
Process x = Process.Start(p);
x.WaitForExit();
回答5:
Hey use this code below , you must have 7zip application in your system .
public void ExtractFile(string source, string destination)
{
string zPath = @"C:\Program Files\7-Zip\7zG.exe";// change the path and give yours
try
{
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = zPath;
pro.Arguments = "x \"" + source + "\" -o" + destination;
Process x = Process.Start(pro);
x.WaitForExit();
}
catch (System.Exception Ex) {
//DO logic here
}
}
to create :
public void CreateZip()
{
string sourceName = @"d:\a\example.txt";
string targetName = @"d:\a\123.zip";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = @"C:\Program Files\7-Zip\7zG.exe";
p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
回答6:
This maybe can help you.
//You must create an empty folder to remove.
string tempDirectoryPath = @"C:\Users\HOPE\Desktop\Test Folder\zipfolder";
string zipFilePath = @"C:\Users\HOPE\Desktop\7za920.zip";
Directory.CreateDirectory(tempDirectoryPath);
ZipFile.ExtractToDirectory(zipFilePath, tempDirectoryPath);
来源:https://stackoverflow.com/questions/16558363/unzip-a-file-in-c-sharp-using-7z-exe