Java工具类——字符串压缩StringCompress

1.compress(String):对字符串进行ZIP压缩饼返回字节数组

2.decompress(byte[]):将压缩的字节数组还原成字符串

用途:可用于将字符串保存到数据库BOLB中。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import java.io.ByteArrayInputStream;    
  2. import java.io.ByteArrayOutputStream;    
  3. import java.io.IOException;    
  4. import java.util.zip.ZipEntry;    
  5. import java.util.zip.ZipInputStream;    
  6. import java.util.zip.ZipOutputStream;    
  7.     
  8. public class StringCompress {    
  9.     public static final byte[] compress(String paramString) {    
  10.         if (paramString == null)    
  11.             return null;    
  12.         ByteArrayOutputStream byteArrayOutputStream = null;    
  13.         ZipOutputStream zipOutputStream = null;    
  14.         byte[] arrayOfByte;    
  15.         try {    
  16.             byteArrayOutputStream = new ByteArrayOutputStream();    
  17.             zipOutputStream = new ZipOutputStream(byteArrayOutputStream);    
  18.             zipOutputStream.putNextEntry(new ZipEntry("0"));    
  19.             zipOutputStream.write(paramString.getBytes());    
  20.             zipOutputStream.closeEntry();    
  21.             arrayOfByte = byteArrayOutputStream.toByteArray();    
  22.         } catch (IOException localIOException5) {    
  23.             arrayOfByte = null;    
  24.         } finally {    
  25.             if (zipOutputStream != null)    
  26.                 try {    
  27.                     zipOutputStream.close();    
  28.                 } catch (IOException localIOException6) {    
  29.             }    
  30.             if (byteArrayOutputStream != null)    
  31.                 try {    
  32.                     byteArrayOutputStream.close();    
  33.                 } catch (IOException localIOException7) {    
  34.             }    
  35.         }    
  36.         return arrayOfByte;    
  37.     }    
  38.     
  39.     @SuppressWarnings("unused")    
  40.     public static final String decompress(byte[] paramArrayOfByte) {    
  41.         if (paramArrayOfByte == null)    
  42.             return null;    
  43.         ByteArrayOutputStream byteArrayOutputStream = null;    
  44.         ByteArrayInputStream byteArrayInputStream = null;    
  45.         ZipInputStream zipInputStream = null;    
  46.         String str;    
  47.         try {    
  48.             byteArrayOutputStream = new ByteArrayOutputStream();    
  49.             byteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);    
  50.             zipInputStream = new ZipInputStream(byteArrayInputStream);    
  51.             ZipEntry localZipEntry = zipInputStream.getNextEntry();    
  52.             byte[] arrayOfByte = new byte[1024];    
  53.             int i = -1;    
  54.             while ((i = zipInputStream.read(arrayOfByte)) != -1)    
  55.                 byteArrayOutputStream.write(arrayOfByte, 0, i);    
  56.             str = byteArrayOutputStream.toString();    
  57.         } catch (IOException localIOException7) {    
  58.             str = null;    
  59.         } finally {    
  60.             if (zipInputStream != null)    
  61.                 try {    
  62.                     zipInputStream.close();    
  63.                 } catch (IOException localIOException8) {    
  64.                 }    
  65.             if (byteArrayInputStream != null)    
  66.                 try {    
  67.                     byteArrayInputStream.close();    
  68.                 } catch (IOException localIOException9) {    
  69.                 }    
  70.             if (byteArrayOutputStream != null)    
  71.                 try {    
  72.                     byteArrayOutputStream.close();    
  73.                 } catch (IOException localIOException10) {    
  74.             }    
  75.         }    
  76.         return str;    
  77.     }    
  78. }    

转自【http://www.open-open.com/lib/view/open1374372029683.html】
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值