using System;
using System.Data;
using System.IO;
using System.IO.Compression;

namespace TsSFCDeviceSvc.App_Code
{
    public class DataCompressHelper
    {
        #region 压缩

        /// <summary>
        /// 压缩DataSet
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static byte[] AddDataSet(System.Data.DataSet ds)
        {
            if (ds == null || ds.Tables.Count == 0)
            {
                return new byte[] { };
            }
            try
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                ds.WriteXml(ms, System.Data.XmlWriteMode.WriteSchema);

                ms.Seek(0, System.IO.SeekOrigin.Begin);
                byte[] kao = new byte[ms.Length];
                ms.Read(kao, 0, kao.Length);
                ms.Close();
                return AddZip(kao);
            }
            catch
            {
                return new byte[] { };
            }
        }

        public static byte[] AddZip(byte[] needAdd)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            try
            {
                ICSharpCode.SharpZipLib.GZip.GZipOutputStream zipOut =
                    new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(ms);

                zipOut.Write(needAdd, 0, needAdd.Length);
                zipOut.Finish();
                zipOut.Close();

                return ms.ToArray();
            }
            finally
            {
                ms.Close();
            }
        }

        #endregion

        #region 解压缩

        /// <summary>
        /// 解压数据返回DataSet
        /// </summary>
        /// <param name="addDs"></param>
        /// <returns></returns>
        public static System.Data.DataSet ExactDataSet(byte[] addDs)
        {
            if (addDs == null || addDs.Length == 0)
            {
                return new DataSet();
            }

            byte[] kao = ExactZip(addDs);
            System.Data.DataSet ds = new System.Data.DataSet();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            ms.Write(kao, 0, kao.Length);
            ms.Seek(0, System.IO.SeekOrigin.Begin);
            System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(ms);
            ds.ReadXml(reader, System.Data.XmlReadMode.Auto);
            ms.Close();
            return ds;
        }

        public static byte[] ExactZip(byte[] needExact)
        {
            System.IO.MemoryStream ret = new System.IO.MemoryStream(needExact);
            System.IO.MemoryStream ret1 = new System.IO.MemoryStream();

            ICSharpCode.SharpZipLib.GZip.GZipInputStream zipIn = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(ret);

            byte[] buffer = new byte[2048];
            int size = 2048;
            while (true)
            {
                size = zipIn.Read(buffer, 0, buffer.Length);
                if (size > 0)
                {
                    ret1.Write(buffer, 0, size);
                }
                else
                {
                    break;
                }
            }
            return ret1.ToArray();
        }

        #endregion
    }

    /// <summary>
    /// JSON字符串压缩
    /// </summary>
    public class GZipHelper
    {
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static DataSet GetDatasetByString(string Value)
        {
            DataSet ds = new DataSet();
            string CC = GZipDecompressString(Value);
            System.IO.StringReader Sr = new StringReader(CC);
            ds.ReadXml(Sr);
            return ds;
        }
        /// <summary>
        /// 根据DATASET压缩字符串
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static string GetStringByDataset(string ds)
        {
            return GZipCompressString(ds);
        }
        /// <summary>
        /// 将传入字符串以GZip算法压缩后，返回Base64编码字符
        /// </summary>
        /// <param name="rawString">需要压缩的字符串</param>
        /// <returns>压缩后的Base64编码的字符串</returns>
        public static string GZipCompressString(string rawString)
        {
            if (string.IsNullOrEmpty(rawString) || rawString.Length == 0)
            {
                return "";
            }
            else
            {
                byte[] rawData = System.Text.Encoding.UTF8.GetBytes(rawString.ToString());
                byte[] zippedData = Compress(rawData);
                return Convert.ToBase64String(zippedData);
            }
        }

        /// <summary>
        /// GZip压缩
        /// </summary>
        /// <param name="rawData">待压缩的字节数组</param>
        /// <returns>返回压缩后的字节数组</returns>
        public static byte[] Compress(byte[] rawData)
        {
            MemoryStream ms = new MemoryStream();

            #region ICSharpCode ZIP 方法
            /*
            ICSharpCode.SharpZipLib.Zip.ZipOutputStream gz = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(ms);

            var zzp = new ICSharpCode.SharpZipLib.Zip.ZipEntry("1");

            gz.PutNextEntry(zzp);

            gz.Write(rawData, 0, rawData.Length);

            gz.CloseEntry();

            gz.Close();
            */
            #endregion

            #region ICSharpCode GZIP 方法
            ICSharpCode.SharpZipLib.GZip.GZipOutputStream gz = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(ms);

            gz.Write(rawData, 0, rawData.Length);

            gz.Close();

            #endregion

            #region Microsoft原生GZip压缩
            /*
            GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);

            compressedzipStream.Write(rawData, 0, rawData.Length);
            compressedzipStream.Close();
            */
            #endregion

            return ms.ToArray();
        }
        /// <summary>
        /// 将传入的二进制字符串资料以GZip算法解压缩
        /// </summary>
        /// <param name="zippedString">经GZip压缩后的二进制字符串</param>
        /// <returns>原始未压缩字符串</returns>
        public static string GZipDecompressString(string zippedString)
        {
            if (string.IsNullOrEmpty(zippedString) || zippedString.Length == 0)
            {
                return "";
            }
            else
            {
                byte[] zippedData = Convert.FromBase64String(zippedString.ToString());
                return (string)(System.Text.Encoding.UTF8.GetString(Decompress(zippedData)));
            }
        }
        /// <summary>
        /// ZIP解压
        /// </summary>
        /// <param name="zippedData"></param>
        /// <returns></returns>
        public static byte[] Decompress(byte[] zippedData)
        {
            MemoryStream ms = new MemoryStream(zippedData);
            GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress);
            MemoryStream outBuffer = new MemoryStream();
            byte[] block = new byte[1024];
            while (true)
            {
                int bytesRead = compressedzipStream.Read(block, 0, block.Length);
                if (bytesRead <= 0)
                    break;
                else
                    outBuffer.Write(block, 0, bytesRead);
            }
            compressedzipStream.Close();
            return outBuffer.ToArray();
        }
    }
}