/* ==============================================================================
 * 功能描述：ZipCompressHelp  
 * 创 建 者：徐高庆
 * 创建日期：2022/10/18 16:26:09
 * CLR Version :4.0.30319.42000
 * ==============================================================================*/
using System.IO;
using System.IO.Compression;

namespace MrWu.IO
{
    /// <summary>
    /// ZipCompressHelp
    /// </summary>
    public class ZipCompressHelp
    {
        
        /// <summary>
        /// 压缩流
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] CompressByte(byte[] data)
        {
            if (data == null || data.Length <= 0) return null;
            using (MemoryStream im = new MemoryStream(data))
            {
                using (MemoryStream om = new MemoryStream())
                {
                    using (DeflateStream com = new DeflateStream(om, CompressionMode.Compress))
                    {
                        im.CopyTo(com);
                    }
                    return om.ToArray();
                }
            }
        }

        /// <summary>
        /// 解压流
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] DecompressByte(byte[] data)
        {
            if (data == null || data.Length <= 0) return null;
            using (var im = new MemoryStream(data))
            {
                using (var om = new MemoryStream())
                {
                    using (var dcom = new DeflateStream(im, CompressionMode.Decompress))
                    {
                        dcom.CopyTo(om);
                    }
                    return om.ToArray();
                }
            }
        }


        /// <summary>
        /// GZip压缩方式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] GZipByte(byte[] str)
        {
            if (str == null) return null;           
                using (var output = new MemoryStream())
                {
                    using (
                        var compressor = new Ionic.Zlib.GZipStream(output, Ionic.Zlib.CompressionMode.Compress,
                            Ionic.Zlib.CompressionLevel.Default))
                    {
                        compressor.Write(str, 0, str.Length);
                    }
                    return output.ToArray();
                }
        }

        /// <summary>
        /// 解压
        /// </summary>
        public static byte[] GDexZipByte(byte[] str)
        {
            if (str == null) return null;
          
                using (var xx = new MemoryStream(str))
                {
                    using (var de = new Ionic.Zlib.GZipStream(xx, Ionic.Zlib.CompressionMode.Decompress,
                            Ionic.Zlib.CompressionLevel.Default))
                    {
                        using (var output = new MemoryStream())
                        {
                            CopyTo(de, output);
                            return output.ToArray();
                        }
                    }
                }
          
        }

        static void CopyTo(Stream im, Stream om)
        {
            if (im == null || om == null) return;
            byte[] buffer = new byte[1024 * 1024];
            int r;
            while ((r = im.Read(buffer, 0, buffer.Length)) > 0)
            {
                om.Write(buffer, 0, r);
            }
        }
    }
}
