5

I want to compress a folder into a file with the .7z extension, with 7zip.

I would like to know how I would do this, because I'm not sure (which is why I'd asking.)

This is in C#.

Links to pages or sample code would be helpful.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3026440
  • 85
  • 1
  • 5
  • 1
    Possible duplicate of this question, http://stackoverflow.com/questions/7646328/how-to-use-the-7z-sdk-to-compress-and-decompress-a-file – Aung Kaung Hein Nov 28 '13 at 01:03
  • For a simple solution, use [`Process.Start`](http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start(v=vs.110).aspx) (this requires 7zip to be installed). Otherwise, see the [LZMA SDK](http://www.7-zip.org/sdk.html) – user703016 Nov 28 '13 at 01:04
  • I want to simply compress it in to a .7z file, thats all. – user3026440 Nov 28 '13 at 01:10

3 Answers3

3

code to zip or unzip file using 7zip

this code is used to zip a folder

  public void CreateZipFolder(string sourceName, string targetName)
        {
            // this code use for zip a folder
             sourceName = @"d:\Data Files"; // folder to be zip
             targetName = @"d:\Data Files.zip"; // zip name you can change 
            ProcessStartInfo p = new ProcessStartInfo();
            p.FileName = @"D:\7-Zip\7z.exe";
            p.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
            p.WindowStyle = ProcessWindowStyle.Hidden;
            Process x = Process.Start(p);
            x.WaitForExit();
        }

this code is used to zip a file

 public void CreateZip(string sourceName, string targetName)
        {
            // file name to be zip , you must provide file name with extension
             sourceName = @"d:\ipmsg.log";
             // targeted file , you can change file name 
             targetName = @"d:\ipmsg.zip"; 

            ProcessStartInfo p = new ProcessStartInfo();
            p.FileName = @"D:\7-Zip\7z.exe";
            p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
            p.WindowStyle = ProcessWindowStyle.Hidden;
            Process x = Process.Start(p);
            x.WaitForExit();

        }

this code is used for unzip

 public void ExtractFile(string source, string destination)
        {
            // If the directory doesn't exist, create it.
            if (!Directory.Exists(destination))
                Directory.CreateDirectory(destination);

            string zPath = @"D:\7-Zip\7zG.exe";
            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) { }
        }

image is used to know files in 7zip folder

Vishal Sen
  • 1,135
  • 1
  • 13
  • 23
0

I agree that this is a duplicate, but I've used the demo from this codeproject before and it is very helpful:

http://www.codeproject.com/Articles/27148/C-NET-Interface-for-7-Zip-Archive-DLLs

Scroll down the page for the demo and good luck!

Nick
  • 353
  • 1
  • 2
  • 11
0

Here fileDirPath is the path to my folder which has all my files and preferredPath is the path where I want my .zip file to be.

eg: var fileDirePath = @“C:\Temp”; var prefferedPath = @“C:\Output\results.zip”;

private void CreateZipFile(string fileDirPath, string prefferedPath)
    {
        ProcessStartInfo p = new ProcessStartInfo();
        p.FileName = @"C:\Program Files\7-Zip\7z.exe";
        p.Arguments = "a \"" + prefferedPath + "\" \"" + fileDirPath + "\"";
        p.WindowStyle = ProcessWindowStyle.Hidden;
        Process x = Process.Start(p);
        x.WaitForExit();
        return;
    }
Harshani
  • 649
  • 2
  • 9
  • 22