Use icsharpcode. sharpziplib for compression

Source: Internet
Author: User

# Ziplib isZip, Gzip, tar and Bzip2 LibraryWritten entirely in C # For. NET platform. it is implemented as an assembly (installable In the GAC), and thus can easily be ininitialized into other projects (in any.. Net Language ). # ziplib was ported from the GNU classpath zip library for use with # developer (http://www.icsharpcode.net/OpenSource/SD) which needed gzip/zip compression. later Bzip2 compression and tar archiving was added due to popular demand.

Download: http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
Compared with the copression provided by. net, ziplib is superior in terms of compression. It is much smaller than Bzip2 compression. You can try it if you don't believe it. In addition, this function is more powerful. The following is a small example. The specific application source code is/files/Yank/compress.rar.

1 using system;
2 using system. Data;
3 using system. IO;
4 using icsharpcode. sharpziplib. Zip. compression;
5 using icsharpcode. sharpziplib. Zip. Compression. streams;
6 using icsharpcode. sharpziplib. gzip;
7
8/** // <summary>
9 // summary description for icsharp
10 /// </Summary>
11 public class icsharp
12 {
13 public icsharp ()
14 {
15 //
16 // todo: Add constructor logic here
17 //
18}
19/** // <summary>
20 // Compression
21 /// </Summary>
22 // <Param name = "Param"> </param>
23 // <returns> </returns>
24 Public String compress (string PARAM)
25 {
26 byte [] DATA = system. Text. encoding. utf8.getbytes (PARAM );
27 // byte [] DATA = convert. frombase64string (PARAM );
28 memorystream MS = new memorystream ();
29 stream = new icsharpcode. sharpziplib. bzip2.bzip2outputstream (MS );
30 try
31 {
32 stream. Write (data, 0, Data. Length );
33}
34 finally
35 {
36 stream. Close ();
37 Ms. Close ();
38}
39 return convert. tobase64string (Ms. toarray ());
40}
41/** // <summary>
42 // decompress
43 // </Summary>
44 // <Param name = "Param"> </param>
45 /// <returns> </returns>
46 Public String decompress (string PARAM)
47 {
48 string commonstring = "";
49 byte [] buffer = convert. frombase64string (PARAM );
50memorystream MS = new memorystream (buffer );
51 stream Sm = new icsharpcode. sharpziplib. bzip2.bzip2inputstream (MS );
52 // specify the format to be read, or garbled characters
53 streamreader reader = new streamreader (SM, system. Text. encoding. utf8 );
54 try
55 {
56 commonstring = reader. readtoend ();
57}
58 finally
59 {
60 SM. Close ();
61 Ms. Close ();
62}
63 return commonstring;
64}
65}

Encoding. utf8 andConvert. frombase64stringEncoding. utf8 attributeGets the encoding in UTF-8 format.
UnicodeEach character in all supported scripts is assigned a code bit (a number ). Unicode conversion format (UTF) is a bitwise encoding method. Unicode standard version 3.2 uses the following UTF:
UTF-8, which represents each bitwise as a sequence of 1 to 4 bytes.
UTF-16, which represents each bitwise as a sequence consisting of 1 to 2 16 integers.
UTF-32, which represents each bitwise as a 32-bit integer.

Convert. frombase64string Method
String(It encodes binary data into base 64 digits) into an equivalent 8-bit unsigned integer array.
Its parameters also have certain requirements:

The parameter isIt consists of 64-digit base, white space, and trailing padding characters. 64-based numbers listed in ascending order starting from scratch are uppercase letters "A" to "Z", lowercase letters "A" to "Z", numbers "0" to "9 "and the symbols "+" and "/". Blank characters include tab, space, carriage return, and line feed.SCan contain any number of white spaces, because all white spaces are ignored. The valueless character "=" is used for trailing blank spaces.SCan contain zero, one, or two filling characters at the end.
Exception:

Exception type Condition
Argumentnullexception SIs a null reference (in Visual BasicNothing).
Formatexception SThe length (ignore blank characters) is less than 4.

-Or-

SIs not an even multiple of 4.

SIs not an even multiple of 4.

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.