package com.yundaex.common.exception.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; public class ZipUtils { /**
*
* 使用gzip进行压缩
* @throws UnsupportedEncodingException
*/
public static String gzip(String primStr) throws UnsupportedEncodingException {
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();
}
}
}
// System.out.println(out.toString("ISO-8859-1"));
// System.out.println(Base64.encode(out.toString("ISO-8859-1")));
System.out.println(out.toString("ISO-8859-1"));
return out.toString("ISO-8859-1");
} /**
*
* <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;
} /**
* 使用zip进行压缩
*
* @param str
* 压缩前的文本
* @return 返回压缩后的文本
*/
public static final String zip(String str) {
if (str == null)
return null;
byte[] compressed;
ByteArrayOutputStream out = null;
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) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return compressedStr;
} /**
* 使用zip进行解压缩
*
* @param compressed
* 压缩后的文本
* @return 解压后的字符串
*/
public static final String unzip(String compressedStr) {
if (compressedStr == null) {
return null;
} ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed = null;
try {
byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
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) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return decompressed;
}
}

ZipUtils的更多相关文章

  1. 压缩工具类 - ZipUtils.java

    压缩工具类,提供压缩文件.解压文件的方法. 源码如下:(点击下载 - ZipUtils.java .FolderUtils.java.ant-1.7.0.jar.commons-io-2.4.jar. ...

  2. 项目实战工具类(二):ZipUtils(压缩/解压缩文件相关)

    import android.content.Context; import android.util.Log; import java.io.File; import java.io.FileInp ...

  3. Android总结之Gzip/Zip压缩

    前言: 做过Android网络开发的都知道,在网络传输中我们一般都会开启GZIP压缩,但是出于刨根问底的天性仅仅知道如何开启就不能满足俺的好奇心的,所以想着写个demo测试一下比较常用的两个数据压缩方 ...

  4. 图片资源的加密和cocos2d-x中的解密

    主要处理png图片,其他格式图片也是一样的原理.阅读前可以简略了解一下png格式图片的Data trunck. 首先使用python脚本去掉png的PNG SIG(8 bytes) 以及末尾的PNGI ...

  5. ZIP4J---ZIP文件压缩与解压缩学习

    package com.wbh.common.utils; import java.io.File; import java.io.FileInputStream; import java.io.IO ...

  6. Java实现zip压缩多个文件下载

    为了更好的演示,首先创建一个文件实体FileBean,包含了文件路径和文件名称: package com.javaweb.entity; import java.io.Serializable; /* ...

  7. 破解TexturePacker加密资源

    http://blog.csdn.net/ynnmnm/article/details/38392795 最近我们要开一个新项目,UI与交互打算借鉴当前正火的<圣火英雄传>,程序开发为了和 ...

  8. cocos2dx 3.x(TexturePacker进行图片加密)

    游戏开发过程中要涉及到大量的图片,使用TexturePacker可以把小图合成大图.这是我们使用最多的功能,但是TexturePacker还带有对图片加密的功能.之前还是对加密不慎了解,所以写下来分享 ...

  9. java GZIP压缩和解压

    最近碰到了一个按GZIP解压指定的输入流数据,备份下 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream ...

随机推荐

  1. BZOJ 1637 [Usaco2007 Mar]Balanced Lineup:前缀和 + 差分

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1637 题意: Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ...

  2. 时间序列数据库概览——基于文件(RRD)、K/V数据库(influxDB)、关系型数据库

    一般人们谈论时间序列数据库的时候指代的就是这一类存储.按照底层技术不同可以划分为三类. 直接基于文件的简单存储:RRD Tool,Graphite Whisper.这类工具附属于监控告警工具,底层没有 ...

  3. Mybatis-Generator_学习_01_mybatis-generator笔记

    一.generatorConfig.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  4. 常见css兼容问题

    链接的虚线框问题 <!-- html --> <a class="noDashedBox" href="#"><img src=& ...

  5. 002-CSS基础

    1 CSS和文档 CSS 层叠样式表 元素 每个元素都会生成一个框(box) 元素 = 替换元素 + 非替换元素 替换元素 显示的内容是元素内的某个属性而不是元素本身, 如img 非替换元素 大部分类 ...

  6. SpringBoot 版本升级后报错 Cannot instantiate interface org.springframework.context.ApplicationContextInitializer

    本篇博客纯粹讲我遇到这个问题的解决以及思考,如果你想知道解决方法,可以直接看正确解决方案部分.因为是前端写的,所以可能有些明显的内容很容易就看出来了. 首先:升级后更新其他依赖,以及Applicati ...

  7. 如何运行jnlp文件

    运行DOS命令: C:\Users\thinkpad>javaws D:\***.jnlp 如提示“应用程序已被java安全阻止”则进入控制面板->Java  打开java控制面板,在安全 ...

  8. 分布式一致性协议之:Paxos算法(转)

    Paxos算法的难理解与算法的知名度一样令人敬仰,从我个人的经历而言,难理解的原因并不是该算法高深到大家智商不够,而在于Lamport在表达该算法时过于晦涩且缺乏一个完整的应用场景.如果大师能换种思路 ...

  9. oracle 查询及删除重复记录的SQL语句

    查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 where Id in (select Id from 表 group ...

  10. CF-828C

    C. String Reconstruction time limit per test 2 seconds memory limit per test 256 megabytes input sta ...