当前位置: 移动技术网 > IT编程>开发语言>.net > .net压缩功能实现方法

.net压缩功能实现方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

应际蕾新浪微博,莫囧网,杏坛门

复制代码 代码如下:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网