C # Compression Components introduction and Getting Started

Source: Internet
Author: User
Tags save file

1. Preface

As the GIZHGA Rights management system as the earliest batch of learning users, learned a lot of things, in the group also made a lot of friends, more important is idle time, we have released a lot of reliable outsourcing work. This is also because Gizhga released a " compressed file corruption detection " outsourcing task, the earliest in the year in C # very simple use of the compression components, this time with this demand, only so in-depth study, by the way this blog.

In the daily project development process, the operation of Zip or RAR compressed files is also a common thing. Mainly packaging files, unzip files and so on the normal operation, also believe that many people also used . NET -brought GZip compression. Today is to introduce some of the open-source compression components of some simple introduction, due to a lot of compression format, efficiency is not the same, so open-source things are very good, support is very comprehensive.

Or the old ritual, this article will be mentioned in this article related components and their source code, documents or official case packaging, unified on March 6, around 11 o'clock in the morning sent, the need to leave the mailbox, expired. Of course, good articles and resources also need the support of the park friends, If you have help, do not stingy mouse oh, your support is my original motivation.

Back to Table of Contents 2. Basics of compression formats and algorithms

Data compression is the process of representing information in a specific encoding mechanism with data bits (or other information-related units) that are less than encoded. With the rapid development of information technology, the use of data compression is becoming more and more important in the network age. For some special data such as photo, audio, video compression algorithm is also different. Today we say that the compression algorithm is lossless compression, lossless data compression index is compressed, the information is not lost, but also fully restored to the original before compression. The term "lossless" is relative to lossy data compression, and lossy data compression allows only one approximation of the original data to be rebuilt in exchange for better compression ratios. The common lossless compression algorithms are LZW, ZIP, RAR, 7-zip and so on. Take a closer look at several mainstream lossless data compression formats:

The zip file format is a popular data compression and document storage file format, formerly known as deflate. Currently, the zip format belongs to one of several mainstream compression formats, and its competitors include the RAR format and the open source 7z format. From the performance comparison, RAR and 7-zip format compression ratio is higher than the ZIP format, and 7-zip due to provide free compression tools and gradually in more areas to be applied. The format is open and free, and more and more software embedded supports opening the zip file.

RAR is a proprietary file format for data compression and archive packaging, and the RAR encoder has always been patented. So that's why we see a lot of open source compression software or tools that can decompress RAR without supporting RAR packaging. RAR is usually higher than zip compression, but the compression/decompression speed is relatively slow.

7z is a file format that can compress data using a variety of compression algorithms. The format was originally implemented and adopted by 7-zip, but the file format is public and the 7-zip software itself is open source under the GNU Wide general Public License (GNU LGPL) agreement. The main features of the 7z format are: Open source and modular component structure (allowing the use of any compression, conversion or encryption algorithm), high compression ratios (different results with different compression algorithms), large file support, and the development structure of this format allows the addition of compression algorithms beyond the standard.

Summary: Overall, I still prefer the 7ZIP format, the compression rate is really much higher, and open source. However, if the actual project for historical reasons, such as the need to use zip or rar, there is no way, or there are many open source components to choose from.

Back to Directory 3.several common . NET Open Source data compression components 3.1 Zip-dotnetzip

DotNetZip is an early one in. NET open source compression components, I used it in 2009 when I gave my tutor a small project, so I was familiar with its basic operation. Now the latest version is 1.9.1.8, a long time not updated, but also very stable. Website: http://dotnetzip.codeplex.com/. With DotNetZip, you can easily create, unzip, and update Zip files. The official website has many examples, as well as the introduction, the length is relatively long, does not go to translate. Simple to use, look at the following section of the use of the example is OK.

3.2 7zip-sevenzipsharp

Sevenzipsharp is an open source 7-zip format operation component in. NET that supports all 7ZIP formats. The latest version is released in 2010 at 0.64. Due to the high compression rate of 7Zip and the open source features, it is the best choice to use in new projects. Website: http://sevenzipsharp.codeplex.com/. It supports more formats than above, such as: 7zip,rar,zip,gzip,cab,lzh and so on. Note that in addition to referencing this component, you need to copy the 7z.dll file to the bin directory, because Sevenzipsharp is a package for 7z.dll. 7z.dll can be downloaded on the official website http://7-zip.org/, or to see my final data download, will be packaged in.

3.3 Comprehensive-sharpcompress

Sharpcompress, too. NET under the open source compressed file operations component. Unlike the above 2, he supports more formats, such as Rar,zip,tar,7zip and so on. This open source project seems to have started last year and has evolved from several other open source projects, including the Dotnetzip,nunrar project. Of course, RAR is also just decompression, the above mentioned RAR is a patented algorithm, so do not support the creation of RAR files. Of course, this also supports a lot of other compression formats, such as TAR, gzip, and so on, not much to say, examples of our most common zip, rar and 7ZIP.

