Java-ZipUtil工具类
来源:互联网 发布:燕郊招聘淘宝客服 编辑:程序博客网 时间:2024/05/19 00:47
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;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); //compressed=compressedStr.getBytes(); 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; }}
阅读全文
0 0
- Java-ZipUtil工具类
- Java-ZipUtil工具类
- Java压缩和解压文件工具类ZipUtil
- Java压缩和解压文件工具类ZipUtil
- Java压缩和解压文件工具类ZipUtil
- Java压缩和解压文件工具类ZipUtil
- Java压缩和解压文件工具类ZipUtil
- ZipUtil压缩解压工具类——java
- Java压缩和解压文件工具类ZipUtil
- ZipUtil
- ZipUtil
- ZipUtil
- ZipUtil 历史上最好用的 压缩解压缩工具类 附代码
- ZipUtil 压缩包工具类
- android ziputil
- ZIPUtil压缩解压缩
- 递归压缩文件或目录ZipUtil
- java文件工具类
- 在unity3d中动态创建easyAR的imageTarget
- 【c#】c#中 函数参数的注意事项
- 七月算法深度学习 第三期 学习笔记-第一节 数学基础
- 区间最多约数
- jQuery基础知识
- Java-ZipUtil工具类
- echarts动态绘制折线图
- github设置添加SSH
- 欢迎使用CSDN-markdown编辑器
- 公司是怎么招聘软件工程师的---朱有鹏物联网大讲堂
- 趣解Spring-Ioc的含义及注入方式
- CW3003/CW3004/3005赛微双通道智能USB识别充电芯片方案
- 啦啦啦,我是卖报的小行家
- OpenStack多节点安装(一):基本环境