字符串GZIP压缩解压

Posted liuxiaoji

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串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>

 

以上是关于字符串GZIP压缩解压的主要内容,如果未能解决你的问题,请参考以下文章

java中使用GZIP对String进行压缩和解压

Gzip压缩与解压

字符串GZIP压缩解压

对数据进行GZIP压缩和解压

解压gzip字符串golang

接口实现后台GZIP压缩,pako.js 前端解压