ZipUtils
- 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的更多相关文章
- 压缩工具类 - ZipUtils.java
压缩工具类,提供压缩文件.解压文件的方法. 源码如下:(点击下载 - ZipUtils.java .FolderUtils.java.ant-1.7.0.jar.commons-io-2.4.jar. ...
- 项目实战工具类(二):ZipUtils(压缩/解压缩文件相关)
import android.content.Context; import android.util.Log; import java.io.File; import java.io.FileInp ...
- Android总结之Gzip/Zip压缩
前言: 做过Android网络开发的都知道,在网络传输中我们一般都会开启GZIP压缩,但是出于刨根问底的天性仅仅知道如何开启就不能满足俺的好奇心的,所以想着写个demo测试一下比较常用的两个数据压缩方 ...
- 图片资源的加密和cocos2d-x中的解密
主要处理png图片,其他格式图片也是一样的原理.阅读前可以简略了解一下png格式图片的Data trunck. 首先使用python脚本去掉png的PNG SIG(8 bytes) 以及末尾的PNGI ...
- ZIP4J---ZIP文件压缩与解压缩学习
package com.wbh.common.utils; import java.io.File; import java.io.FileInputStream; import java.io.IO ...
- Java实现zip压缩多个文件下载
为了更好的演示,首先创建一个文件实体FileBean,包含了文件路径和文件名称: package com.javaweb.entity; import java.io.Serializable; /* ...
- 破解TexturePacker加密资源
http://blog.csdn.net/ynnmnm/article/details/38392795 最近我们要开一个新项目,UI与交互打算借鉴当前正火的<圣火英雄传>,程序开发为了和 ...
- cocos2dx 3.x(TexturePacker进行图片加密)
游戏开发过程中要涉及到大量的图片,使用TexturePacker可以把小图合成大图.这是我们使用最多的功能,但是TexturePacker还带有对图片加密的功能.之前还是对加密不慎了解,所以写下来分享 ...
- java GZIP压缩和解压
最近碰到了一个按GZIP解压指定的输入流数据,备份下 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream ...
随机推荐
- JS工具函数汇总
备注:http://phpjs.org/ 这个站点把PHP常用的方法用js实现了,推荐一下 1.从数组中随机获取几个不重复项 //从一个给定的数组arr中,随机返回num个不重复项 function ...
- 分享知识-快乐自己:SpringMVC 结合使用拦截器(判断是否用户是否已登陆)
基础拦截器操作: 拦截器是一种AOP操作实现,那么在AOP之中用户一定不需要去关注拦截器的存在,用户只需要按照自己已经习惯的处理方式进行代码的编写即可. 首先我们先创建一个自定义的拦截器: packa ...
- 单元测试:TESTNG和powermock的使用
pom文件: <properties> <testng.version>6.8</testng.version> <powermo ...
- mysql密码过期的修改方法(your password has expired)
今天打开SQLyog提示密码过期:Your password has expired 解决方法: 1. 启动MySQL服务 2. 启动MySQL后台 3. 执行以下命令 step 1: S ...
- java对象的初始化过程和创建对象的几种方式
1.加载父类,加载父类的静态属性和静态代码块 2.加载子类,加载子类的静态属性和静态代码块 3.初始化父类中的非静态属性并赋初值,执行父类非静态代码块,执行父类构造. 4.初始化子类中的非静态属性并赋 ...
- Linux 查看并删除.svn目录
1. find . -type d -name ".svn"|xargs rm -rf
- Vmware ESXi 6.5 安装手册
1 安装前准备 1.1 硬件环境准备 无 备注: 本指导书以虚拟光驱.虚拟软驱为例,如使用物理光驱.物理软驱安装系统操作则以实际系统光盘.软盘代替. 1.2 ...
- 树——平衡二叉树插入和查找的JAVA实现(2):增加删除方法
package com.tomsnail.data.tree; /** * AVL二叉平衡树 * @author tomsnail * @date 2015年3月30日 下午4:35:50 */ pu ...
- 《TCP/IP详解卷一:协议》 概述
分层 TCP/IP协议族是一组不同层次上的多个协议的组合.TCP/IP通常被认为是一个四层次协议系统. 链路层(数据链路层或网络接口层):通常包括操作系统中的设备驱动程序和计算 ...
- HDU4027(线段树单点更新区间)
Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K ...