The gzip compression string is used to reduce the server load. This function in the server compresses the webpage content and transmits it to the visiting Computer Browser for display. generally, the plain text content can be compressed to 40% of the original size. in this way, the transmission is faster, and the result is that you click the URL and it will be displayed soon. of course, this will also increase the server load.
Static string compressstring (string input)
{
String retvalue = string. empty;
If (! String. isnullorempty (input ))
{
Byte [] bytesource = encoding. utf8.getbytes (input );
Memorystream msm = new memorystream ();
Using (gzips tutorial tream gzs = new gzipstream (msm, compressionmode. compress, true ))
{
Gzs. write (bytesource, 0, bytesource. length );
}
Msm. position = 0;
Byte [] compbytes = new byte [msm. length];
Msm. read (compbytes, 0, compbytes. length );
Msm. close ();
Byte [] finalbuffer = new byte [compbytes. length + 4];
Buffer. blockcopy (compbytes, 0, finalbuffer, 4, compbytes. length );
Buffer. blockcopy (bitconverter. getbytes (bytesource. length), 0, finalbuffer, 0, 4 );
Retvalue = system. convert. tobase64string (finalbuffer );
}
Return retvalue;
}
Static string decompressstring (string input)
{
String retvalue = string. empty;
If (! String. isnullorempty (input ))
{
Byte [] source = system. convert. frombase64string (input );
Using (memorystream msm = new memorystream ())
{
Int length = bitconverter. toint32 (source, 0 );
Msm. write (source, 4, source. length-4 );
// Console. writeline (encoding. utf8.getstring (source ));
Msm. position = 0;
Byte [] decmpbytes = new byte [length];
Using (gzipstream gzs = new gzipstream (msm, compressionmode. decompress ))
{
Gzs. read (decmpbytes, 0, length );
// Gzs. copyto ();
}
Retvalue = encoding. utf8.getstring (decmpbytes );
}
}
Return retvalue;
}
Static string compressstring (string input)
{
String retvalue = string. empty;
If (! String. isnullorempty (input ))
{
Byte [] bytesource = encoding. utf8.getbytes (input );
Memorystream msm = new memorystream ();
Using (gzipstream gzs = new gzipstream (msm, compressionmode. compress, true ))
{
Gzs. write (bytesource, 0, bytesource. length );
}
Msm. position = 0;
Byte [] compbytes = new byte [msm. length];
Msm. read (compbytes, 0, compbytes. length );
Msm. close ();
Byte [] finalbuffer = new byte [compbytes. length + 4];
Buffer. blockcopy (compbytes, 0, finalbuffer, 4, compbytes. length );
Buffer. blockcopy (bitconverter. getbytes (bytesource. length), 0, finalbuffer, 0, 4 );
Retvalue = system. convert. tobase64string (finalbuffer );
}
Return retvalue;
}
Static string decompressstring (string input)
{
String retvalue = string. empty;
If (! String. isnullorempty (input ))
{
Byte [] source = system. convert. frombase64string (input );
Using (memorystream msm = new memorystream ())
{
Int length = bitconverter. toint32 (source, 0 );
Msm. write (source, 4, source. length-4 );
// Console. writeline (encoding. utf8.getstring (source ));
Msm. position = 0;
Byte [] decmpbytes = new byte [length];
Using (gzipstream gzs = new gzipstream (msm, compressionmode. decompress ))
{
Gzs. read (decmpbytes, 0, length );
// Gzs. copyto ();
}
Retvalue = encoding. utf8.getstring (decmpbytes );
}
}
Return retvalue;
}
There are two obvious advantages to reduce the file size. One is to reduce the storage space, and the other is to reduce the transmission time when the file is transmitted over the network. Gzip is a frequently used command in linux to compress and decompress files, which is convenient and easy to use. Syntax: gzip [Option] compressed (decompressed) file name the meaning of each option of this command is as follows:-c writes the output to the standard output and retains the original file. -D. decompress the compressed file. -L the following fields are displayed for each compressed file: size of the compressed file, size of the uncompressed file, and compression ratio; name of the uncompressed file-r recursively searches for the specified directory and compresses all the files or decompress them. -T test to check whether the compressed file is complete. -V displays the file name and compression ratio for each compressed and decompressed file. -Num uses the specified numeric num to adjust the compression speed.-1 or -- fast indicates the fastest compression method (low compression ratio), and-9 or -- best indicates the slowest compression method (high compression ratio ). The default value is 6. Command instance: gzip * % compresses each file in the current directory into A. gz file. Gzip-dv * % decompress each compressed file in the current directory and list detailed information. Gzip-l * % detailed information of each compressed file in Example 1 is displayed without decompression. Gzip usr.tar %: the compressed tar backup file usr.tar. the extension name of the compressed file is .tar.gz.