对字符串,字节数组进行GZIP压缩和解压缩

对字符串,字节数组进行GZIP压缩和解压缩,代码如下:

[java]  view plain  copy
  1. import java.io.ByteArrayInputStream;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.IOException;  
  4. import java.io.ObjectInputStream;  
  5. import java.util.zip.DataFormatException;  
  6. import java.util.zip.Deflater;  
  7. import java.util.zip.GZIPInputStream;  
  8. import java.util.zip.GZIPOutputStream;  
  9. import java.util.zip.Inflater;  
  10. import java.util.zip.ZipEntry;  
  11. import java.util.zip.ZipException;  
  12. import java.util.zip.ZipInputStream;  
  13. import java.util.zip.ZipOutputStream;  
  14. import org.apache.log4j.Logger;  
  15. import org.apache.tools.bzip2.CBZip2InputStream;  
  16. import org.apache.tools.bzip2.CBZip2OutputStream;  
  17. public class ZipUtil {  
  18.     private static final Logger LOG = Logger.getLogger(ZipUtil.class);  
  19.     private static final int BUFF_SIZE = 4096;  
  20.     /*************************************************************************** 
  21.      * 压缩GZip 
  22.      *  
  23.      * @param data 
  24.      * @author taoyi 
  25.      * @return 
  26.      */  
  27.     public static byte[] gZip(byte[] data) {  
  28.         byte[] b = null;  
  29.         try {  
  30.             ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  31.             GZIPOutputStream gzip = new GZIPOutputStream(bos);  
  32.             gzip.write(data);  
  33.             gzip.finish();  
  34.             gzip.close();  
  35.             b = bos.toByteArray();  
  36.             bos.close();  
  37.         } catch (Exception ex) {  
  38.             ex.printStackTrace();  
  39.         }  
  40.         return b;  
  41.     }  
  42.     /*************************************************************************** 
  43.      * 解压GZip 
  44.      *  
  45.      * @param data 
  46.      * @author taoyi 
  47.      * @return 
  48.      */  
  49.     public static byte[] unGZip(byte[] data) {  
  50.         byte[] b = null;  
  51.         try {  
  52.             ByteArrayInputStream bis = new ByteArrayInputStream(data);  
  53.             GZIPInputStream gzip = new GZIPInputStream(bis);  
  54.             byte[] buf = new byte[1024];  
  55.             int num = -1;  
  56.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  57.             while ((num = gzip.read(buf, 0, buf.length)) != -1) {  
  58.                 baos.write(buf, 0, num);  
  59.             }  
  60.             b = baos.toByteArray();  
  61.             baos.flush();  
  62.             baos.close();  
  63.             gzip.close();  
  64.             bis.close();  
  65.         } catch (Exception ex) {  
  66.             ex.printStackTrace();  
  67.         }  
  68.         return b;  
  69.     }  
  70.     /*************************************************************************** 
  71.      * 压缩Zip 
  72.      *  
  73.      * @param data 
  74.      * @author zhouxaobo 
  75.      * @return 
  76.      */  
  77.     public static byte[] zip(byte[] data) {  
  78.         byte[] b = null;  
  79.         try {  
  80.             ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  81.             ZipOutputStream zip = new ZipOutputStream(bos);  
  82.             ZipEntry entry = new ZipEntry("zip");  
  83.             entry.setSize(data.length);  
  84.             zip.putNextEntry(entry);  
  85.             zip.write(data);  
  86.             zip.closeEntry();  
  87.             zip.close();  
  88.             b = bos.toByteArray();  
  89.             bos.close();  
  90.         } catch (Exception ex) {  
  91.             ex.printStackTrace();  
  92.         }  
  93.         return b;  
  94.     }  
  95.     /*************************************************************************** 
  96.      * 解压Zip 
  97.      *  
  98.      * @param data 
  99.      * @author zhouxxiaobo 
  100.      * @return 
  101.      */  
  102.     public static byte[] unZip(byte[] data) {  
  103.         byte[] b = null;  
  104.         try {  
  105.             ByteArrayInputStream bis = new ByteArrayInputStream(data);  
  106.             ZipInputStream zip = new ZipInputStream(bis);  
  107.             while (zip.getNextEntry() != null) {  
  108.                 byte[] buf = new byte[1024];  
  109.                 int num = -1;  
  110.                 ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  111.                 while ((num = zip.read(buf, 0, buf.length)) != -1) {  
  112.                     baos.write(buf, 0, num);  
  113.                 }  
  114.                 b = baos.toByteArray();  
  115.                 baos.flush();  
  116.                 baos.close();  
  117.             }  
  118.             zip.close();  
  119.             bis.close();  
  120.         } catch (Exception ex) {  
  121.             ex.printStackTrace();  
  122.         }  
  123.         return b;  
  124.     }  
  125.     /*************************************************************************** 
  126.      * 压缩BZip2 
  127.      * @param data 
  128.      * @author zhouxiaobo 
  129.      * @return 
  130.      */  
  131.     public static byte[] bZip2(byte[] data) {  
  132.         byte[] b = null;  
  133.         try {  
  134.             ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  135.             CBZip2OutputStream bzip2 = new CBZip2OutputStream(bos);  
  136.             bzip2.write(data);  
  137.             bzip2.flush();  
  138.             bzip2.close();  
  139.             b = bos.toByteArray();  
  140.             bos.close();  
  141.         } catch (Exception ex) {  
  142.             ex.printStackTrace();  
  143.         }  
  144.         return b;  
  145.     }  
  146.     /*************************************************************************** 
  147.      * 解压BZip2 
  148.      * @param data 
  149.      * @author zhouxiaobo 
  150.      * @return 
  151.      */  
  152.     public static byte[] unBZip2(byte[] data) {  
  153.         byte[] b = null;  
  154.         try {  
  155.             ByteArrayInputStream bis = new ByteArrayInputStream(data);  
  156.             CBZip2InputStream bzip2 = new CBZip2InputStream(bis);  
  157.             byte[] buf = new byte[1024];  
  158.             int num = -1;  
  159.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  160.             while ((num = bzip2.read(buf, 0, buf.length)) != -1) {  
  161.                 baos.write(buf, 0, num);  
  162.             }  
  163.             b = baos.toByteArray();  
  164.             baos.flush();  
  165.             baos.close();  
  166.             bzip2.close();  
  167.             bis.close();  
  168.         } catch (Exception ex) {  
  169.             ex.printStackTrace();  
  170.         }  
  171.         return b;  
  172.     }  
  173.     /** 
  174.      *  
  175.      * @param str 
  176.      *            待压缩的字符串 
  177.      * @return 
  178.      * @throws IOException 
  179.      * @author zhouxiaobo 
  180.      */  
  181.     public static String compress(String str) throws IOException {  
  182.         if (str == null || str.length() == 0) {  
  183.             return str;  
  184.         }  
  185.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  186.         GZIPOutputStream gzip = new GZIPOutputStream(out);  
  187.         gzip.write(str.getBytes());  
  188.         gzip.close();  
  189.         System.out.println("压缩后:" + out.toString());  
  190.         return out.toString("GBK");  
  191.     }  
  192.     /** 
  193.      *  
  194.      * @param str 
  195.      *            已压缩的字符串 
  196.      * @return 
  197.      * @throws IOException 
  198.      * @author taoyi 
  199.      */  
  200.     public static String uncompress(String str) throws IOException {  
  201.         if (str == null || str.length() == 0) {  
  202.             return str;  
  203.         }  
  204.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  205.         ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("GBK"));  
  206.         GZIPInputStream gunzip = new GZIPInputStream(in);  
  207.         byte[] buffer = new byte[256];  
  208.         int n;  
  209.         while ((n = gunzip.read(buffer)) >= 0) {  
  210.             out.write(buffer, 0, n);  
  211.         }  
  212.         // toString()使用平台默认编码,也可以显式的指定如toString("GBK")  
  213.         return out.toString("GBK");  
  214.     }  
  215.     /** 
  216.      * 解压c++压缩的zlib 
  217.      *  
  218.      * @param compressedData 
  219.      * @return 
  220.      */  
  221.     public static byte[] unZipInflate(byte[] compressedData) {  
  222.         Inflater decompressor = new Inflater();  
  223.         decompressor.setInput(compressedData);  
  224.         // Create an expandable byte array to hold the decompressed data  
  225.         ByteArrayOutputStream bos = new ByteArrayOutputStream(  
  226.                 compressedData.length);  
  227.         // Decompress the data  
  228.         byte[] buf = new byte[1024];  
  229.         while (!decompressor.finished()) {  
  230.             try {  
  231.                 int count = decompressor.inflate(buf);  
  232.                 bos.write(buf, 0, count);  
  233.             } catch (DataFormatException e) {  
  234.                 //e.printStackTrace();  
  235.                 LOG.error("解压C++压缩包出错!");  
  236.                 break;  
  237.             }  
  238.         }  
  239.         try {  
  240.             bos.close();  
  241.         } catch (IOException e) {  
  242.             //e.printStackTrace();  
  243.             LOG.error("流关闭异常!");  
  244.         }  
  245.         // Get the decompressed data  
  246.         return bos.toByteArray();  
  247.     }  
  248.       
  249.     public static String inflater(byte[] buff) {  
  250.         String str = null;  
  251.         int len = buff.length;  
  252.         byte[] result = new byte[len];  
  253.         if ((null == buff) || (0 == len)) {  
  254.             return null;  
  255.         }  
  256.         try {  
  257.             Inflater decompresser = new Inflater();  
  258.             int resultlen = 0;  
  259.             decompresser.setInput(buff, 0, buff.length);  
  260.             resultlen = decompresser.inflate(result);  
  261.             decompresser.end();  
  262.             str = new String(result, 0, resultlen, "UTF-8");  
  263.         } catch (IOException e) {  
  264.             // TODO: handle exception  
  265.             e.printStackTrace();  
  266.         } catch (DataFormatException e) {  
  267.             // TODO Auto-generated catch block  
  268.             e.printStackTrace();  
  269.         }  
  270.         return str;  
  271.     }  
  272.     /** 
  273.      * 压缩压缩的zlib 
  274.      *  
  275.      * @param compressedData 
  276.      * @return 
  277.      * @author zhouxiaobo 
  278.      */  
  279.     public static byte[] ZipDnflate(byte[] compressedData)  
  280.             throws DataFormatException {  
  281.         Deflater compressor = new Deflater();  
  282.         compressor.setInput(compressedData);  
  283.         compressor.finish();  
  284.            
  285.         // Create an expandable byte array to hold the decompressed data  
  286.         ByteArrayOutputStream bos = new ByteArrayOutputStream(  
  287.                 compressedData.length);  
  288.         // Decompress the data  
  289.         byte[] buf = new byte[1024];  
  290.         while (!compressor.finished()) {  
  291.             int count = compressor.deflate(buf);  
  292.             bos.write(buf, 0, count);  
  293.         }  
  294.         try {  
  295.             bos.close();  
  296.         } catch (IOException e) {  
  297.             e.printStackTrace();  
  298.         }  
  299.         // Get the decompressed data  
  300. /*      byte [] byteArrey=bos.toByteArray(); 
  301.         String strZip=new String(byteArrey);*/  
  302.         return bos.toByteArray();  
  303.     }  
  304.     /** 
  305.      * 解压协议包体部分 ,压缩格式为zip 
  306.      */  
  307.     private static Object extract(byte[] compressed) {  
  308.         if (compressed == null)  
  309.             return null;  
  310.         // byte[] compressed;  
  311.         ByteArrayOutputStream out = null;  
  312.         ByteArrayInputStream in = null;  
  313.         ZipInputStream zin = null;  
  314.         Object decompressed = null;  
  315.         try {  
  316.             out = new ByteArrayOutputStream();  
  317.             in = new ByteArrayInputStream(compressed);  
  318.             byte[] buffer = new byte[BUFF_SIZE];  
  319.             int offset = -1;  
  320.             zin = new ZipInputStream(in);  
  321.             ZipEntry entry = zin.getNextEntry();  
  322.             if (entry != null) {  
  323.                 while ((offset = zin.read(buffer)) != -1) {  
  324.                     out.write(buffer, 0, offset);  
  325.                 }  
  326.             }  
  327.             ByteArrayInputStream byteInput = new ByteArrayInputStream(out  
  328.                     .toByteArray());  
  329.             ObjectInputStream ois = new ObjectInputStream(byteInput);  
  330.             decompressed = ois.readObject();  
  331.         } catch (ZipException e1) {  
  332.             // TODO Auto-generated catch block  
  333.             LOG.info("获得压缩流数据出现异常!");  
  334.             e1.printStackTrace();  
  335.         } catch (IOException e1) {  
  336.             LOG.info("I/O流出现异常!");  
  337.             // TODO Auto-generated catch block  
  338.             e1.printStackTrace();  
  339.         } catch (ClassNotFoundException e) {  
  340.             // TODO Auto-generated catch block  
  341.             e.printStackTrace();  
  342.         } finally {  
  343.             if (zin != null) {  
  344.                 try {  
  345.                     zin.close();  
  346.                     zin = null;  
  347.                 } catch (IOException e) {  
  348.                 }  
  349.             }  
  350.             if (in != null) {  
  351.                 try {  
  352.                     in.close();  
  353.                     in = null;  
  354.                 } catch (IOException e) {  
  355.                 }  
  356.             }  
  357.             if (out != null) {  
  358.                 try {  
  359.                     out.close();  
  360.                     out = null;  
  361.                 } catch (IOException e) {  
  362.                 }  
  363.             }  
  364.         }  
  365.         return decompressed;  
  366.     }  
  367. }  

猜你喜欢

转载自blog.csdn.net/kwame211/article/details/80349683