3.4about calling WINRAR

There is also a common way to work with compressed files, which is to use the WinRAR command line. There are many examples of operations on the Internet, I have also tested and succeeded. But it has to be said that flexibility is poor. Certainly can satisfy a small number of people's needs, but I think this method is not very good indeed. So it is not advocated here, after all, there are ready-made better things. Why do you say this winrar? The main is that we all know WinRAR is charged, RAR is a patented algorithm, but in fact WinRAR still provides a free program call way, is Unrar, In the WinRAR installation folder there is a UnrarSrc.txt file, open it, inside has address: www.rarlab.com. Need to see, I find this thing can take a lot of time, is because the data is too little, so also leave a footprint it.

Back to Table of Contents 4. Basic Introductory Use Tutorial 4.1 basic use of DotNetZip
1//Create a compressed file2using (ZipFile zip =NewZipFile ())3{4//Set a password, or you can set a separate password for each file5 zip. password="123456!";6//Add File7 zip. AddFile ("ReadMe.txt");8//Add Directory9 zip. Adddirectory (@"Mydocuments\projectx");10//Set up note informationOne zip.comment ="This is a demo";12//You can also set the compression mode, encoding, etc.13//Save FileZip. Save ("Package.zip");15}1617//Unzip the file18using (ZipFile zip = Zipfile.read ("Demo.zip"))19 {//Traverse each file object in the zip file and extract to the specified directory20foreach (ZipEntry EInchZip21  {22 e.extract (@ "c:\test", true); 23 // If you have a password, use the following method 24  E.extractwithpassword (basedirectory, Password); 25 }26 //  You can also access file objects by index 27 //zipentry e = zip["Myreport.doc"]; 28}             

Of course, this is just the simplest compression and decompression zip file operation, there are more functions can operate zip, such as removing files, through the file stream to compress and decompress and so on. You can take a look at the help documentation, just to introduce the most basic features. Because this research is mainly to detect the wrong zip file, so here, leave a question to everyone: how to tell if a zip file is damaged? After a while will give you a train of thought, of course, not the only solution.

4.2 Sevenzipsharp Basic Use

This shows only basic usage, class libraries, exception handling, file flow operations, and so on. You can see the help documentation.

1//Initializes a compressor that can compress multiple files at a time2 Sevenzipcompressor sc =NewSevenzipcompressor ();3//Set the compression format, here is the enumeration type, you can choose other4 SC. Archiveformat =Outarchiveformat.zip;5//Compression mode, newly created or appended, if appended6 Sc.compressionmode =Compressionmode.create;7//Set the compression algorithm, or do not set, with the default8//You can use Zipencryptionmethod to set the password for each file9 Sc.compressionmethod =SevenZip.CompressionMethod.Default;10//Individual compressed files, compressfilesencrypted method can set the password11//Note the file path here, to write the full nameSc.compressfiles ("Test.zip",@"C:\X\a.txt",@"C:\X\b.txt");13//Compressdirectory method to compress a directory separately14//The Compressfiledictionary method can compress a file or directory, pass in a dictionary, and automatically recognize a directory or file1516//Decompression, you can set the decompression password at the time of initializationSevenzipextractor SE =New Sevenzipextractor ("Test.zip "); 18 foreach (var Item in SE. Archivefilenames) 19 {// 20 se. Beginextractfiles (@ "c:\x  "21 }22 // All-in-one decompression 23 //< Span style= "COLOR: #008000" > SE. Beginextractarchive (@ "c:\x");              
4.3 sharpcompress basic Use

Sharpcompress is developed on the basis of DotNetZip and other open source projects, so its use is similar to DotNetZip. Here is a URL, there are examples, we have to figure out: Example link

Back to Table of Contents 4. Summary and Resources

Summing up, I think dotnetzip use the most flexible, and sevenzipsharp and sharpcompress support the format is many, and 7zip compression rate is very large, format open source, use more occasions. RAR format try not to use it, business algorithms, not to use, the General class Library can be decompressed, but compression can be used in the above section 3.4 mentioned Unrar. There may also be many other open source components, the shortcomings, please also point out.

Here are the URLs of the above open source class libraries and related usage resources:

dotnetzip:http://dotnetzip.codeplex.com/

sevenzipsharp:http://sevenzipsharp.codeplex.com/

sharpcompress:http://sharpcompress.codeplex.com/

C # Compression Components introduction and Getting Started

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.