一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

gzip压缩字符串

时间:2011-01-13 编辑:简简单单 来源:一聚教程网

gzip压缩字符串 的使用是减轻服务器负载,服务器中的这个功能就将网页内容压缩后传输到来访的电脑浏览器中显示出来.一般对纯文本内容可压缩到原大小的40%.这样传输就快了,效果就是你点击网址后会很快的显示出来.当然这也会增加服务器的负载.

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

减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间。gzip 是在 linux 系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用。   语法:gzip [选项] 压缩(解压缩)的文件名   该命令的各选项含义如下:   -c 将输出写到标准输出上,并保留原有文件。   -d 将压缩文件解压。   -l 对每个压缩文件,显示下列字段:   压缩文件的大小;未压缩文件的大小;压缩比;未压缩文件的名字   -r 递归式地查找指定目录并压缩其中的所有文件或者是解压缩。   -t 测试,检查压缩文件是否完整。   -v 对每一个压缩和解压的文件,显示文件名和压缩比。   -num 用指定的数字 num 调整压缩的速度,-1 或 --fast 表示最快压缩方法(低压缩比),   -9 或--best表示最慢压缩方法(高压缩比)。系统缺省值为 6。   指令实例:   gzip *   % 把当前目录下的每个文件压缩成 .gz 文件。   gzip -dv *   % 把当前目录下每个压缩的文件解压,并列出详细的信息。   gzip -l *   % 详细显示例1中每个压缩的文件的信息,并不解压。   gzip usr.tar   % 压缩 tar 备份文件 usr.tar,此时压缩文件的扩展名为.tar.gz。

热门栏目