欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net压缩功能实现方法

程序员文章站 2024-02-24 14:27:16
复制代码 代码如下:public static class compressor    {    &n...
复制代码 代码如下:

public static class compressor    {

            public static byte[] compress(byte[] data)
            {
                using (memorystream output = new memorystream())
                {
                    using (gzipstream gzip = new gzipstream(output, compressionmode.compress, true))
                    {
                        gzip.write(data, 0, data.length);
                        gzip.close();
                        return output.toarray();
                    }
                }
            }

            public static byte[] decompress(byte[] data)
            {
                using (memorystream input = new memorystream())
                {
                    input.write(data, 0, data.length);
                    input.position = 0;
                    using (gzipstream gzip = new gzipstream(input, compressionmode.decompress, true))
                    {
                        using (memorystream output = new memorystream())
                        {
                            byte[] buff = new byte[64];
                            int read = -1;
                            read = gzip.read(buff, 0, buff.length);
                            while (read > 0)
                            {
                                output.write(buff, 0, read);
                                read = gzip.read(buff, 0, buff.length);
                            }
                            gzip.close();
                            return output.toarray();
                        }
                    }
                }
            }