C # file stream compression and decompression

Source: Internet
Author: User
Tags deflater

C # file stream compression and decompression

////// Decompress the file stream ///Public class ZipHelper {public static int BEST_COMPRESSION = 9; public static int BEST_SPEED = 1; public static int DEFAULT_COMPRESSION =-1; public static int NO_COMPRESSION = 0; # region Deflate compression ////// Deflate compression (the highest compression level by default )/////////
 Public static Stream Deflate (Stream stream) {return ZipHelper. Deflate (Stream, ZipHelper. DEFAULT_COMPRESSION );}////// Deflate compression /////////Compression Quality level (0 ~ 9 )///
 Public static Stream Deflate (Stream stream, int level) {byte [] array = ZipHelper. streamToBytes (stream); byte [] array2 = new byte [array. length]; Deflater deflater = new Deflater (); deflater. setLevel (level); deflater. setStrategy (DeflateStrategy. default); deflater. setInput (array); deflater. finish (); int num = deflater. deflate (array2); byte [] array3 = new byte [num]; Array. copy (array2, array3, num); return ZipHelper. bytesToStream (array3 );}////// Deflate compression /////////Compression Quality level (0 ~ 9 )///
 Public static byte [] Deflate (byte [] input, int level) {byte [] result; try {if (input = null & input. length = 0) {result = new byte [0];} else {byte [] array = new byte [input. length]; Deflater deflater = new Deflater (); deflater. setLevel (level); deflater. setStrategy (DeflateStrategy. default); deflater. setInput (input); deflater. finish (); int num = deflater. deflate (array); byte [] array2 = new byt E [num]; Array. Copy (array, array2, num); result = array2 ;}} catch (Exception innerException) {throw new Exception (compression program error !, InnerException);} return result ;}# endregion # region Inflate unzip ////// Inflate unzip the package /////////
 Public static byte [] Inflate (byte [] input) {byte [] result; try {if (input = null & input. length = 0) {result = new byte [0];} else {Inflater inflater = new Inflater (); inflater. setInput (input); byte [] array = new byte [1024]; using (MemoryStream memoryStream = new MemoryStream () {for (int I = inflater. inflate (array, 0, array. length); I> 0; I = inflater. inflate (array, 0, array. length) {me MoryStream. write (array, 0, I);} byte [] buffer = memoryStream. getBuffer (); memoryStream. close (); result = buffer ;}} catch (Exception innerException) {throw new Exception (decompression program error !, InnerException);} return result ;}////// Inflate unzip the package /////////
 Public static Stream Inflate (Stream zipStream) {byte [] input = ZipHelper. streamToBytes (zipStream); byte [] bytes = ZipHelper. inflate (input); return ZipHelper. bytesToStream (bytes);} # endregion # region GZip compression ////// GZip compression /////////Public static void GZipCompress (Stream srcStream, Stream output) {ZipHelper. GZipCompress (srcStream, 6, output );}////// GZip compression /////////Compression Quality level (0 ~ 9 )///Public static void GZipCompress (Stream srcStream, int compressLevel, Stream output) {if (compressLevel <1 | compressLevel> 9) {throw new Exception (string. format (the compression level {0} You specified is not in the valid range (1-9), compressLevel);} srcStream. position = 0L; GZipOutputStream gZipOutputStream = new GZipOutputStream (output); gZipOutputStream. setLevel (compressLevel); try {int I = 4096; byte [] buffer = new byte [I]; while (I> 0) {I = srcStream. read (buffer, 0, I); gZipOutputStream. write (buffer, 0, I) ;}} catch (Exception ex) {throw new Exception (GZip compression error: + ex. message);} srcStream. close (); gZipOutputStream. finish ();}////// Decompress GZip /////////Public static void GZipDeCompress (Stream zipStream, Stream outputStream) {GZipInputStream gZipInputStream = new GZipInputStream (zipStream); try {int I = 4096; byte [] buffer = new byte [I]; while (I> 0) {I = gZipInputStream. read (buffer, 0, I); outputStream. write (buffer, 0, I) ;}} catch (Exception ex) {throw new Exception (GZip decompression error: + ex. message);} zipStream. close (); gZipInputStream. close () ;}# endregion # region BZip2 compression ////// BZip2 compression ////////////Public static void BZip2Compress (Stream inStream, Stream outStream, int blockSize) {BZip2.Compress (inStream, outStream, blockSize );}////// Decompress BZip2 /////////Public static void BZip2Decompress (Stream inStream, Stream outStream) {BZip2.Decompress (inStream, outStream) ;}# endregion private static byte [] StreamToBytes (Stream stream) {byte [] array = new byte [stream. length]; stream. seek (0L, SeekOrigin. begin); stream. read (array, 0, array. length); stream. close (); return array;} private static Stream BytesToStream (byte [] bytes) {return new MemoryStream (bytes);} private static void StreamToFile (Stream stream, string fileName) {byte [] array = new byte [stream. length]; stream. read (array, 0, array. length); stream. seek (0L, SeekOrigin. begin); FileStream fileStream = new FileStream (fileName, FileMode. create); BinaryWriter binaryWriter = new BinaryWriter (fileStream); binaryWriter. write (array); binaryWriter. close (); fileStream. close ();} private static Stream FileToStream (string fileName) {FileStream fileStream = new FileStream (fileName, FileMode. open, FileAccess. read, FileShare. read); byte [] array = new byte [fileStream. length]; fileStream. read (array, 0, array. length); fileStream. close (); return new MemoryStream (array );}}

 

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.