Android GZIP压缩与解压工具类

这个博客提供了一个用于在Android中进行GZIP压缩和解压的工具类,包括将字符串压缩为字节数组以及将字节数组解压缩回字符串的方法。这些方法考虑了不同字符编码,并处理了可能的IOException。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. import java.io.ByteArrayInputStream;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.IOException;  
  4. import java.util.zip.GZIPInputStream;  
  5. import java.util.zip.GZIPOutputStream;  
  6.   
  7. /** 
  8.  * GZIP压缩解压类 
  9.  */  
  10. public class MessageGZIP {  
  11.       
  12.     private static String encode = "utf-8";//"ISO-8859-1"  
  13.       
  14.     public String getEncode() {  
  15.         return encode;  
  16.     }  
  17.   
  18.     /* 
  19.      * 设置 编码,默认编码:UTF-8 
  20.      */  
  21.     public void setEncode(String encode) {  
  22.         MessageGZIP.encode = encode;  
  23.     }  
  24.   
  25.     /* 
  26.      * 字符串压缩为字节数组 
  27.      */  
  28.     public static byte[] compressToByte(String str){  
  29.         if (str == null || str.length() == 0) {  
  30.             return null;  
  31.         }  
  32.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  33.         GZIPOutputStream gzip;  
  34.         try {  
  35.             gzip = new GZIPOutputStream(out);  
  36.             gzip.write(str.getBytes(encode));  
  37.             gzip.close();  
  38.         } catch (IOException e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         return out.toByteArray();  
  42.     }  
  43.   
  44.     /* 
  45.      * 字符串压缩为字节数组 
  46.      */  
  47.     public static byte[] compressToByte(String str,String encoding){  
  48.         if (str == null || str.length() == 0) {  
  49.             return null;  
  50.         }  
  51.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  52.         GZIPOutputStream gzip;  
  53.         try {  
  54.             gzip = new GZIPOutputStream(out);  
  55.             gzip.write(str.getBytes(encoding));  
  56.             gzip.close();  
  57.         } catch (IOException e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.         return out.toByteArray();  
  61.     }  
  62.   
  63.     /* 
  64.      * 字节数组解压缩后返回字符串 
  65.      */  
  66.     public static String uncompressToString(byte[] b) {  
  67.         if (b == null || b.length == 0) {  
  68.             return null;  
  69.         }  
  70.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  71.         ByteArrayInputStream in = new ByteArrayInputStream(b);  
  72.   
  73.         try {  
  74.             GZIPInputStream gunzip = new GZIPInputStream(in);  
  75.             byte[] buffer = new byte[256];  
  76.             int n;  
  77.             while ((n = gunzip.read(buffer)) >= 0) {  
  78.                 out.write(buffer, 0, n);  
  79.             }  
  80.         } catch (IOException e) {  
  81.             e.printStackTrace();  
  82.         }  
  83.         return out.toString();  
  84.     }  
  85.   
  86.     /* 
  87.      * 字节数组解压缩后返回字符串 
  88.      */  
  89.     public static String uncompressToString(byte[] b, String encoding) {  
  90.         if (b == null || b.length == 0) {  
  91.             return null;  
  92.         }  
  93.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  94.         ByteArrayInputStream in = new ByteArrayInputStream(b);  
  95.   
  96.         try {  
  97.             GZIPInputStream gunzip = new GZIPInputStream(in);  
  98.             byte[] buffer = new byte[256];  
  99.             int n;  
  100.             while ((n = gunzip.read(buffer)) >= 0) {  
  101.                 out.write(buffer, 0, n);  
  102.             }  
  103.             return out.toString(encoding);  
  104.         } catch (IOException e) {  
  105.             e.printStackTrace();  
  106.         }  
  107.         return null;  
  108.     }  
  109. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值