C#调用7z实现文件的压缩与解压

C#调用7z实现文件的压缩与解压

1.关于7z

首先在这里先介绍一下7z压缩软件,7z是一种主流的 压缩格式,它拥有极高的压缩比。在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式。主要有以下特点:

  • 来源且模块化的组件结构
  • 最高的压缩比
  • 强大的AES-256加密
  • 可更改配置的压缩算法
  • 支持操大文件
  • 支持多线程压缩
  • 具有多种压缩文件格式
2.解压缩实现代码
实现对文件的解压缩方法是通过cmd命令,调用7z程式通过cmd命令实现对文件进行解压和压缩的操作,具体实现代码如下:
  • 压缩代码
       public object CompressZipFile(string sourceFile, string destinationFile)
            {
                object resObj;
                Process process = new Process();
                string tempDestinationFile = "";
    
                try
                {
                    if (process == null)
                    {
                        process = new Process();
                    }
                    tempDestinationFile = destinationFile.ToLower().EndsWith(".zip") ? destinationFile : destinationFile + ".zip";
                    process.StartInfo.FileName = "cmd.exe";
                    string command1 = @"cd c:\progra~1\7-zip";
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();
                    process.StandardInput.WriteLine(@"c:");
                    process.StandardInput.WriteLine(@"cd\");
                    process.StandardInput.WriteLine(command1);
                    process.StandardInput.WriteLine(@"7Z a -tzip " + destinationFile + " " + sourceFile);
                    process.StandardInput.WriteLine("exit");
    
                    string output = process.StandardOutput.ReadToEnd();
                    if (output.ToUpper().IndexOf("EVERYTHING IS OK") > -1)
                    {
                        resObj = new object[] { 0, "Compress file[" + destinationFile + "] OK!" };
                    }
                    else
                    {
                        resObj = new object[] { 1, "Compress File[" + destinationFile + "] Error!" };
                    }
    
                }
                catch (Exception ex)
                {
                    resObj = new object[] { 1, "Compress File[" + destinationFile + "] exception:" + ex.Message };
    
                }
                return resObj;
            }
    
  • 解压代码
       public object decompressFile(string zipFilepath, string FileDir)
            {
                object resObj;
                try
                {
                    string batfiledata = @"c:\progra~1\7-zip\7z.exe x " + zipFilepath + " -o" + FileDir + " -y";
    
                    Process process = new Process();
                    process.StartInfo.FileName = "cmd.exe";
                    process.StartInfo.Arguments = "/c " + batfiledata;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();
    
                    string output = process.StandardOutput.ReadToEnd();
                    if (output.ToUpper().IndexOf("EVERYTHING IS OK") > -1)
                    {
                        resObj = new object[] { 0, "DeCompress file[" + zipFilepath + "] OK!" };
                    }
                    else
                    {
                        resObj = new object[] { 1, "DeCompress File[" + zipFilepath + "] Error!" };
                    }
    
                }
                catch (Exception ex)
                {
                    resObj = new object[] { 1, ex.Message };
                }
    
                return resObj;
    
            }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值