Gzip Compressed string

Source: Internet
Author: User
Tags gz file

The

Gzip compressed string is used to reduce server load, and this feature in the server will compress the content of the Web page and transmit it to a visiting computer browser. Generally, the plain text content can be compressed to 40% of the original size. So the transmission is fast, The effect is that you click on the URL will be quickly displayed. Of course, this also increases the load on the server.

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 (source) (encoding.utf8.getstring);

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 (source) (encoding.utf8.getstring);

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;
}

The

Reduce file size has two obvious benefits, one is to reduce storage space, and the second is the transmission of files over the network, you can reduce the transmission time.   Gzip is a regular use of Linux in the file compression and decompression of the command, both convenient and useful.   Syntax: gzip (uncompressed) file name the options for this command have the following meanings:-c writes the output to the standard output and retains the original file.   -D Unzip the compressed file.   -L Displays the following fields for each compressed file: The size of the compressed file, the size of the uncompressed file, the compression ratio, the name of the uncompressed file-R recursively finds the specified directory and compresses all of its files or is uncompressed.   -T test to check that the compressed file is complete.   -V displays filename and compression ratio for each compressed and decompressed file. -num adjusts the speed of the compression with the specified number num,-1 or--fast represents the fastest compression method (low compression ratio),-9 or--best represents the slowest compression method (high compression ratio).   The default value for the system is 6.   Instruction instance: gzip *% compresses each file in the current directory into a. gz file.   GZIP-DV *% extracts each compressed file in the current directory and lists detailed information.   Gzip-l *% details the information of each compressed file in Example 1 and does not understand the pressure. Gzip Usr.tar% Compressed tar backup file Usr.tar, at which time the compressed file extension is. tar.gz.

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.