8

I have installed 7-zip 4.65 on my machine at C:\Program files. I want to use it in C# code to zip a file. The file name will be provided by the user dynamically. Can any one please provide a sample code on how to use 7zip in C# code?

stukelly
  • 4,257
  • 3
  • 37
  • 44
user386647
  • 81
  • 1
  • 1
  • 2

5 Answers5

11

lots of answer given above but i used this below mention code to zip or unzip a file using 7zip

you must have 7zip application in your system .

     public void ExtractFile(string source, string destination)
        {
            // If the directory doesn't exist, create it.
            if (!Directory.Exists(destination))
                Directory.CreateDirectory(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 zip file

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
  • This is a project I'm working on right now. At first I downloaded the LZMA SDK from the 7-zip site and wrote some code using Pavlov's C# library to compress. 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
  • 1
    pro.Arguments = "x \"" + source + "\" -o" + destination; Please anyone can explain that what is the meaning of (x , ~o) these characters in argument – Apoorva Asthana Aug 14 '19 at 05:39
  • 3
    @ApoorvaAsthana Please go and check this link https://sevenzip.osdn.jp/chm/cmdline/commands/index.htm – Vishal Sen Aug 21 '19 at 15:21
  • I'd use the try-catch block in CreateZip in same way it's done in ExtractFile and add a check for existence of sourceName as a file or a folder. – Fil Sep 14 '22 at 09:35
4

Instead of the binary version you need the source code.

This can be get as the LZMA SDK.

There you'll find a folder CS that contains a C# implementation of the algorithm for 7zip files.

Oliver
  • 43,366
  • 8
  • 94
  • 151
3

Have you tried this C# interface for 7zip: http://www.codeproject.com/KB/DLL/cs_interface_7zip.aspx

[edit] Looks like this has been answered already: Free compression library for C# which supports 7zip (LZMA)

further libraries:

http://www.eggheadcafe.com/tutorials/aspnet/064b41e4-60bc-4d35-9136-368603bcc27a/7zip-lzma-inmemory-com.aspx

http://sevenzipsharp.codeplex.com/

http://www.7-zip.org/sdk.html - From the official site so probably best to use this

Community
  • 1
  • 1
gef
  • 7,025
  • 4
  • 41
  • 48
0

Or you can use the J# zip library (which is included in the .Net Framework) a sample: http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx

Flakron Bytyqi
  • 3,234
  • 20
  • 20
  • Can J# do the .7z format? (OP didn't specify which format, but I know 7zip defaults to its own format, though it can do .zip too.) – Joe White Aug 15 '10 at 12:15
0

I suppose if you wish to use the installed one you have in c:\program files, you could just use System.Diagnostics.Process to run command line apps - http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

Passing parameters is easy too. There are plenty of examples here - http://www.c-sharpcorner.com/UploadFile/DipalChoksi/ShellCommandsInCS12032005042031AM/ShellCommandsInCS.aspx

Frank Tzanabetis
  • 2,756
  • 2
  • 22
  • 22