如何在C#中使用7Zip压缩文件夹? [英] How would I compress a folder with 7Zip in C#?

查看:741
本文介绍了如何在C#中使用7Zip压缩文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件夹压缩为扩展名为.7z并带有7zip的文件。

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.)

这是C#。

链接到页面或示例代码会有所帮助。

Links to pages or sample code would be helpful.

推荐答案

使用7zip压缩或解压缩文件的代码

code to zip or unzip file using 7zip

此代码用于压缩文件夹

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

此代码用于压缩文件

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

        }

此代码用于解压缩

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

这篇关于如何在C#中使用7Zip压缩文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