字符串GZIP压缩解压

c#

    /// <summary>
    /// 字符串压缩解压
    /// </summary>
    public class Zipper
    {
        public static string CompressString(string str)
        {
            str = HttpUtility.UrlEncode(str);
            var compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str);
            var compressAfterByte = Compress(compressBeforeByte);
            string compressString = Convert.ToBase64String(compressAfterByte);
            return compressString;
        }

        public static string DecompressString(string str)
        {
            var compressBeforeByte = Convert.FromBase64String(str);
            var compressAfterByte = Decompress(compressBeforeByte);
            string compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte);
            return HttpUtility.UrlDecode(compressString);
        }

        /// <summary>
        /// Compress
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private static byte[] Compress(byte[] data)
        {
            try
            {
                var ms = new MemoryStream();
                var zip = new GZipStream(ms, CompressionMode.Compress, true);
                zip.Write(data, 0, data.Length);
                zip.Close();
                var buffer = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(buffer, 0, buffer.Length);
                ms.Close();
                return buffer;

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// Decompress
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private static byte[] Decompress(byte[] data)
        {
            try
            {
                var ms = new MemoryStream(data);
                var zip = new GZipStream(ms, CompressionMode.Decompress, true);
                var msreader = new MemoryStream();
                var buffer = new byte[0x1000];
                while (true)
                {
                    var reader = zip.Read(buffer, 0, buffer.Length);
                    if (reader <= 0)
                    {
                        break;
                    }
                    msreader.Write(buffer, 0, reader);
                }
                zip.Close();
                ms.Close();
                msreader.Position = 0;
                buffer = msreader.ToArray();
                msreader.Close();
                return buffer;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }

js 基于pako.js

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>zip</title>
    <script src="js/jquery.min.js"></script>
    <script src="js/pako.min.js"></script>
    <script type="text/javascript">
        function encode() {
            var str = $('#content').val();
            str = window.btoa(pako.gzip(encodeURIComponent(str), { to: "string" }));
            $('#ciphertext').text(str);
        }
        function decode() {
            var encodedData =  $('#content').val();
            var decodedData = window.atob(encodedData);

            var charData = decodedData.split('').map(function (x) { return x.charCodeAt(0); });
            var binData = new Uint8Array(charData);
            var data = pako.inflate(binData);
            decodedData = String.fromCharCode.apply(null, new Uint16Array(data));

            $('#ciphertext').text(decodeURIComponent(decodedData));
        }
    </script>
</head>
<body>
    <input id="content" type="text">
    <button onclick="encode()">encode</button>
    <button onclick="decode()">decode</button>
    <div id="ciphertext"></div>
</body>
</html>

 

posted @ 2018-08-24 11:25  刘小吉  阅读(932)  评论(0编辑  收藏  举报