. NET 2.0 compression function

Source: Internet
Author: User
In. NET 1.1, we need to implement the compression function. Generally, we use the sharpziplib of open source or call the J # class library.
Now the compression function is added to. NET 2.0, and The namespace is using system. Io. compression;

The following is an example:

Compressed string
Public   Static   String Zipstring ( String Uncompressedstring)
{

Byte [] Bytdata = System. Text. encoding. utf8.getbytes (uncompressedstring );
Memorystream MS =   New Memorystream ();
Stream s =   New Gzipstream (MS, compressionmode. Compress );
S. Write (bytdata, 0 , Bytdata. Length );
S. Close ();
Byte [] Compresseddata = ( Byte []) Ms. toarray ();
Return System. Convert. tobase64string (compresseddata, 0 , Compresseddata. Length );
}

Extract string
 

Public   Static   String Unzipstring ( String Uncompressedstring)
{
System. Text. stringbuilder uncompressedstring =   New System. Text. stringbuilder ();
Byte [] Writedata =   New   Byte [ 4096 ];

Byte [] Bytdata = System. Convert. frombase64string (uncompressedstring );
Int Totallength =   0 ;
Int Size =   0 ;

Stream s =   New Gzipstream ( New Memorystream (bytdata), compressionmode. Decompress );
While ( True )
{
Size = S. Read (writedata, 0 , Writedata. Length );
If (Size >   0 )
{
Totallength+ =Size;
Uncompressedstring. append (system. Text. encoding. utf8.getstring (writedata,0, Size ));
}
Else
{
Break;
}
}
S. Close ();
Return Uncompressedstring. tostring ();
}

Compressed file

  Public   Static   Bool Addzip ( String Srcfilename, String Zipfilename)
{
If ( ! File. exists (srcfilename ))
Return   False ;
Bool Result;
Filestream FS =   Null , Output =   Null ;
Gzipstream zipstream =   Null ;
Try
{
FS =   New Filestream (srcfilename, filemode. Open, fileaccess. Read );
Byte [] Buffer =   New   Byte [Fs. Length];
FS. Read (buffer, 0 , Buffer. Length );
FS. Close ();
If ( ! File. exists (zipfilename ))
{
Output = File. Create (zipfilename );
Zipstream =   New Gzipstream (output, compressionmode. Compress );
Zipstream. Write (buffer, 0 , Buffer. Length );
Result =   True ;
}
Else
{
Result= False;
}
}
Catch (Exception)
{
Result= False;
}
Finally
{
If (Zipstream ! =   Null )
{
Zipstream. Flush ();
Zipstream. Close ();
}
}
Return Result;
}

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.