Unzip a file in c# using 7z.exe

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!

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.

How to extract 7z file to a folder C# .net, This article will show you how to extract .7z, .zip, .rar file using 7z.exe in C#.net. Here we add the code to control the 7-Zip executable. In the first  Free Software to Zip, UnZip & Open zipped or compressed files/folders. Unzip your zipped or compressed files easily with zipping software for Windows or Mac.

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/

C# 7 Zip Executable (Process.Start), Extract that file with 7-Zip, and it will have the same data. Tip: First make sure all the files are being copied and the Arguments syntax is correct. The quotes in the​  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.

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

        }
    }
}

7 Zip Command Line Examples, Compress, extract, archive and optimize with the 7z.exe executable. 7-Zip. The 7z.exe program is used to compress, extract and update files through the  why bother trying use 7z.exe application externally? kludgy way of doing it. instead use 1 of many libraries @ disposal. if new application, , targeting .net 4.5, new system.io.compression namespace has zipfile class. alternatively, sharpziplib gpl library file compression in .net. there online samples.

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

How to extract .7z file in c#, I want to extract file of .7z file. I am able to do it for .zip file using ZipFile library but not for .7z. Anyhelp would be appreciated. Worked great! Then I realized I simply had individual LZMA files on my hands, not a 7z archive file. Pavlov does not provide any C# library for this. So I have instead just decided that it will be easiest to just do a process call to 7z.exe. Thank you for your sample, which will get me started. – Steve Greene Nov 25 '15 at 20:15

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

Unzip using 7zip - MSDN, Dim exePath As String = "C:\Program Files\7-Zip\7z.exe" I am trying to unzip the C:\DATA\TestData\K0080130001.zip file into the directory  Extracting a Zip File. In order to extract the contents of an existing Zip file you use ExtractToDirectory () method of the ZipFile class. The ExtractToDirectory () method accepts two parameters - path of the source Zip file and path of the destination folder where it has to be unzipped.

How to zip files, The 7-Zip utility can be used from a command line interface, graphical user interface, or with a window-based shell integration. By default, 7-Zip creates 7z format  7-Zip. The 7z.exe program is used to compress, extract and update files through the command line. It provides superior compression. It is a great program.

7-Zip command line to extract a folder from an archive, You need to use 7z x archive.zip to extract with full paths. See: http://sevenzip.​sourceforge.jp/chm/cmdline/commands/extract_full.htm. First steps. Create a new C# project in Visual Studio. Here we will show a console application. Next, you need to download the executable. Add 7z.exe: Right-click on your project name in the Solution Explorer and select Add Existing Item.

Extract a certain file from an archive with 7-Zip from the command line, As a follow-up to surfasb's answer, add a -r flag at the end to recurse: 7z e [​archive.zip] -o[outputdir] [fileFilter] -r. Multiple filters support: 7z e [archive.zip]  This article will show you how to extract .7z, .zip, .rar file using 7z.exe in C#.net. Here we add the code to control the 7-Zip executable. In the first part of the code, we specify the source file name and the target file name. The source file name is the name of the file that you want to extract.

Comments
  • i could run your command successfully on commandline
  • @Jack Incorrect. Note the @ sign in front of the string. stackoverflow.com/questions/5179389/at-sign-in-file-path-string
  • I'm using version 4.65.0.0 of 7z.exe on Win XP .What version did you use ?
  • stackoverflow.com/questions/34773371/…
  • One issue i see is AppDomain.CurrentDomain.BaseDirectory includes a trailling "\\" so you should remove "\\" from "\\7z.exe".
  • Then I would consider using SharpZipLib or DotNetZip. Google provides a massive amount of information.
  • For sharpZipLib I should add an extern reference ?
  • Yes. "How to use library in C#". C# Using other people's code.
  • @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.
  • DON'T FORGET: need to add a reference to the .NET assembly System.IO.Compression.FileSystem
  • WHo is ZIPFile?My application doesn't recognize it .
  • Downvoted because you merely copy and pasted information from a linked page with no other information. Link-only answers should really be comments.
  • 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.
  • Not so. If you had only posted code and no link, then that would have been plagiarism and eligible for deletion.
  • 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.