package com.wy.data.emaildata.util;

import org.apache.commons.io.FileUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
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进行压缩
*/
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;
} /**
* 使用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 compressedStr 压缩后的文本
* @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;
} public static void main(String[] args) throws IOException {
String strOld=FileUtils.readFileToString(new File("D:/test.txt"),"utf-8");
System.out.println(strOld.length());
String gzip = zip(strOld);
System.out.println(gzip.length());
}
}

最新文章

  1. js控制台输出console
  2. 编译Android AOSP代码
  3. spring mvc拦截器和&lt;mvc:annotation-driven /&gt;的详解
  4. Ieditor
  5. [MobilewebApp]图片的适配与清晰度
  6. Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析(转载)
  7. MFC简易画图
  8. tomcat之JNDI数据源配置
  9. PHP 开发中的外围资源性能分析(二)
  10. 面试题——设计一个程序:运行报错Stack Overflow Error
  11. Akka的fault tolerant
  12. 阅读《effective java-第17条》遇到的问题解决与分享
  13. C# .NET ASP.NET 其中关系你了解多少
  14. Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦
  15. (cljs/run-at (JSVM. :all) &quot;细说函数&quot;)
  16. Win7 安装bundle
  17. PDF 补丁丁 0.6.0.3427 版发布(修复提取图片问题)
  18. 11.1 vue(2)
  19. iOS push新的调用方法
  20. 【转】每天一个linux命令(29):chgrp命令

热门文章

  1. Docker工具
  2. python模块之shutil和zipfile
  3. CodeForce:732B-Cormen — The Best Friend Of a Man
  4. 数学基础:HUD1124-Factorial(N!末尾0的个数)
  5. 排序 sort函数
  6. CodeForces 599E Sandy and Nuts 状压DP
  7. JAVA-基础(六) Java.serialization 序列化
  8. Ubuntu超简单文书编辑器:nano
  9. 大咖分享 | 一文解锁首届云创大会干货——上篇(文末附演讲ppt文件免费下载)
  10. 带有命名空间的xml解析,C#