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. 在HTML中的下拉框中怎样实现超连接?

    给你个例子自己改吧: <SELECT name="select" onchange="window.open(this.options[this.selectedI ...

  2. SELECT command denied to user 'username'@'ip' for table 'user'错误处理

    错误信息 使用RDS for MySQL,程序执行查询SQL时报错如下: SELECT command denied to user 'username'@'ip' for table 'user' ...

  3. H3C DHCP地址分配方式

  4. H3C查看文件内容

    <H3C>more logfile.log 创建一个目录 <H3C>mkdir gaochengwang 重命名目录及文件 <H3C>rename wnt 0904 ...

  5. Linux 内核控制 urb

    控制 urb 被初始化几乎和 块 urb 相同的方式, 使用对函数 usb_fill_control_urb 的 调用: void usb_fill_control_urb(struct urb *u ...

  6. 2019-1-29-UWP-IRandomAccessStream-与-Stream-互转

    title author date CreateTime categories UWP IRandomAccessStream 与 Stream 互转 lindexi 2019-01-29 16:33 ...

  7. shell 脚本文件十六进制转化为ascii码代码, Shell中ASCII值和字符之间的转换

    Shell中ASCII值和字符之间的转换     1.ASCII值转换为字符        方法一: i=97 echo $i | awk '{printf("%c", $1)}' ...

  8. AOP 事物连接,记忆连接数据库,连接池

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

  9. 【Linux】Linux下date,time等时间设置

    date命令的帮助信息 [root@localhost source]# date --help用法:date [选项]... [+格式] 或:date [-u|--utc|--universal] ...

  10. springboot多租户设计

    1. 概述 根据不同用户的请求,选择不同的数据源,不同的数据源可以是Oracle.MySQL或者其它.用到的技术栈,没有什么复杂的技术,可以看到,依赖也就加了几个而已,如下: 2. 先睹为快 如下图, ...