android数据压缩与解压缩

在数据传输的过程中如果能够对数据进行压缩处理后,然后再与数据库进行传输处理能节省大量的数据,而且能加快传输的速度!效果还是挺明显的:

 

测试结果:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [{"name":"张123三","age":"12","sex":"男","info":".....  
  2. 压缩前:2507  
  3. 压缩后:170  
  4. 解压缩后:2507  
  5. 解压缩后数据:[{"name":"张123三","age":"12","sex":"男","info":"....  


 

 压缩代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <pre name="code" class="java">package com.xiaoyi.utils.compress;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.util.zip.GZIPInputStream;  
  8. import java.util.zip.GZIPOutputStream;  
  9.   
  10. public class Compress {  
  11.     private static final int BUFFER_LENGTH = 400;  
  12.       
  13.       
  14.     //压缩字节最小长度,小于这个长度的字节数组不适合压缩,压缩完会更大  
  15.     public static final int BYTE_MIN_LENGTH = 50;  
  16.       
  17.       
  18.     //字节数组是否压缩标志位  
  19.     public static final byte FLAG_GBK_STRING_UNCOMPRESSED_BYTEARRAY = 0;  
  20.     public static final byte FLAG_GBK_STRING_COMPRESSED_BYTEARRAY = 1;  
  21.     public static final byte FLAG_UTF8_STRING_COMPRESSED_BYTEARRAY = 2;  
  22.     public static final byte FLAG_NO_UPDATE_INFO = 3;  
  23.       
  24.     /**   
  25.      * 数据压缩   
  26.      *    
  27.      * @param is   
  28.      * @param os   
  29.      * @throws Exception   
  30.      */    
  31.     public static void compress(InputStream is, OutputStream os)     
  32.             throws Exception {     
  33.     
  34.         GZIPOutputStream gos = new GZIPOutputStream(os);     
  35.     
  36.         int count;     
  37.         byte data[] = new byte[BUFFER_LENGTH];     
  38.         while ((count = is.read(data, 0, BUFFER_LENGTH)) != -1) {     
  39.             gos.write(data, 0, count);     
  40.         }     
  41.     
  42.         gos.finish();     
  43.     
  44.         gos.flush();     
  45.         gos.close();     
  46.     }     
  47.       
  48.       
  49.     /**   
  50.      * 数据解压缩   
  51.      *    
  52.      * @param is   
  53.      * @param os   
  54.      * @throws Exception   
  55.      */    
  56.     public static void decompress(InputStream is, OutputStream os)     
  57.             throws Exception {     
  58.     
  59.         GZIPInputStream gis = new GZIPInputStream(is);     
  60.     
  61.         int count;     
  62.         byte data[] = new byte[BUFFER_LENGTH];     
  63.         while ((count = gis.read(data, 0, BUFFER_LENGTH)) != -1) {     
  64.             os.write(data, 0, count);     
  65.         }     
  66.     
  67.         gis.close();     
  68.     }   
  69.       
  70.     /**  
  71.      * 数据压缩  
  72.      *   
  73.      * @param data  
  74.      * @return  
  75.      * @throws Exception  
  76.      */    
  77.     public static byte[] byteCompress(byte[] data) throws Exception {    
  78.         ByteArrayInputStream bais = new ByteArrayInputStream(data);    
  79.         ByteArrayOutputStream baos = new ByteArrayOutputStream();    
  80.     
  81.         // 压缩    
  82.         compress(bais, baos);    
  83.     
  84.         byte[] output = baos.toByteArray();    
  85.     
  86.         baos.flush();    
  87.         baos.close();    
  88.     
  89.         bais.close();    
  90.     
  91.         return output;    
  92.     }   
  93.       
  94.       
  95.     /**  
  96.      * 数据解压缩  
  97.      *   
  98.      * @param data  
  99.      * @return  
  100.      * @throws Exception  
  101.      */    
  102.     public static byte[] byteDecompress(byte[] data) throws Exception {    
  103.         ByteArrayInputStream bais = new ByteArrayInputStream(data);    
  104.         ByteArrayOutputStream baos = new ByteArrayOutputStream();    
  105.     
  106.         // 解压缩    
  107.     
  108.         decompress(bais, baos);    
  109.     
  110.         data = baos.toByteArray();    
  111.     
  112.         baos.flush();    
  113.         baos.close();    
  114.     
  115.         bais.close();    
  116.     
  117.         return data;    
  118.     }    
  119. }  
 



压缩代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ArrayList<Jjjz> jjjzs = null;  
  2.    
  3.  Gson gson = new Gson();  
  4.  String jsonStr = gson.toJson(jjjzs, jjjzs.getClass());  
  5.    
  6.  byte[] resultOriginalByte = jsonStr.getBytes();  
  7.  Compress.byteCompress(resultOriginalByte)//压缩后的数据  


解压代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. byte[] resultByte = Compress.byteDecompress(compressedByte);  
  2.             result = new String(resultByte, "utf-8");  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值