Use C # To Compress/decompress a 7-zip file

Source: Internet
Author: User

7-Zip Introduction

7-Zip is a compression software that claims to have the highest compression ratio today. It not only supports the unique 7z file format, but also supports a variety of other compression file formats, including ZIP, RAR, CAB, GZIP, BZIP2, TAR, etc. The compression ratio of this software is 30-50% higher than that of common ZIP files. Therefore, it can compress Zip files by 2-10%.
7-Zip Main Features
The algorithm is updated to increase the compression ratio of the 7z format.
Supported formats:
Compression and decompression: 7z, ZIP, GZIP, BZIP2, and TAR
Extract only: RAR, CAB, ISO, ARJ, LZH, CHM, WIM, Z, CPIO, RPM, DEB, and NSIS
For ZIP and GZIP formats, 7-Zip provides 2-10% higher compression ratio than PKZip and WinZip.
In the 7z format, you can create an auto-release (SFX) compressed file.
Integrated Windows Shell Extension
Powerful File Management
Powerful command line version
Support for FAR Manager plug-ins
Supports 69 languages

How to compress/decompress a 7-zip file in C #

1. Control the console to use the 7z.exe File

public static void Unzip(DirectoryInfo DirecInfo)
{
if (DirectInfo.Exists)
{
foreach (FileInfo fileInfo in DirecInfo.GetFiles("*.zip"))
{
Process process = new Process();
process.StartInfo.FileName = @"C:\Program Files\7-zip\7z.exe";
process.StartInfo.Arguments =                         @" e C:\Directory\" + fileInfo.Name + @" -o C:\Directory";
process.Start();
}
}
}

 

2. lzma sdk Based on the compression algorithm
Lzma sdk contains the C # source code of the compression and decompression algorithms (the current version is 4.65)
: http://sourceforge.net/projects/sevenzip/files/LZMA%20SDK/4.65/lzma465.tar.bz2/download
3.Use the 7-Zip compression/Decompression function in. NET Applications(AuthorAbel AvramTranslatorZhao Yi)
Developer Eugene Sichkar creates a series of C # interfaces for 7-Zip dynamic link libraries, and uses the 7-Zip compression/Decompression function in. NET applications.
According to Eugene, the project implements the following interfaces:
  • IProgress-Basic progress callback
  • IArchiveOpenCallback-Open the callback of the compressed package
  • ICryptoGetTextPassword-Indicates the password callback for compression.
  • IArchiveExtractCallback-Callback for extracting the compressed package
  • IArchiveOpenVolumeCallback-Enable the callback for the extra compressed volume
  • ISequentialInStream-Basic read-only data stream Interfaces
  • ISequentialOutStream-Basic write-only Interfaces
  • IInStream-Input data stream interfaces that can be randomly read
  • IOutStream-Output data stream interface
  • IInArchive-Main compression Interfaces

For more information, see the example in the source code.

4.SevenZipSharp

Markhor has created the SevenZipSharp project, which is open-source and implements self-extracting and compressing all formats supported by 7-ZIP. it improves some methods of the C # interface of the 7-Zip dynamic link library.

Examples of common compression/DecompressionSevenZipSharpExample file ):

Decompress the file

using (SevenZipExtractor tmp = new SevenZipExtractor(@"d:\Temp\7z465_extra.7z"))
{
for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
{
tmp.ExtractFiles(@"d:\temp\!Пусто\", tmp.ArchiveFileData[i].Index);
}
//tmp.ExtractFiles(@"d:\temp\!Пусто\", 1, 3, 5); 
}
 
 

Volume Compression

SevenZipExtractor.SetLibraryPath(@"d:\Work\Misc\7zip\9.04\CPP\"+                                  7zip\Bundles\Format7zF\7z.dll");
using (SevenZipExtractor tmp = new SevenZipExtractor(@"d:\Temp\SevenZip.7z.001"))
{
tmp.ExtractArchive(@"d:\Temp\!Пусто");
}

Compressed file

SevenZipCompressor tmp = new SevenZipCompressor();
tmp.CompressFiles(@"d:\Temp\arch.7z", @"d:\Temp\log.txt");
tmp.CompressDirectory(@"c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\1033",                  @"D:\Temp\arch.7z");

ZIP file Compression

SevenZipCompressor tmp = new SevenZipCompressor();
tmp.ArchiveFormat = OutArchiveFormat.Zip;
tmp.CompressFiles(@"d:\Temp\arch.zip", @"d:\Temp\gpl.txt", @"d:\Temp\ru_office.txt");

Multi-thread Decompression

Thread t1 = new Thread(() =>
{
using (SevenZipExtractor tmp = new SevenZipExtractor(@"D:\Temp\7z465_extra.7z"))
{
tmp.FileExtractionStarted += new EventHandler<FileInfoEventArgs>((s, e) =>
{
Console.WriteLine(String.Format("[{0}%] {1}",
e.PercentDone, e.FileInfo.FileName));
});
tmp.ExtractionFinished +=                new EventHandler((s, e) => { Console.WriteLine("Finished!"); });
tmp.ExtractArchive(@"D:\Temp\t1");
}
});
Thread t2 = new Thread(() =>
{
using (SevenZipExtractor tmp = new SevenZipExtractor(@"D:\Temp\7z465_extra.7z"))
{
tmp.FileExtractionStarted += new EventHandler<FileInfoEventArgs>((s, e) =>
{
Console.WriteLine(String.Format("[{0}%] {1}",
e.PercentDone, e.FileInfo.FileName));
});
tmp.ExtractionFinished +=                 new EventHandler((s, e) => { Console.WriteLine("Finished!"); });
tmp.ExtractArchive(@"D:\Temp\t2");
}
});
t1.Start();
t2.Start();
t1.Join();
t2.Join();

Multi-thread Compression

Thread t1 = new Thread(() =>
{
SevenZipCompressor tmp = new SevenZipCompressor();
tmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>((s, e) =>
{
Console.WriteLine(String.Format("[{0}%] {1}",
e.PercentDone, e.FileName));
});
tmp.CompressDirectory(@"D:\Temp\t1", @"D:\Temp\arch1.7z");
});
Thread t2 = new Thread(() =>
{
SevenZipCompressor tmp = new SevenZipCompressor();
tmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>((s, e) =>
{
Console.WriteLine(String.Format("[{0}%] {1}",
e.PercentDone, e.FileName));
});
tmp.CompressDirectory(@"D:\Temp\t2", @"D:\Temp\arch2.7z");
});
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.