1

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!

user1577242
  • 413
  • 2
  • 13
  • 29

6 Answers6

6

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.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Then I would consider using SharpZipLib or DotNetZip. [Google](http://www.google.com/search?q=.net+zip+library) provides a massive amount of information. – Jonathon Reinhart May 15 '13 at 06:59
  • 1
    For sharpZipLib I should add an extern reference ? – user1577242 May 15 '13 at 07:01
  • 1
    Yes. ["How to use library in C#"](https://www.google.com/search?q=how+to+use+library+in+c%23). [C# Using other people's code](http://stackoverflow.com/questions/8778670/c-sharp-using-others-code/8778720#8778720). – Jonathon Reinhart May 15 '13 at 07:04
  • 1
    @user1577242 Using winzip32.exe or any other zip utility externally, when you could just as easily do it the *right* way, and use a library, is just plain absurd. – Jonathon Reinhart Jan 10 '14 at 03:47
  • DON'T FORGET: need to add a reference to the .NET assembly System.IO.Compression.FileSystem – JustBeingHelpful Jan 13 '16 at 20:25
1

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();
}
Vishal Sen
  • 1,135
  • 1
  • 13
  • 23
0

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/

Freelancer
  • 9,008
  • 7
  • 42
  • 81
  • 1
    WHo is ZIPFile?My application doesn't recognize it . – user1577242 May 15 '13 at 06:46
  • Downvoted because you merely copy and pasted information from a linked page with no other information. Link-only answers should really be comments. – Jonathon Reinhart May 15 '13 at 06:48
  • I could have also posted only code. But i have taken care to post link for it also , so that he can follow further discussion over there and may get something useful. Ofcourse, its upon you, whome to upvote and whome to downvote. – Freelancer May 15 '13 at 06:54
  • Not so. If you had only posted code and no link, then that would have been plagiarism and eligible for deletion. – Jonathon Reinhart May 15 '13 at 06:58
  • if that answer is from stack , then it would be eligible for deletion. Now also you can report this answer if you want. I am confident, will not get deleted. – Freelancer May 15 '13 at 06:59
  • No, I said *if you had posted only the code, and not attributed the source with a link*, then it would be [eligible for deletion](http://meta.stackexchange.com/questions/160071/what-to-do-when-plagiarism-is-discovered). – Jonathon Reinhart May 15 '13 at 07:03
  • I know that my friend, i have flaged many of such answers for this purpose. – Freelancer May 15 '13 at 07:12
  • Just to make a note..this suffers with the 260 char limit problem. Other than that, it is fine. – user203687 Sep 26 '17 at 13:24
0

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

        }
    }
}
UUHHIVS
  • 1,179
  • 11
  • 19
0

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();
0

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);
umutcakar
  • 7
  • 1