对ZIP和GZIP内容进行压缩/解压缩+Base64编码/解码处理

package com.zsj.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * 对ZIP和GZIP内容进行压缩/解压缩+Base64编码/解码处理
 * 
 * @author Think
 */
public final class GZipUtil {
    
	public static final String ENCODE_UTF_8 = "UTF-8";
	public static final String ENCODE_ISO_8859_1 = "ISO-8859-1";

//	public static final Charset CHARSET_UTF_8 = Charset.forName(ENCODE_UTF_8);
//	public static final Charset CHARSET_ISO_8859_1 = Charset.forName(ENCODE_ISO_8859_1);
	public static final Charset CHARSET_UTF_8 = StandardCharsets.UTF_8;
	public static final Charset CHARSET_ISO_8859_1 = StandardCharsets.ISO_8859_1;

	
	/**
	 * 使用gzip进行压缩
	 * GZIP压缩+Base64编码:对整个业务报文进行标准GZIP压缩,压缩后通过标准Base64编码转成字符串后放到ywxx业务节点中
	 */
	public static String gzip(String str) throws Exception {
		if (str == null || str.isEmpty()) {
			return str;
		}
		
		// 注释:嵌套打开的流只需关闭最后打开的流,先打开的会自动关闭
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPOutputStream gzip = null;
		try {
			gzip = new GZIPOutputStream(out);
			//gzip.write(str.getBytes()); // 这里字符集为默认字符集
			gzip.write(str.getBytes(CHARSET_UTF_8));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
//			close(gzip);
//			close(out);
			close(gzip, out);
		}
		
//		String compressedStr = new String(org.apache.commons.codec.binary.Base64.encodeBase64(out.toByteArray()), CHARSET_UTF_8);
//		String compressedStr = java.util.Base64.getEncoder().encodeToString(out.toByteArray()); // 需要jdk8+,注意:这里面调用了已过时不建议使用的String构造方法
		String compressedStr = new String(java.util.Base64.getEncoder().encode(out.toByteArray()), CHARSET_UTF_8); // 需要jdk8+
		return compressedStr;
	}
	
	/**
	 * 使用gzip进行解压缩
	 * Base64解码+GZIP解压缩:对整个业务报文进行标准GZIP压缩,压缩后通过标准Base64解码转成字符串后放到ywxx业务节点中
	 */
	public static String gunzip(String compressedStr) throws Exception {
		if (compressedStr == null || compressedStr.isEmpty()) {
			return compressedStr;
		}
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = null;
		GZIPInputStream gzipin = null;
		String decompressedStr = null;
		try {
//			byte[] compressed = org.apache.commons.codec.binary.Base64.decodeBase64(compressedStr);
//			byte[] compressed = java.util.Base64.getDecoder().decode(compressedStr); // 需要jdk8+,注意:这里的解码字符集为ISO-8859-1
			byte[] compressed = java.util.Base64.getDecoder().decode(compressedStr.getBytes(CHARSET_UTF_8)); // 需要jdk8+

			in = new ByteArrayInputStream(compressed);
			gzipin = new GZIPInputStream(in);
			
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = gzipin.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressedStr = out.toString(ENCODE_UTF_8);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
//			close(gzipin);
//			close(in);
//			close(out);
			close(gzipin, in, out);
		}
		
		return decompressedStr;
	}
	
	
	/**
	 * 使用zip进行压缩
	 * GZIP压缩+Base64编码
	 */
	public static String zip(String str) throws Exception {
		if (str == null || str.isEmpty()) {
			return str;
		}
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ZipOutputStream zipout = null;
		String compressedStr = null;
		try {
			zipout = new ZipOutputStream(out);
			zipout.putNextEntry(new ZipEntry("0"));
//			zipout.write(str.getBytes()); // 这里字符集为默认字符集
			zipout.write(str.getBytes(CHARSET_UTF_8));
			zipout.closeEntry();
			
//			compressedStr = new String(org.apache.commons.codec.binary.Base64.encodeBase64(out.toByteArray()), CHARSET_UTF_8);
//			compressedStr = java.util.Base64.getEncoder().encodeToString(out.toByteArray()); // 需要jdk8+,注意:这里面调用了已过时不建议使用的String构造方法
			compressedStr = new String(java.util.Base64.getEncoder().encode(out.toByteArray()), CHARSET_UTF_8); // 需要jdk8+
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
//			close(zipout);
//			close(out);
			close(zipout, out);
		}
		
		return compressedStr;
	}
	
	/**
	 * 使用zip进行解压缩
	 * Base64解码+GZIP解压缩
	 */
	public static String unzip(String compressedStr) throws Exception {
		if (compressedStr == null || compressedStr.isEmpty()) {
			return compressedStr;
		}
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = null;
		ZipInputStream zipin = null;
		String decompressedStr = null;
		try {
//			byte[] compressed = org.apache.commons.codec.binary.Base64.decodeBase64(compressedStr);
//			byte[] compressed = java.util.Base64.getDecoder().decode(compressedStr); // 需要jdk8+,注意:这里的解码字符集为ISO-8859-1
			byte[] compressed = java.util.Base64.getDecoder().decode(compressedStr.getBytes(CHARSET_UTF_8)); // 需要jdk8+
			
			in = new ByteArrayInputStream(compressed);
			zipin = new ZipInputStream(in);
			zipin.getNextEntry();
			
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = zipin.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressedStr = out.toString(ENCODE_UTF_8);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
//			close(zipin);
//			close(in);
//			close(out);
			close(zipin, in, out);
		}
		
		return decompressedStr;
	}
	
	
	/**
	 * 实现了Closeable接口的实现类调用close方法进行资源释放
	 */
	private static void close(Closeable... objects) {
		for (Closeable object : objects) {
			if (object != null) {
				try {
					object.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}

用心工作,认真生活。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值