• android与服务端通讯时使用到的GZIP压缩及解压


    为了减小android项目与服务端进行通讯时的数据流量,我们可以使用GZIP对服务端传输的数据进行压缩,在android客户端解压。或在客户端压缩,在服务端解压。代码如下:

    android客户端的GZIP操作类:

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.util.zip.GZIPInputStream;
    import java.util.zip.GZIPOutputStream;
    
    import org.kobjects.base64.Base64;
    
    /**
     * @author Jinqi 2014/1/27
     * GZIP压缩和解压缩的操作类
     *
     */
    public class GzipHelper {
        /** GZIP压缩
         * @param str 要压缩的字符串
         * @return GZIP压缩后的字符串
         * @throws IOException
         */
        public static String compress(String str) throws IOException {
    
            byte[] blockcopy = ByteBuffer
                    .allocate(4)
                    .order(java.nio.ByteOrder.LITTLE_ENDIAN)
                    .putInt(str.length())
                    .array();
            ByteArrayOutputStream os = new ByteArrayOutputStream(str.length());
            GZIPOutputStream gos = new GZIPOutputStream(os);
            gos.write(str.getBytes());
            gos.close();
            os.close();
            byte[] compressed = new byte[4 + os.toByteArray().length];
            System.arraycopy(blockcopy, 0, compressed, 0, 4);
            System.arraycopy(os.toByteArray(), 0, compressed, 4,
                    os.toByteArray().length);
            return Base64.encode(compressed);
    
        }
    
        /** GZIP解压缩
         * @param zipText GZIP压缩过的字符串
         * @return 解压后的字符串
         * @throws IOException
         */
        public static String decompress(String zipText) throws IOException {
            byte[] compressed = Base64.decode(zipText);
            if (compressed.length > 4)
            {
                GZIPInputStream gzipInputStream = new GZIPInputStream(
                        new ByteArrayInputStream(compressed, 4,
                                compressed.length - 4));
    
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                for (int value = 0; value != -1;) {
                    value = gzipInputStream.read();
                    if (value != -1) {
                        baos.write(value);
                    }
                }
                gzipInputStream.close();
                baos.close();
                String sReturn = new String(baos.toByteArray(), "UTF-8");
                return sReturn;
            }
            else
            {
                return "";
            }
        }
    }

    .net服务端的GZIP操作类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Text;
    using System.IO;
    using System.IO.Compression;
    
    namespace MobileServ
    {
        public class ZipHelper
        {
            public static string compress(string text)
            {
                byte[] buffer = Encoding.UTF8.GetBytes(text);
                MemoryStream ms = new MemoryStream();
                using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
                {
                    zip.Write(buffer, 0, buffer.Length);
                }
    
                ms.Position = 0;
                MemoryStream outStream = new MemoryStream();
    
                byte[] compressed = new byte[ms.Length];
                ms.Read(compressed, 0, compressed.Length);
    
                byte[] gzBuffer = new byte[compressed.Length + 4];
                System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
                System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
                return Convert.ToBase64String(gzBuffer);
            }
    
            public static string decompress(string compressedText)
            {
                byte[] gzBuffer = Convert.FromBase64String(compressedText);
                using (MemoryStream ms = new MemoryStream())
                {
                    int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                    ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
    
                    byte[] buffer = new byte[msgLength];
    
                    ms.Position = 0;
                    using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
                    {
                        zip.Read(buffer, 0, buffer.Length);
                    }
    
                    return Encoding.UTF8.GetString(buffer);
                }
            }
        }
    }

    通过GIZP压缩及解压,可以将传输的数据压缩1/4左右,效果非常明显。

  • 相关阅读:
    python 使用pyinstaller生成exe,以及编译报错:编译时报错如下:No module named timedeltas not build. If you want import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to
    Remote desktop manager 如何导入.db配置文件
    C# string怎么转换成泛型T?
    C# 如何在ComboBox输入文字改变时,触发事件?
    C# 检查panel所有的checkbox 是否被选中
    C# bool? 的意思
    WPF: Accessing Databases with Windows Presentation Foundation / WPF链接数据库
    WPF 03
    WPF MVVC 基础
    使用 Topshelf 创建 Windows 服务
  • 原文地址:https://www.cnblogs.com/jinqi79731/p/3534640.html
Copyright © 2020-2023  润新知