7-Zip can be used in C# programs. It provides excellent compression ratios. We embed the 7-Zip command-line executable in a C# program. We then invoke it with the Process class.

Compression results

Standard .NET GZIP: 895,425 bytes (Uses GZipStream class)
7-Zip GZIP: 825,285 bytes
7-Zip GZIP Ultra: 819,631 bytes

New project

First, you need to create a new C# project, usually either Console or Windows Forms, although any will work. Here we will show a console application. Next, you need to download the executable.

Download:Go to the downloads page at 7-Zip.org and download the console application. The description is "7-Zip Command Line Version" and the filename is 7za457.zip. This is the file we will embed in our program. After you download, unzip it.

7-Zip site

Add 7za.exe

Right-click on your project name in the Solution Explorer and select Add Existing Item. You need to change the drop-down icon on the dialog to "All Files (*.*)", which will show the 7za.exe executable.

Copy if newer. With Visual Studio, you must specify "Copy if newer" or "Copy always" to copy files such as executables to the output directory. Specify either of those options to copy the 7za.exe executable.

Add example text file. We need an example file to test 7-Zip with, so add a new text file to your console project. Make sure to specify "Copy if newer" for it too. This will be our source file.

Implementation

Here we add the code to control the 7-Zip executable. In the first part of the code, we specify the source file name and the target file name. The source file name is the name of the text file you added to the project. The target name is the location of the archive you want to create.

Note:If the archive is already there, you will have to delete it or tell 7-Zip to overwrite it.

Program that calls 7-Zip executable: C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics; class Program
{
static void Main()
{
string sourceName = "ExampleText.txt";
string targetName = "Example.gz"; // 1
// Initialize process information.
//
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "7za.exe"; // 2
// Use 7-zip
// specify a=archive and -tgzip=gzip
// and then target file in quotes followed by source file in quotes
//
p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden; // 3.
// Start process and wait for it to exit
//
Process x = Process.Start(p);
x.WaitForExit();
}
}

We use ProcessStartInfo and set the FileName to the name of the 7za.exe executable in the project. Pay close attention to how the quotes are escaped and used in the line where Arguments is set. In this example, I use GZip compression.

Next:We actually start the Process and execute it. Windows can cause problems here if you are not an administrator on your PC.

Verify results

Now we need to check if our project worked properly. Open the bin\Debug or bin\Release folder in your project's directory, and you should see the compressed file. Extract that file with 7-Zip in Windows, and it will have the same data.

Bugs. Here we look at possible problems with this tutorial. First make sure all the files are being copied and the Arguments syntax is correct. The quotes in the syntax are really important if you deal with more complicated paths. After that, you might have problems with Vista's UAC stuff.

Note:The first argument (a) in the command doesn't have a hyphen (-) before it.

Options

I won't go over every option, because 7-Zip itself provides an excellent reference. Go to the 7-Zip folder and double-click on the "7-zip.chm" file. That's a compiled HTML help file that you can browse for many options.

Specify compression levels:You want the smallest possible compressed files, but don't want to wait for hours. On the example, I use -mx=9, which sets the maximum for Zip and GZip. Experiment with this.

Specify archive type:You may need something different from GZip in your program. The ".7z" format probably has the best compression ratio. If you want to use HTTP compression, use GZip, but if you are archiving, then I suggest 7z. Specify the compression with these flags.

Is this worthwhile?

Yes, but only if you are trying to achieve excellent compression ratios. Otherwise, use GZipStream in your C# programs or just Windows' zip capabilities. If you have a website, use 7-Zip to compress static resources. Use GZip like I show in the code example, and with level 9 compression, your web pages will be 10% smaller than most GZip-compressed pages.

GZipStream Example

HTTP compression. For my web site's static pages, 7-Zip was a substantial improvement over standard compression methods. Look at how I reduced the size of my files 10% over standard GZip. You can see the results of these experiments at the top of this document.

Note:There is a thorough guide to using 7-Zip and 7za.exe on the console, which can help with many problems using 7-Zip on the console.

7-Zip Command-Line Examples

Summary

We invoked the 7-Zip executable from C# code. Sometimes it is best to go outside the .NET Framework when developing and use an open-source exe like 7-Zip. For me, 10% is a big improvement and 7-Zip is definitely worthwhile.

Review:Here I showed an effective way of embedding 7-Zip and improving compression ratios.

