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

摘要:
C#调用7z来压缩和解压缩文件。1.关于7z,我们先来介绍一下7z压缩软件。7z是具有非常高压缩比的主流压缩格式。在计算机科学中,7z是一种文件格式,可以使用多种压缩算法进行数据压缩。

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

1.关于7z

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

  • 来源且模块化的组件结构
  • 最高的压缩比
  • 强大的AES-256加密
  • 可更改配置的压缩算法
  • 支持操大文件
  • 支持多线程压缩
  • 具有多种压缩文件格式
2.解压缩实现代码
实现对文件的解压缩方法是通过cmd命令,调用7z程式通过cmd命令实现对文件进行解压和压缩的操作,具体实现代码如下:
  • 压缩代码
压缩的cmd命令:"7Z a -tzip " + zipPath + "  " + filePath;
public ExecutionResult CompressFile(string filePath, string zipPath)//运行DOS命令
        {
            ExecutionResult exeRes = new ExecutionResult();
            exeRes.Status = true;
            try
            {
                Process process = new Process();
                process.StartInfo.FileName = "cmd.exe";
                string message = "";
                string command1 = "c:";
                string command2 = @"cd";
                string command3 = @"cd C:Progra~17-Zip";
                string command4 = "";


                command4 = "7Z a -tzip " + zipPath + "  " + filePath;

                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(command1);
                process.StandardInput.WriteLine(command2);
                process.StandardInput.WriteLine(command3);
                process.StandardInput.WriteLine(command4);
                process.StandardInput.WriteLine("exit");
                message = process.StandardOutput.ReadToEnd(); //要等压缩完成后才可以来抓取这个压缩文件

                process.Close();
                if (!message.Contains("Everything is Ok"))
                {
                    exeRes.Status = false;
                    exeRes.Message = message;
                }
                else
                {
                    exeRes.Anything = zipPath;
                }
            }
            catch (Exception ex)
            {
                exeRes.Message = ex.Message;
            }

            return exeRes;
        }
  • 解压代码
解压的cmd命令:"7Z x -tzip " + zipPath + " -o" + filePath + " -y";
public ExecutionResult DeCompressFile( string zipPath, string filePath)//运行DOS命令
        {
            ExecutionResult exeRes = new ExecutionResult();
            exeRes.Status = true;
            try
            {
                Process process = new Process();
                process.StartInfo.FileName = "cmd.exe";
                string message = "";
                string command1 = "c:";
                string command2 = @"cd";
                string command3 = @"cd C:Progra~17-Zip";
                string command4 = "";


                command4 = "7Z x -tzip " + zipPath + " -o" + filePath + " -y";

                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(command1);
                process.StandardInput.WriteLine(command2);
                process.StandardInput.WriteLine(command3);
                process.StandardInput.WriteLine(command4);
                process.StandardInput.WriteLine("exit");
                //process.WaitForExit();
                message = process.StandardOutput.ReadToEnd();//要等压缩完成后才可以来抓取这个压缩文件

                process.Close();
                if (!message.Contains("Everything is Ok"))
                {
                    exeRes.Status = false;
                    exeRes.Message = message;
                }
                else
                {
                    exeRes.Anything = filePath;
                }
            }
            catch (Exception ex)
            {
                exeRes.Message = ex.Message;
            }

            return exeRes;
        }
 

免责声明:文章转载自《C#调用7z实现文件的压缩与解压》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇第三方支付系统测试点提取DICOM文件添加私有Tag(DCMTK Private Tag)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

java常用加解密工具类

packagecom.sh.springboottdemo2.util; importcom.sun.org.apache.xerces.internal.impl.dv.util.Base64; importjavax.crypto.Cipher; importjavax.crypto.KeyGenerator; importjavax.crypt...

Delphi 文件操作资料

var file_name:string; file_name:='c:\luther.gliethttp.txt'; FileWrite(com.log_fd, PChar(file_name)^, length(file_name)); 文件是同一种类型元素的有序集合,是内存与外设之间传输数据的渠道。文件的本质是一个数据流,所有的文件实际上是一串二进...

java后台访问接口

// 发送url地址获取信息 public static String sendPost(String jsonStr, String path) { String msg = "";// 保存调用http服务后的响应信息 try { byte[] data = jsonStr.getByte...

C# + ArcEngine 常用方法(不定时更新)

1、Arcengine调用GP服务,抛出异常方法 object sev = null; try{ Application.DoEvents(); gp.Execut...

C# 扩展类与分布类

一、扩展类 //定义扩展方法 public static classExtsionString { public static string GetTop10(this stringvalue) { return value.Substring(0, 10); } }...

Java 使用stringTemplate导出大批量数据excel(百万级)

目前java框架中能够生成excel文件的的确不少,但是,能够生成大数据量的excel框架,我倒是没发现,一般数据量大了都会出现内存溢出,所以,生成大数据量的excel文件要返璞归真,用java的基础技术,IO流来实现。 如果想用IO流来生成excel文件,必须要知道excel的文件格式内容,相当于生成html文件一样,用字符串拼接html标签保存到文本文...