C# 解压缩工具类GZip

2021-06-11 09:04

阅读:534

标签:解压缩工具   read   empty   stat   mode   name   代码   using   工具   

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;

namespace 落地页测试代码
{
    public class GZip
    {
        /// 
        /// 压缩
        /// 
        /// 文本
        public static string Compress(string text)
        {
            if (text=="")
                return string.Empty;
            byte[] buffer = Encoding.UTF8.GetBytes(text);
            return Convert.ToBase64String(Compress(buffer));
        }

        /// 
        /// 解压缩
        /// 
        /// 文本
        public static string Decompress(string text)
        {
            if (text=="")
                return string.Empty;
            byte[] buffer = Convert.FromBase64String(text);
            using (var ms = new MemoryStream(buffer))
            {
                using (var zip = new GZipStream(ms, CompressionMode.Decompress))
                {
                    using (var reader = new StreamReader(zip))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
        }

        /// 
        /// 压缩
        /// 
        /// 字节流
        public static byte[] Compress(byte[] buffer)
        {
            if (buffer == null)
                return null;
            using (var ms = new MemoryStream())
            {
                using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
                {
                    zip.Write(buffer, 0, buffer.Length);
                }
                return ms.ToArray();
            }
        }

        /// 
        /// 解压缩
        /// 
        /// 字节流
        public static byte[] Decompress(byte[] buffer)
        {
            if (buffer == null)
                return null;
            return Decompress(new MemoryStream(buffer));
        }

        /// 
        /// 压缩
        /// 
        /// 
        public static byte[] Compress(Stream stream)
        {
            if (stream == null || stream.Length == 0)
                return null;
            return Compress(StreamToBytes(stream));
        }

        /// 
        /// 解压缩
        /// 
        /// 
        public static byte[] Decompress(Stream stream)
        {
            if (stream == null || stream.Length == 0)
                return null;
            using (var zip = new GZipStream(stream, CompressionMode.Decompress))
            {
                using (var reader = new StreamReader(zip))
                {
                    return Encoding.UTF8.GetBytes(reader.ReadToEnd());
                }
            }
        }
        /// 
        /// 流转换为字节流
        /// 
        /// 
        public static byte[] StreamToBytes(Stream stream)
        {
            stream.Seek(0, SeekOrigin.Begin);
            var buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            return buffer;
        }
    }
}

 

C# 解压缩工具类GZip

标签:解压缩工具   read   empty   stat   mode   name   代码   using   工具   

原文地址:https://www.cnblogs.com/bin521/p/10569807.html


评论


亲,登录后才可以留言!