How would I compress a folder with 7Zip in C#?

2019-05-28 14:49发布

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.

标签: c# 7zip
3条回答
闹够了就滚
2楼-- · 2019-05-28 15:09

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;
    }
查看更多
时光不老,我们不散
3楼-- · 2019-05-28 15:19

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!

查看更多
Bombasti
4楼-- · 2019-05-28 15:26

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

查看更多
登录 后发表回答