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~1\7-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~1\7-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实现文件的压缩与解压的更多相关文章

  1. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  2. Asp.net中文件的压缩与解压

    这里笔者为大家介绍在asp.net中使用文件的压缩与解压.在asp.net中使用压缩给大家带来的好处是显而易见的,首先是减小了服务器端文件存储的空间,其次下载时候下载的是压缩文件想必也会有效果吧,特别 ...

  3. [转帖]Linux操作系统中,*.zip、*.tar、*.tar.gz、*.tar.bz2、*.tar.xz、*.jar、*.7z等格式的压缩与解压

    Linux操作系统中,*.zip.*.tar.*.tar.gz.*.tar.bz2.*.tar.xz.*.jar.*.7z等格式的压缩与解压 https://blog.csdn.net/gatieme ...

  4. HDFS中文件的压缩与解压

    HDFS中文件的压缩与解压 文件的压缩有两大好处:1.可以减少存储文件所需要的磁盘空间:2.可以加速数据在网络和磁盘上的传输.尤其是在处理大数据时,这两大好处是相当重要的. 下面是一个使用gzip工具 ...

  5. C# -- 文件的压缩与解压(GZipStream)

    文件的压缩与解压 需引入 System.IO.Compression; 1.C#代码(入门案例) Console.WriteLine("压缩文件..............."); ...

  6. Linux操作系统中,*.zip、*.tar、*.tar.gz、*.tar.bz2、*.tar.xz、*.jar、*.7z等格式的压缩与解压

    zip格式 压缩: zip -r [目标文件名].zip [原文件/目录名] 解压: unzip [原文件名].zip 注:-r参数代表递归 tar格式(该格式仅仅打包,不压缩) 打包:tar -cv ...

  7. Linux操作系统中,.zip、.tar、.tar.gz、.tar.bz2、.tar.xz、.jar、.7z等格式的压缩与解压

    zip格式 压缩: zip -r [目标文件名].zip [原文件/目录名] 解压: unzip [原文件名].zip 注:-r参数代表递归 tar格式(该格式仅仅打包,不压缩) 打包:tar -cv ...

  8. linux下tar gz bz2 tgz z等众多压缩文件的压缩与解压方法

    Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲ta ...

  9. C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

随机推荐

  1. 命名实体识别视频51cto

    https://edu.51cto.com/center/course/lesson/index?id=402918 https://edu.51cto.com/course/18466.html

  2. H3C 寻找邻居

  3. activeMQ的两个默认端口8161和61616的区别

    activeMQ默认配置下启动会启动8161和61616两个端口,其中8161是mq自带的管理后台的端口,61616是mq服务默认端口 . 8161是后台管理系统,61616是给java用的tcp端口 ...

  4. 对象转json字符串案例

    测试对象与json字符串的转换 json字符串转对象 Stringstr = "{\"id\":\"1001\",\"name\" ...

  5. vue-learning:7-template-v-bind-with-class-and-style

    绑定元素样式的指令v-bind:class 和v-bind:style 在HTML元素结构中,class和style特性(attribute)是非常突出的,可以为元素添加样式属性(property). ...

  6. JavaSE基础---多线程

    进程:正在进行的程序.其实就是一个应用程序运行时的内存分配空间. 线程:进程中一个程序执行控制单元,一条执行路径.进程负责的事应用程序的空间的标识,线程负责的事应用程序的执行顺序. 进程和线程的关系: ...

  7. CodeForces 1204 (#581 div 2)

    传送门 A.BowWow and the Timetable •题意 给你一个二进制数,让你求小于这个数的所有4的幂的个数 •思路 第一反应是二进制与四进制转换 (其实不用真正的转换 QwQ) 由于二 ...

  8. WPF 设置纯软件渲染

    最近看到有小伙伴说 WPF 使用硬件渲染,如何让 WPF 不使用硬件渲染,因为他觉得性能太好了.万一这个版本发布了,产品经理说下个版本要提升性能就不好了.于是就找到一个快速的方法,让程序不使用硬件渲染 ...

  9. CP防火墙升级和打补丁

    CP防火墙的升级和打补丁可以在命令行下操作,也可以在web ui下进行,CP的升级首先得升级Deployment Agent软件 Step1:升级Deployment Agent ========== ...

  10. spring MVC 核心配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...