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. java分配内存空间

    分配内存空间 数组名=new 数据类型[数组长度]: new关键字用来实现为数组或对象分配内存 (堆内存) 数组具有固定的长度.获取数组的长度: 数组名.length 定义数组+分配内存空间 数据类型 ...

  2. 后缀数组 + LCP加速多模式匹配算法 O(m+logn)

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  3. windows命令行下redis读取中文字符乱码

    我在eclipse上对redis进行了一个操作,添加了一个中文字符串进去,可以看到是添加成功了的 但是在命令行中读取的时候却成了乱码,如下图所示 这是因为windows命令行的编码是gbk 可以通过如 ...

  4. vue-element Tree树形控件通过id默认选中

    一.设置 1.给树形控件设置 ref="tree" node-key="id" 2.在获取数据的位置加上 this.$nextTick(() => { t ...

  5. H3C MSTP

  6. C# 通过编程的方法在桌面创建回收站快捷方式

    基本所有的桌面都会存在回收站快捷方式,如果想要多创建几个快捷方式,点开就是回收站,请看本文的方法 在引用 Windows Script Host Object Model 这个 COM 方法之后可以使 ...

  7. androidBLE dfu升级使用及可能出现的问题

    android-dfu-library是nordic提供的对nRF5x芯片固件进行空中升级的库,地址是https://github.com/NordicSemiconductor/Android-DF ...

  8. Channel 9视频整理【3】

    Will 保哥 微软mvp https://channel9.msdn.com/Niners/Will_Huang 繁体中文视频 Visual Studio 2017 新功能探索 https://ch ...

  9. HDU4528 小明捉迷藏 [搜索-BFS]

    一.题意 小明S在迷宫n*m中找大明D和二明E,障碍物X不能走,问你计算是否能在时间t内找到大明和二明 二.分析 2.1与普通的BFS不同,这里可以走回头路,这里应该建立四维的标记数组标记数组,例如v ...

  10. UI自动化测试框架 ---TestCafe

    UI自动化测试框架 ---TestCafe 官网文档链接: https://devexpress.github.io/testcafe/ https://devexpress.github.io/te ...