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

谁说我不能喝 提交于 2019-12-08 02:27:51

问题


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.


回答1:


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




回答2:


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!




回答3:


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


来源:https://stackoverflow.com/questions/20255968/how-would-i-compress-a-folder-with-7zip-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!