java压缩解压string

用到3个类

ZipUtil.java包含压缩解压string的方法

ZipUtilException.java压缩解压自定义异常类

ZipUtilTest.java测试类


ZipUtil.java

package util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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;

import org.apache.log4j.Logger;

/**
 * @Title 压缩工具类
 * @Description Linux系统gzip压缩,gunzip解压;Windows系统用zip压缩,unzip解压.
 * @author wangzs
 * @date 2016-4-15
 */
public class ZipUtil {
	private static Logger logger = Logger.getLogger(ZipUtil.class);

	/**
	 * 使用gzip进行压缩
	 * 
	 * @param str
	 *            需要压缩的字符
	 * @return 压缩后的字符
	 * @throws ZipUtilException
	 * @throws UnsupportedEncodingException
	 */
	public static String gzip(String str) throws ZipUtilException {
		if (str == null) {
			logger.error("使用gzip进行压缩,输入字符为null.");
			throw new ZipUtilException("使用gzip进行压缩,输入字符为null.");
		}

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		// GZIPOutputStream 此类为使用 GZIP 文件格式写入压缩数据实现流过滤器。
		GZIPOutputStream gzip = null;
		try {
			gzip = new GZIPOutputStream(out);
			gzip.write(str.getBytes());
		} catch (IOException e) {
			logger.error("使用gzip进行压缩exception." + e.getMessage());
			throw new ZipUtilException("使用gzip进行压缩exception.", e);
		} finally {
			if (gzip != null) {
				try {
					gzip.close();
				} catch (IOException e) {
					logger.error("使用gzip进行压缩,关闭GZIPOutputStream流exception." + e.getMessage());
					throw new ZipUtilException("使用gzip进行压缩,关闭GZIPOutputStream流exception.", e);
				}
			}
		}
		return new sun.misc.BASE64Encoder().encode(out.toByteArray());
	}

	/**
	 * 使用gunzip进行解压
	 * 
	 * @param str
	 *            需要解压的字符
	 * @return 解压后的字符
	 * @throws ZipUtilException
	 */
	public static String gunzip(String str) throws ZipUtilException {
		if (str == null) {
			logger.error("使用gunzip进行解压,输入字符为null.");
			throw new ZipUtilException("使用gunzip进行解压,输入字符为null.");
		}

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = null;
		// GZIPInputStream此类为读取 GZIP 文件格式的压缩数据实现流过滤器。
		GZIPInputStream ginzip = null;
		byte[] compressed = null;
		String decompressed = null;
		try {
			compressed = new sun.misc.BASE64Decoder().decodeBuffer(str);
			in = new ByteArrayInputStream(compressed);
			ginzip = new GZIPInputStream(in);

			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = ginzip.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressed = out.toString();
		} catch (IOException e) {
			logger.error("使用gunzip进行解压exception." + e.getMessage());
			throw new ZipUtilException("使用gunzip进行解压exception.", e);
		} finally {
			if (ginzip != null) {
				try {
					ginzip.close();
				} catch (IOException e) {
					logger.error("使用gzip进行解压,关闭GZIPInputStream流exception." + e.getMessage());
					throw new ZipUtilException("使用gzip进行解压,关闭GZIPInputStream流exception.", e);
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					logger.error("使用gunzip进行解压,关闭ByteArrayInputStream流exception." + e.getMessage());
					throw new ZipUtilException("使用gunzip进行解压,关闭ByteArrayInputStream流exception.", e);
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					logger.error("使用gunzip进行解压,关闭ByteArrayOutputStream流exception." + e.getMessage());
					throw new ZipUtilException("使用gunzip进行解压,关闭ByteArrayOutputStream流exception.", e);
				}
			}
		}
		return decompressed;
	}

	/**
	 * 使用zip进行压缩
	 * 
	 * @param str
	 *            需要压缩的字符
	 * @return 压缩后的字符
	 * @throws ZipUtilException
	 */
	public static final String zip(String str) throws ZipUtilException {
		if (str == null) {
			logger.error("使用zip进行压缩,输入字符为null.");
			throw new ZipUtilException("使用zip进行压缩,输入字符为null.");
		}
		byte[] compressed;
		ByteArrayOutputStream out = null;
		// ZipOutputStream此类为以 ZIP 文件格式写入文件实现输出流过滤器。包括对已压缩和未压缩条目的支持。
		ZipOutputStream zout = null;
		String compressedStr = null;
		try {
			out = new ByteArrayOutputStream();
			zout = new ZipOutputStream(out);
			zout.putNextEntry(new ZipEntry("0"));
			zout.write(str.getBytes());
			zout.closeEntry();
			compressed = out.toByteArray();
			compressedStr = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
		} catch (IOException e) {
			compressed = null;
		} finally {
			if (zout != null) {
				try {
					zout.close();
				} catch (IOException e) {
					logger.error("使用zip进行压缩,关闭ZipOutputStream流exception." + e.getMessage());
					throw new ZipUtilException("使用zip进行压缩,关闭ZipOutputStream流exception.", e);
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					logger.error("使用zip进行压缩,关闭ByteArrayOutputStream流exception." + e.getMessage());
					throw new ZipUtilException("使用zip进行压缩,关闭ByteArrayOutputStream流exception.", e);
				}
			}
		}
		return compressedStr;
	}