C# 7-Zip Executable的更多相关文章

  1. Can't use Subversion command line client: svn Probably the path to Subversion executable is wrong. Fix it.

    1.最近使用SVN工具时,Checkout出项目到本地后后,然后将其导入到Intellij idea中开发,在提交svn代码的时候,出现这样的错误:Can't use Subversion comma ...

  2. linux专题一之文件归档和压缩(tar、file、zip)

     本文主要从以下几个方便来说明文件的归档和压缩,同时比较几种不同压缩方法的压缩比率及特点. 文件归档命令tar,tar.gz源码包的安装管理 创建tar包-解压-查询tar包内容 zip命令的用法 为 ...

  3. Android系统Recovery工作原理之使用update.zip升级过程分析(一)

    通过分析update.zip包在具体Android系统升级的过程,来理解Android系统中Recovery模式服务的工作原理.我们先从update.zip包的制作开始,然后是Android系统的启动 ...

  4. Chromedriver executable needs to be in path 解决办法

    执行webdriver.Chrome()时报错:Chromedriver executable needs to be in path. 原因可能是为有安装Chromedriver 可能是Chrome ...

  5. Android studio 安装已经下载好的gradle.zip文件【ubuntu 14.04 LTS环境】

    一 下载 gradle-3.3-all.zip 包 http://download.csdn.net/detail/t6546545/9732412 http://www.fxxz.com/soft/ ...

  6. Python解压ZIP、RAR等常用压缩格式的方法

    解压大杀器 首先祭出可以应对多种压缩包格式的python库:patool.如果平时只用基本的解压.打包等操作,也不想详细了解各种压缩格式对应的python库,patool应该是个不错的选择. pato ...

  7. Spring Boot Executable jar/war 原理

    spring boot executable jar/war spring boot里其实不仅可以直接以 java -jar demo.jar的方式启动,还可以把jar/war变为一个可以执行的脚本来 ...

  8. Android系统Recovery工作原理之使用update.zip升级过程分析(一)---update.zip包的制作【转】

    本文转载自:http://blog.csdn.net/mu0206mu/article/details/7399822 这篇及以后的篇幅将通过分析update.zip包在具体Android系统升级的过 ...

  9. python版本下载时时,官方目录web-based与executable和embeddable 的区别

    背景:安装python时不知道选择哪个版本以及他们之间的意思. 1.X86和X86-64的区别:系統是32 bit 的版本还是 64bit 的 2.web-based ,executable , em ...

随机推荐

  1. java SE :标准输入/输出

    一 标准设备输入/输出 A 标准输入/输出类 System B 控制台读写类 Console  标准输入/输出类  System 1 标准输入.标准输出.错误输出流 // 标准输入流 public f ...

  2. git的使用总结【干货·转载】

    源文地址:https://juejin.im/post/5a54386af265da3e3b7a6317 摘抄: 版本树 / graph / network 干净简洁清晰 提交信息明确 易维护易读 举 ...

  3. 专业的抢票软件 12306bypass

    专业的抢票软件 https://www.12306bypass.com/

  4. thinkjs REST API的跨域设置

    用thinkjs也有一小段时间了,和其它国产框架一样,起初是处于观望态度.当然我最先的选择也不是thinkjs而是选的express,用到后面发现实现一个能让自己用着比较顺手的博客还是一件蛮困难或者说 ...

  5. python中if和elif的区别

    多个if语句是每次单独判断 比如: 例子一: a = 5 if a < 6: #条件1 print(1) if a < 7: #条件2 print(2) else: print(3) 条件 ...

  6. JSTL-2

    流程控制标签:if标签, choose标签, when标签, otherwise标签 <c:if>:的两种语法 1.<c:if test=""   var=&qu ...

  7. Unity 游戏开发技巧集锦之创建部分光滑部分粗糙的材质

    Unity 游戏开发技巧集锦之创建部分光滑部分粗糙的材质 创建部分光滑部分粗糙的材质 生活中,有类物体的表面既有光滑的部分,又有粗糙的部分,例如丽江的石板路,如图3-17所示,石板的表面本来是粗糙的, ...

  8. input file实现多次上传文件(不会覆盖上次上传的文件)

    html原生的file多选控件:<input class="className" type="file" name="name" ac ...

  9. 压测工具Webbench

    webbench最多可以模拟3万个并发连接去测试网站的负载能力,安装使用也特别方便,并且非常小. 1.系统:Linux 2.编译安装: [root@~]$wget http://blog.s135.c ...

  10. CVE-2017-7269—IIS 6.0 WebDAV远程代码执行漏洞分析

    漏洞描述: 3月27日,在Windows 2003 R2上使用IIS 6.0 爆出了0Day漏洞(CVE-2017-7269),漏洞利用PoC开始流传,但糟糕的是这产品已经停止更新了.网上流传的poc ...