对于使用荏苒一个7zip的样本文件C#.NET代码 [英] Sample C# .net code for zipping a file using 7zip

查看:268
本文介绍了对于使用荏苒一个7zip的样本文件C#.NET代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了7-Zip的4.65,在C:\Program文件。我想用它在C#代码来压缩文件。文件名称将通过动态的用户提供。任何一个可以请提供有关如何在C#代码中使用7zip的一个示例代码?

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?

推荐答案

很多回答上述但我用这下面提及的代码压缩或使用解压缩文件7zip的

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

您必须在您的系统中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 
              }
        }

创建zip文件

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

这篇关于对于使用荏苒一个7zip的样本文件C#.NET代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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