使用gzip进行压缩、解压

  1 package javabase;
  2 
  3 import lombok.extern.slf4j.Slf4j;
  4 
  5 import java.io.ByteArrayInputStream;
  6 import java.io.ByteArrayOutputStream;
  7 import java.io.IOException;
  8 import java.util.zip.GZIPInputStream;
  9 import java.util.zip.GZIPOutputStream;
 10 import java.util.zip.ZipException;
 11 
 12 /**
 13  * Created by chenan on 2020/7/11.
 14  */
 15 
 16 @Slf4j
 17 public class GZIPUtils {
 18     /**
 19      * 使用gzip进行压缩
 20      */
 21     public static String compress(String primStr) {
 22         if (primStr == null || primStr.length() == 0) {
 23             return primStr;
 24         }
 25         ByteArrayOutputStream out = new ByteArrayOutputStream();
 26         GZIPOutputStream gzip = null;
 27         String outString = null;
 28         try {
 29             gzip = new GZIPOutputStream(out);
 30             gzip.write(primStr.getBytes());
 31         } catch (IOException e) {
 32             log.error("gzip压缩失败:", e);
 33         } finally {
 34             if (gzip != null) {
 35                 try {
 36                     gzip.close();
 37                 } catch (IOException e) {
 38                     log.error("gzip压缩失败:", e);
 39                 }
 40             }
 41         }
 42 //        String outString = new sun.misc.BASE64Encoder().encode(out.toByteArray());
 43         try {
 44             outString = out.toString("ISO-8859-1");
 45         } catch (Exception e) {
 46             log.error("压缩转化失败");
 47             outString = primStr;
 48         }
 49         return outString;
 50     }
 51 
 52     /**
 53      * 使用gzip进行解压缩
 54      */
 55     public static String uncompress(String compressedStr) {
 56         if (compressedStr == null) {
 57             return null;
 58         }
 59         ByteArrayOutputStream out = new ByteArrayOutputStream();
 60         ByteArrayInputStream in = null;
 61         GZIPInputStream ginzip = null;
 62         byte[] compressed = null;
 63         String decompressed = null;
 64         try {
 65 //            compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
 66             compressed = compressedStr.getBytes("ISO-8859-1");
 67             in = new ByteArrayInputStream(compressed);
 68             ginzip = new GZIPInputStream(in);
 69 
 70             byte[] buffer = new byte[1024];
 71             int offset = -1;
 72             while ((offset = ginzip.read(buffer)) != -1) {
 73                 out.write(buffer, 0, offset);
 74             }
 75             decompressed = out.toString();
 76         } catch (ZipException e){
 77             log.error("转换格式异常,转化数据为"+compressedStr);
 78             decompressed = compressedStr;
 79         } catch (IOException e) {
 80             log.error("gzip解压失败:", e);
 81             decompressed = compressedStr;
 82         } finally {
 83             if (ginzip != null) {
 84                 try {
 85                     ginzip.close();
 86                 } catch (IOException e) {
 87                     log.error("gzip解压失败:", e);
 88                     decompressed = compressedStr;
 89                 }
 90             }
 91             if (in != null) {
 92                 try {
 93                     in.close();
 94                 } catch (IOException e) {
 95                     log.error("gzip解压失败:", e);
 96                     decompressed = compressedStr;
 97                 }
 98             }
 99             try {
100                 out.close();
101             } catch (IOException e) {
102                 log.error("gzip解压失败:", e);
103                 decompressed = compressedStr;
104             }
105         }
106         return decompressed;
107     }
108 
109 }
我是代码搬运工!!!
原文地址:https://www.cnblogs.com/FanKL/p/13994628.html