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.

最新文章

  1. js高程读书笔记(第4章--变量、作用域和内存)
  2. [No000056]你无法真正占有一个人,包括你的爱人,先生或太太、小孩,以及你自己....
  3. MSA:多重比对序列的格式及其应用
  4. C# 格式化字符串,日期,字符串操作汇总
  5. cookie 二:
  6. yii2构造方法
  7. Swift—Cocoa错误处理模式-备
  8. 轻松管理您的网络password
  9. C# 根据Excel模版导出文件
  10. 基于ko-easyui实现的PC前端模板功能完善
  11. vw实现移动端自适应页面
  12. python学习(一)--python解释器
  13. 小小知识点(二)——如何修改win10 的C盘中用户下的文件夹名称
  14. google像apple 30亿美元购买流量
  15. Type I and type II errors | 第一类错误和第二类错误
  16. Html利用CSS布局技巧
  17. mongodb操作技巧
  18. everything 提供http和ftp的功能
  19. 【刷题】LOJ 6002 「网络流 24 题」最小路径覆盖
  20. @echo 与 echo的区别

热门文章

  1. loadrunner场景中按scenario和group执行的区别
  2. LeetCode 628. 三个数的最大乘积
  3. 【转】Vue v-bind与v-model的区别
  4. 长安大学第四届ACM-ICPC“迎新杯”程序设计竞赛-重现赛 F - 打铁的箱子
  5. SPOJDRUIDEOI - Fata7y Ya Warda!【单调栈】
  6. 【51Nod 1190】最小公倍数之和 V2
  7. POJ 3553 Light Switching Game 博弈论 nim积 sg函数
  8. 【最大化平均值】POJ3111-K Best
  9. Android之View / SurfaceView / GLSurfaceView
  10. bzoj 3809 莫队