C#调用7z实现文件的压缩与解压
作者:農碼一生 发布时间:2021-06-12 16:12:48
标签: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;
}
来源:https://www.cnblogs.com/wml-it/p/12148778.html
0
投稿
猜你喜欢
- 要说在 Spring Boot 中注册过滤器有三种方式,你都能想到哪些呢?今天松哥就来和大家聊一聊 Spring Boot 中注册过滤器的三
- 前文传送门:ByteBuf使用subPage级别内存分配ByteBuf回收之前的章节我们提到过, 堆外内存是不受jvm垃圾回收机制控制的,
- Javaweb获取表单数据的几种方式一、通过键值对的形式获取表单数据getParameter(String name):通过key,返回一个
- 在基于Mybatis-plus实现多租户架构中,介绍了在多租户项目中如果要开启一个子线程,那么需要手动进行RequestAttributes
- 外观模式: 又称门面模式: 外观Facade为子系统的一组接口提供一个一致界面,使得这组子系统易于使用(通过引入一个新的外观角色降低原系统复
- 概述ReentrantReadWriteLock不知道大家熟悉吗?其实在实际的项目中用的比较少,反正我所在的项目没有用到过。Reentran
- Maven打包没有指定主类在使用IDEA开发项目的时候经常会遇到使用Maven打包项目(打成jar包或者zip包),但是之前不太清楚打包插件
- Android 开发的程序员开发程序的时候,一定为log而苦恼过吧。Eclipse老是Log找不到,是不是很让人不爽,虽然Android S
- 用Java编写简单的五子棋,供大家参考,具体内容如下前言这两天在空闲时间做了个五子棋项目,分享给大家看一下,界面是这样的:界面很丑我知道,本
- 序言for循环语句是java循环语句中最常用的循环语句,一般用在循环次数已知的情况下使用。for循环语句的语法格式如下:java语言 for
- IDEA单元测试报错:Class not found:xxxx springboot报错引入了新依赖,想着在测试模块进行测试。结果报错说Cl
- 最近项目中有截屏的需求,普通的view截屏方法网上一搜一大把,但是SurfaceView截屏黑屏问题很多文章说的并不清楚,自己参考了一些别的
- Java有四种访问权限,其中三种有访问权限修饰符,分别为private,public和protected,还有一种不带任何修饰符:1.&nb
- 问题遇到问题:在前后端分离跨域访问的项目中shiro进行权限拦截失效 (即使有正确权限的访问也会被拦截) 时造成302重定向错误等问题报错:
- 1.需求WPF本身没有直接把点集合绘制成曲线的函数。可以通过贝塞尔曲线函数来绘制。贝兹曲线由线段与节点组成,节点是可拖动的支点,线段像可伸缩
- “Java is still not dead—and people are starting to figure that out.”本教
- 1.工作原理(算法思路)给定一个待排序数组,找到数组中最小的那个元素如果最小元素不是待排序数组的第一个元素,则将其和第一个元素互换在剩下的元
- 左值右值定义:左值指的是既能够出现在等号左边也能出现在等号右边的变量(或表达式),右值指的则是只能出现在等号右边的变量(或表达式).int
- 以卖票的例子来介绍多线程和资源共享,下面我们
- 注解的介绍@ControllerAdvice@ControllerAdvice注解是Spring3.2中新增的注解,学名是Controlle