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 ...
随机推荐
- BZOJ 1647 [Usaco2007 Open]Fliptile 翻格子游戏:部分枚举 位运算
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1647 题意: 在一个n*m(1 <= n,m <= 15)的棋盘上,每一个格子 ...
- BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1375 题意: 给你n个数,你可以从中选任意多个,但不能不选.问你所选数字之和为f的倍数 ...
- laravel 在apache或nginx的配置
laravel 下载后,如何运行起来呢,根据自己的应用,记录了几个关键点: 1.apache 配置: 打开http.conf文件,将mod_rewrite前面的#去掉(启用重写模块): 2.nginx ...
- ECMAScript Obejct 属性操作API
Object 创建对象 我们有很多种方法创建一个对象,Object.create, Object.assign Object.create //字面量 var o = {}; // 等同于 var o ...
- 2015推荐的Android框架
一.Guava Google的基于java1.6的类库集合的扩展项目,包括collections, caching, primitives support, concurrency libraries ...
- onhashchange
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 1026 Table Tennis (30)(30 分)
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...
- .NETFramework:WebClient
ylbtech-.NETFramework:WebClient 1.程序集 System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5 ...
- Poco 编译mysql
POCO mysql需要自己添加connecter的header和lib MySQL Client: For the MySQL connector, the MySQL client librari ...
- Advanced R之函数
转载请注明出处,谢谢. 再次声明下,本人水平有些,错误之处敬请指正. 函数 函数是R基本的块结构单元:为了掌握本书中的更高级技术,你需要对函数有扎实的了解.也许你已经写过一些函数,并了解函数的基本知识 ...