Android GZIP压缩与解压工具类

来源:互联网 发布:软件安全性测试 编辑:程序博客网 时间:2024/06/05 05:33
  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. }  
0 0
原创粉丝点击