使用 7zip 压缩文件的示例 C# .net 代码 [英] Sample C# .net code for zipping a file using 7zip

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

问题描述

我已经在我的机器上的 C:Program 文件中安装了 7-zip 4.65.我想在 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 Files7-Zip7zG.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 
              }
        }

创建压缩文件

public void CreateZip()
{
    string sourceName = @"d:aexample.txt";
    string targetName = @"d:a123.zip";
    ProcessStartInfo p = new ProcessStartInfo();
    p.FileName = @"C:Program Files7-Zip7zG.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天全站免登陆