C # GZip compression/Decompression
Sometimes we need to compress data, such as video/audio/content, and so on. Many of them are used in socket programming. However, Microsoft provides us with a class GZIP for decompression. However, this is something that was launched many years ago. for example, RAR in the source code is an enhancement of its algorithm modification. However, I don't have this algorithm. If you want to study it, you can search for it on Github. I provide two compression methods here, but what I like most is MyZip.
public static byte[] GZipCompress(byte[] bytes) { using (MemoryStream ms = new MemoryStream()) { using (GZipStream zs = new GZipStream(ms, CompressionMode.Compress)) zs.Write(bytes, 0, bytes.Length); return ms.ToArray(); } }
The compression above is just a simple form first you need to create or provide a valid stream and then new GZipStream (MS, CompressionMode. compress) Is it still quite simple? ms needs to store the compressed file stream and then write the bytes you need to Compress to GZipStream and finally put them in a Dispose
public static byte[] GZipDecompress(byte[] bytes) { using (MemoryStream ms = new MemoryStream(bytes)) { using (GZipStream zs = new GZipStream(ms, CompressionMode.Decompress)) { byte[] buffer = new byte[512]; MemoryStream buf = new MemoryStream(); for (int offset; (offset = zs.Read(buffer, 0, 512)) > 0;) buf.Write(buffer, 0, offset); return buf.ToArray(); } } }
You can use GZipStream to decompress part 4.0 above. copyTo function without having to solve the problem above some of the above unzipping needs to provide a stream data that needs to be decompressed and then we are in the new GZipStream (MS, CompressionMode. decompress) however, we can start to read the data in the decompressed stream if there are different amounts of data provided. However, there is no way to determine the length of the decompressed stream. Some people add an additional part of the stream to the compressed stream for storing the original length. but I don't think I can use it.
Public byte [] GZipCompress (byte [] bytes) {int hContext = 0; // body handle int input_used = 0; // uncompressed size int output_used = 0; // size after compression InitCompression (); // initialize the compressed CreateCompression (ref hContext, GZIP_LVL); // create the compressed body byte [] buffer = new byte [512]; // compression buffer if (Compress (hContext, bytes, bytes. length, buffer, 512, ref input_used, ref output_used, GZIP_LVL )! = 0) Console. WriteLine (error); // compression failure DestroyCompression (hContext); // destroy the body object return buffer ;}
The above is through GZip. in fact, the Compress part of the dll compression code is generally to Compress bytes cyclically, but it is simple to give you a usage so it is not necessary to do so professionally but GZip. dll, I remember, is integrated in Win8 by default. After all, it is Microsoft.
Public byte [] GZipDecompress (byte [] bytes) {int hContext = 0; // body handle int input_used = 0; // input size int output_used = 0; // output size: InitCompression (); // initialize and decompress CreateCompression (ref hContext, GZIP_LVL); // create and decompress the body byte [] buffer = new byte [512]; // extract the buffer if (Decompress (hContext, bytes, bytes. length, buffer, 512, ref input_used, ref output_used )! = 0) Console. WriteLine (error); // decompress failed DestroyCompression (hContext); // destroy the body object return buffer ;}
However, it seems that I still think that using GZipStream to use more user-friendly APIS is only used in the C ++ or E language. Otherwise, there is no need for me to write a exploitation. GZip. dll to decompress the file.
??