123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package com.chinacreator.videoalliance.order.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 com.chinacreator.common.util.Base64;
- public class ZipUtils {
- /**
- *
- * 使用gzip进行压缩
- */
- public static String gzip(String primStr) {
- if (primStr == null || primStr.length() == 0) {
- return primStr;
- }
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- GZIPOutputStream gzip = null;
- try {
- gzip = new GZIPOutputStream(out);
- gzip.write(primStr.getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (gzip != null) {
- try {
- gzip.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return new sun.misc.BASE64Encoder().encode(out.toByteArray());
- }
- /**
- *
- * <p>
- * Description:使用gzip进行解压缩
- * </p>
- *
- * @param compressedStr
- * @return
- */
- public static String gunzip(String compressedStr) {
- if (compressedStr == null) {
- return null;
- }
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- ByteArrayInputStream in = null;
- GZIPInputStream ginzip = null;
- byte[] compressed = null;
- String decompressed = null;
- try {
- compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
- 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) {
- e.printStackTrace();
- } finally {
- if (ginzip != null) {
- try {
- ginzip.close();
- } catch (IOException e) {
- }
- }
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- }
- }
- if (out != null) {
- try {
- out.close();
- } catch (IOException e) {
- }
- }
- }
- return decompressed;
- }
- public static void main(String[] args) throws IOException {
- String loaclbeas = Base64.encodeBase64String("我wo阿斯u安萨地方".getBytes());
- System.out.println(loaclbeas);
- System.out.println(new String(Base64.decodeBase64(loaclbeas.getBytes()),"UTF-8"));
- }
- }
|