Java压缩技术(三) ZIP解压缩——Java原生实现
原文:http://snowolf.iteye.com/blog/642492
JavaEye的朋友跟我说:“你一口气把ZIP压缩和解压缩都写到一个帖子里,我看起来很累,不如分开好阅读”。ok,面向读者需求,我做调整,这里单说ZIP解压缩!
解压缩与压缩运作方式相反,原理大抵相同,由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码,如:
- CheckedInputStream cis = new CheckedInputStream(new FileInputStream(
- srcFile), new CRC32());
- ZipInputStream zis = new ZipInputStream(cis);
需要注意的是,在构建解压文件时,需要考虑目录的自动创建,这里通过递归方式逐层创建父目录,如下所示:
- /**
- * 文件探针
- *
- *
- * 当父目录不存在时,创建目录!
- *
- *
- * @param dirFile
- */
- private static void fileProber(File dirFile) {
- File parentFile = dirFile.getParentFile();
- if (!parentFile.exists()) {
- // 递归寻找上级目录
- fileProber(parentFile);
- parentFile.mkdir();
- }
- }
在压缩的时候,我们是将一个一个文件作为压缩添加项(ZipEntry)添加至压缩包中,解压缩就要将一个一个压缩项从压缩包中提取出来,如下所示:
- /**
- * 文件 解压缩
- *
- * @param destFile
- * 目标文件
- * @param zis
- * ZipInputStream
- * @throws Exception
- */
- private static void decompress(File destFile, ZipInputStream zis)
- throws Exception {
- ZipEntry entry = null;
- while ((entry = zis.getNextEntry()) != null) {
- // 文件
- String dir = destFile.getPath() + File.separator + entry.getName();
- File dirFile = new File(dir);
- // 文件检查
- fileProber(dirFile);
- if (entry.isDirectory()){
- dirFile.mkdirs();
- } else {
- decompressFile(dirFile, zis);
- }
- zis.closeEntry();
- }
- }
最核心的解压缩实现,其实与压缩实现非常相似,代码如下所示:
- /**
- * 文件解压缩
- *
- * @param destFile
- * 目标文件
- * @param zis
- * ZipInputStream
- * @throws Exception
- */
- private static void decompressFile(File destFile, ZipInputStream zis)
- throws Exception {
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream(destFile));
- int count;
- byte data[] = new byte[BUFFER];
- while ((count = zis.read(data, 0, BUFFER)) != -1) {
- bos.write(data, 0, count);
- }
- bos.close();
- }
来个完整的解压缩实现,代码如下:
- /**
- * 2010-4-12
- */
- package org.zlex.commons.io;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.util.zip.CRC32;
- import java.util.zip.CheckedInputStream;
- import java.util.zip.CheckedOutputStream;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- import java.util.zip.ZipOutputStream;
- /**
- * ZIP压缩工具
- *
- * @author 梁栋
- * @since 1.0
- */
- public class ZipUtils {
- public static final String EXT = ".zip";
- private static final String BASE_DIR = "";
- private static final String PATH = File.separator;
- private static final int BUFFER = 1024;
- /**
- * 文件 解压缩
- *
- * @param srcPath
- * 源文件路径
- *
- * @throws Exception
- */
- public static void decompress(String srcPath) throws Exception {
- File srcFile = new File(srcPath);
- decompress(srcFile);
- }
- /**
- * 解压缩
- *
- * @param srcFile
- * @throws Exception
- */
- public static void decompress(File srcFile) throws Exception {
- String basePath = srcFile.getParent();
- decompress(srcFile, basePath);
- }
- /**
- * 解压缩
- *
- * @param srcFile
- * @param destFile
- * @throws Exception
- */
- public static void decompress(File srcFile, File destFile) throws Exception {
- CheckedInputStream cis = new CheckedInputStream(new FileInputStream(
- srcFile), new CRC32());
- ZipInputStream zis = new ZipInputStream(cis);
- decompress(destFile, zis);
- zis.close();
- }
- /**
- * 解压缩
- *
- * @param srcFile
- * @param destPath
- * @throws Exception
- */
- public static void decompress(File srcFile, String destPath)
- throws Exception {
- decompress(srcFile, new File(destPath));
- }
- /**
- * 文件 解压缩
- *
- * @param srcPath
- * 源文件路径
- * @param destPath
- * 目标文件路径
- * @throws Exception
- */
- public static void decompress(String srcPath, String destPath)
- throws Exception {
- File srcFile = new File(srcPath);
- decompress(srcFile, destPath);
- }
- /**
- * 文件 解压缩
- *
- * @param destFile
- * 目标文件
- * @param zis
- * ZipInputStream
- * @throws Exception
- */
- private static void decompress(File destFile, ZipInputStream zis)
- throws Exception {
- ZipEntry entry = null;
- while ((entry = zis.getNextEntry()) != null) {
- // 文件
- String dir = destFile.getPath() + File.separator + entry.getName();
- File dirFile = new File(dir);
- // 文件检查
- fileProber(dirFile);
- if (entry.isDirectory()) {
- dirFile.mkdirs();
- } else {
- decompressFile(dirFile, zis);
- }
- zis.closeEntry();
- }
- }
- /**
- * 文件探针
- *
- *
- * 当父目录不存在时,创建目录!
- *
- *
- * @param dirFile
- */
- private static void fileProber(File dirFile) {
- File parentFile = dirFile.getParentFile();
- if (!parentFile.exists()) {
- // 递归寻找上级目录
- fileProber(parentFile);
- parentFile.mkdir();
- }
- }
- /**
- * 文件解压缩
- *
- * @param destFile
- * 目标文件
- * @param zis
- * ZipInputStream
- * @throws Exception
- */
- private static void decompressFile(File destFile, ZipInputStream zis)
- throws Exception {
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream(destFile));
- int count;
- byte data[] = new byte[BUFFER];
- while ((count = zis.read(data, 0, BUFFER)) != -1) {
- bos.write(data, 0, count);
- }
- bos.close();
- }
- }
其实,理解了ZIP的工作原理,这些代码看起来很好懂!
把刚才做的压缩文件再用上述代码解开看看,测试用例如下:
- /**
- * 2010-4-12
- */
- package org.zlex.commons.io;
- import static org.junit.Assert.*;
- import org.junit.Test;
- /**
- *
- * @author 梁栋
- * @version 1.0
- * @since 1.0
- */
- public class ZipUtilsTest {
- /**
- *
- */
- @Test
- public void test() throws Exception {
- // 解压到指定目录
- ZipUtils.decompress("d:\\f.txt.zip", "d:\\ff");
- // 解压到当前目录
- ZipUtils.decompress("d:\\fd.zip");
- }
- }
完整代码详见附件!
java原生的ZIP实现虽然在压缩时会因与系统字符集不符产生中文乱码,但在解压缩后,字符集即可恢复。
除了java原生的ZIP实现外,commons和ant也提供了相应的ZIP算法实现,有机会我再一一介绍!
Java压缩技术(三) ZIP解压缩——Java原生实现的更多相关文章
- Java压缩技术(二) ZIP压缩——Java原生实现
原文:http://snowolf.iteye.com/blog/642298 去年整理了一篇ZLib算法Java实现(Java压缩技术(一) ZLib),一直惦记却没时间补充.今天得空,整理一下ZI ...
- Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)
本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...
- Java基础:三步学会Java Socket编程
Java基础:三步学会Java Socket编程 http://tech.163.com 2006-04-10 09:17:18 来源: java-cn 网友评论11 条 论坛 第一步 ...
- Java压缩技术的学习
由于工作的需要,经常要手动去打上线安装包,为了方便,自己写程序去帮助打包.使用过Unix或者Linux的人都基本上都用过tar打包以及gzip压缩,但在Windows下使用得最多的压缩还是RAR和Zi ...
- java 压缩技术
package zip; import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStr ...
- JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包
package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...
- java 压缩和解压zip包
网上有关压缩和解压zip包的博文一大堆,我随便找了一个.看了看,依照自己的须要改动了一下,与各位分享一下,希望各位大神指正: package com.wangpeng.utill; import ja ...
- Java虚拟机(三):Java 类的加载机制
1.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封装类在方法区内的数据结构 ...
- 赶紧收藏!王者级别的Java多线程技术笔记,我java小菜鸡愿奉你为地表最强!
Java多线程技术概述 介绍多线程之前要介绍线程,介绍线程则离不开进程. 首先 , 进程 :是一个正在执行中的程序,每一个进程执行都有一个执行顺序,该顺序是一个执行路径,或者叫一个控制单元: 线程:就 ...
随机推荐
- microsoft ajax registered - to fix microsoft ajax update panel post back
<dnn:DnnScriptBlock runat="server"> <script type="text/javascript"& ...
- WCF开发的流程-服务端和客户端之间的通讯(内含demo讲解)
讲解技术之前,恳请博友让我说几句废话.今天是我第一在博客园发布属于自己原创的博文(如有雷同,那是绝对不可能的事,嘿嘿).之前一直是拜读各位博友的大作,受益匪浅的我在这对博友们说声谢谢,谢谢你们的共享! ...
- linux mysql-workbench 创建与正式库表结构一样的表
先在本地创建数据库 字符集选择这个 创建数据库成功 创建与正式库一样的表 step1: 连接正式库,找到要生成的表,导出创建表的sql语句 step2: 找到本地数据库,选择表,在sql执行区域复制s ...
- CAD动态绘制带面积周长的圆(com接口)
CAD绘制图像的过程中,画圆的情况是非常常见的,用户可以在控件视区点取任意一点做为圆心,再动态点取半径绘制圆. 主要用到函数说明: _DMxDrawX::DrawCircle 绘制一个圆.详细说明如下 ...
- js 根据数组分组动态生成table(相同项合并)
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/ ...
- 微信公众号:1-IDHTTP控件:GET/POST 请求获取access_token
(图来源于方蓓?) 首先要理解公众号的流程.通过图知道,我们要:1.你要有个web服务器,用于和微信服务器通讯.你的web服务器必须让微信服务器能找到.2.通信要求按照微信公众号开发要求的格式提供相关 ...
- jQuery学习----简单介绍,基本使用,操作样式,动画
jQuery简单介绍 jq是js的插件库,说白了,jq就是一个js文件 凡事能用jq实现的,js都能实现.但是js能实现的,jq不一定能够实现 jq的引入 http://www.bootcdn.cn ...
- 费用最少的一款赛门铁克SSL证书
Symantec Secure Site SSL证书,验证域名所有权和企业信息,属于Symantec Class 3企业(OV)验证 级SSL证书,为40位/56位/128/256位自适应加密,目前连 ...
- [bzoj2654]tree_二分_kruskal
tree bzoj-2654 题目大意:给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树.题目保证有解. 注释:$1\le V\le 5\cdot 10^4 ...
- spring boot.定时任务问题记录(TaskScheduler/ScheduledExecutorService异常)
一.背景 spring boot的定时任务非常简单,只需要在启动类中加上@EnableScheduling注解,然后在对应的方法上配置@Scheduled就可以了,系统会自动处理并按照Schedule ...