首页 > 编程 > .NET > 正文

.net实现压缩功能的方法

2024-07-10 12:54:10
字体:
来源:转载
供稿:网友

 关于.net压缩功能有很多的小伙伴们不知道该如何去实现,那么现在本文就将为大家介绍.net实现压缩功能的方法,其实.net压缩功能没有我们想象中的那么复杂,下面我们就一起去参考参考吧。

复制代码 代码如下:

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

看完本文关于.net实现压缩功能的方法后你是不是明白了呢?想了解更多的ASP.NET知识就请关注我们错新技术频道吧! 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表