Compression Helper Class using SharpZipLib

Source: Internet
Author: User
Tags arrays empty int size zip

An auxiliary class that uses sharpziplib to compress, simplifying the operation of compressed byte arrays and strings.








Using System;



Using System.Text;



Using System.IO;



Using ICSharpCode.SharpZipLib.BZip2;



Using ICSharpCode.SharpZipLib.GZip;



Using ICSharpCode.SharpZipLib.Zip;







Namespace Compression



{



<summary>



Compression method.



</summary>



public enum Compressiontype



{



Gzip



BZIP2,



Zip



}







<summary>



An auxiliary class that uses sharpziplib to compress, simplifying the operation of compressed byte arrays and strings.



</summary>



public class Compressionhelper



{



<summary>



Compress the provider, and the default is GZip.



</summary>



public static Compressiontype Compressionprovider = Compressiontype.gzip;







#region Public methods







<summary>



Generates a compressed byte array from the original byte array.



</summary>



<param name= "bytestocompress" > Original byte array. </param>



<returns> returning a compressed byte array </returns>



public static byte[] Compress (byte[] bytestocompress)



{



MemoryStream ms = new MemoryStream ();



Stream s = outputstream (ms);



S.write (bytestocompress, 0, bytestocompress.length);



S.close ();



Return Ms. ToArray ();



}







<summary>



Generates a compressed string from the original string.



</summary>



<param name= "Stringtocompress" > Original string. </param>



<returns> returns the compressed string. </returns>



public static string Compress (String stringtocompress)



{



byte[] Compresseddata = Compresstobyte (stringtocompress);



String strout = Convert.tobase64string (compresseddata);



return strout;



}







<summary>



Generates a compressed byte array from the original string.



</summary>



<param name= "Stringtocompress" > Original string. </param>



<returns> returns a compressed array of bytes. </returns>



public static byte[] Compresstobyte (string stringtocompress)



{



byte[] Bytdata = Encoding.Unicode.GetBytes (stringtocompress);



Return Compress (Bytdata);



}







<summary>



Generates the original string from a compressed string.



</summary>



<param name= "stringtodecompress" > Compressed string. </param>



<returns> returns the original string. </returns>



public string Decompress (string stringtodecompress)



{



String outstring = String. Empty;



if (stringtodecompress = null)



{



throw new ArgumentNullException ("Stringtodecompress", "You tried to use a empty string");



}







Try



{



byte[] Inarr = convert.frombase64string (Stringtodecompress.trim ());



outstring = Encoding.Unicode.GetString (Decompress (Inarr));



}



catch (NullReferenceException nEx)



{



return nex.message;



}







return outstring;



}







<summary>



Generates a raw byte array from a compressed byte array.



</summary>



<param name= "bytestodecompress" > Compressed byte array. </param>



<returns> returns the original byte array. </returns>



public static byte[] Decompress (byte[] bytestodecompress)



{



byte[] WriteData = new byte[4096];



Stream s2 = inputstream (new MemoryStream (bytestodecompress));



MemoryStream outstream = new MemoryStream ();







while (true)



{



int size = s2. Read (writedata, 0, writedata.length);



if (Size > 0)



{



Outstream.write (writedata, 0, size);



}



Else



{



Break



}



}



S2. Close ();



byte[] Outarr = Outstream.toarray ();



Outstream.close ();



return Outarr;



}







#endregion







#region Private Methods







<summary>



Generates a compressed output stream from a given stream.



</summary>



<param name= "InputStream" > Original stream. </param>



<returns> returns the compressed output stream. </returns>



private static stream OutputStream (stream inputstream)



{



Switch (Compressionprovider)



{



Case COMPRESSIONTYPE.BZIP2:



return new Bzip2outputstream (InputStream);







Case Compressiontype.gzip:



return new Gzipoutputstream (InputStream);







Case Compressiontype.zip:



return new Zipoutputstream (InputStream);







Default



return new Gzipoutputstream (InputStream);



}



}







<summary>



Generates a compressed input stream from a given stream.



</summary>



<param name= "InputStream" > Original stream. </param>



<returns> returns the compressed input stream. </returns>



private static stream InputStream (stream inputstream)



{



Switch (Compressionprovider)



{



Case COMPRESSIONTYPE.BZIP2:



return new Bzip2inputstream (InputStream);







Case Compressiontype.gzip:



return new Gzipinputstream (InputStream);







Case Compressiontype.zip:



return new Zipinputstream (InputStream);







Default



return new Gzipinputstream (InputStream);



}



}







#endregion



}



}



Source: http://www.mostlylucid.co.uk/archive/2004/04/06/958.aspx


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.