	/**
	 * 使用unzip进行解压
	 * 
	 * @param str
	 *            需要解压的字符
	 * @return 解压后的字符
	 * @throws ZipUtilException
	 */
	public static final String unzip(String str) throws ZipUtilException {
		if (str == null) {
			logger.error("使用unzip进行解压,输入字符为null.");
			throw new ZipUtilException("使用unzip进行解压,输入字符为null.");
		}

		ByteArrayOutputStream out = null;
		ByteArrayInputStream in = null;
		// ZipInputStream此类为读取 ZIP 文件格式的文件实现输入流过滤器。包括对已压缩和未压缩条目的支持。
		ZipInputStream zin = null;
		String decompressed = null;
		try {
			byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(str);
			out = new ByteArrayOutputStream();
			in = new ByteArrayInputStream(compressed);
			zin = new ZipInputStream(in);
			zin.getNextEntry();
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = zin.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressed = out.toString();
		} catch (IOException e) {
			decompressed = null;
		} finally {
			if (zin != null) {
				try {
					zin.close();
				} catch (IOException e) {
					logger.error("使用unzip进行解压,关闭ZipInputStream流exception." + e.getMessage());
					throw new ZipUtilException("使用unzip进行解压,关闭ZipInputStream流exception.", e);
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					logger.error("使用unzip进行解压,关闭ByteArrayInputStream流exception." + e.getMessage());
					throw new ZipUtilException("使用unzip进行解压,关闭ByteArrayInputStream流exception.", e);
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					logger.error("使用unzip进行解压,关闭ByteArrayOutputStream流exception." + e.getMessage());
					throw new ZipUtilException("使用unzip进行解压,关闭ByteArrayOutputStream流exception.", e);
				}
			}
		}
		return decompressed;
	}
}

ZipUtilException.java

package util;

/**
 * @Title 压缩工具类ZipUtil自定义异常
 * @Description 压缩工具类ZipUtil自定义异常
 * @author wangzs
 * @date 2016-4-15
 */
public class ZipUtilException extends Exception {
	private static final long serialVersionUID = 7188947115083638233L;

	public ZipUtilException() {
		super();
	}

	public ZipUtilException(String message) {
		super(message);
	}

	public ZipUtilException(String message, Throwable cause) {
		super(message, cause);
	}
}

ZipUtilTest.java

package util;

import org.junit.Assert;
import org.junit.Test;

/**
 * @Title ZipUtil压缩工具类测试
 * @Description ZipUtil压缩工具类测试
 * @author wangzs
 * @date 2016-4-15
 */
public class ZipUtilTest {
	private static String LOCAL_STR = "我们都是中国人0123456789qwertyuiopasdfghjklzxcvbnm,/~·.~!@#¥%……&*()——+.<>/?;':";

	// private static String LOCAL_STR = "";
	// private static String LOCAL_STR = null;

	@Test
	public void test_gzip() {
		System.out.println("原始字符:" + LOCAL_STR);
		String gzipStr = "";
		try {
			gzipStr = ZipUtil.gzip(LOCAL_STR);
		} catch (ZipUtilException e) {
			e.printStackTrace();
		}
		System.out.println("gzip压缩结果:" + gzipStr);
		System.out.println("----------------------");
	}

	@Test
	public void test_gunzip() {
		System.out.println("原始字符:" + LOCAL_STR);
		String gzipStr = "";
		try {
			gzipStr = ZipUtil.gzip(LOCAL_STR);
		} catch (ZipUtilException e) {
			e.printStackTrace();
		}
		System.out.println("gzip压缩结果:" + gzipStr);

		String gunzipStr = "";
		try {
			gunzipStr = ZipUtil.gunzip(gzipStr);
		} catch (ZipUtilException e) {
			e.printStackTrace();
		}
		System.out.println("gunzip解压结果:" + gunzipStr);

		Assert.assertEquals(LOCAL_STR, gunzipStr);
		System.out.println("----------------------");
	}

	@Test
	public void test_zip() {
		System.out.println("原始字符:" + LOCAL_STR);
		String zipStr = "";
		try {
			zipStr = ZipUtil.zip(LOCAL_STR);
		} catch (ZipUtilException e) {
			e.printStackTrace();
		}
		System.out.println("zip压缩结果:" + zipStr);
		System.out.println("----------------------");
	}

	@Test
	public void test_unzip() {
		System.out.println("原始字符:" + LOCAL_STR);
		String zipStr = "";
		try {
			zipStr = ZipUtil.zip(LOCAL_STR);
		} catch (ZipUtilException e) {
			e.printStackTrace();
		}
		System.out.println("zip压缩结果:" + zipStr);

		String unzipStr = "";
		try {
			unzipStr = ZipUtil.unzip(zipStr);
		} catch (ZipUtilException e) {
			e.printStackTrace();
		}
		System.out.println("unzip解压结果:" + unzipStr);

		Assert.assertEquals(LOCAL_STR, unzipStr);
		System.out.println("----------------------");
	}
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值