一、简述

  解压技术和压缩技术正好相反,解压技术要用到的类:由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码,如:

CheckedInputStream cis = new CheckedInputStream(new FileInputStream(
srcFile), new CRC32()); ZipInputStream zis = new ZipInputStream(cis);

  需要注意的是,在构建解压文件时,需要考虑目录的自动创建,这里通过递归方式逐层创建父目录,如下所示:

//当父目录不存在时,创建目录! 
private static void fileProber(File dirFile) {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) { // 递归寻找上级目录
fileProber(parentFile);
parentFile.mkdir();
}
}

在压缩的时候,我们是将一个一个文件作为压缩添加项(ZipEntry)添加至压缩包中,解压缩就要将一个一个压缩项从压缩包中提取出来,如下所示:

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, , BUFFER)) != -) {
bos.write(data, , count);
}
bos.close();
}

完整的例子:

package com.joyplus.test;
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.ZipEntry;
import java.util.zip.ZipInputStream; /**
*
* @author
*
*/
public class zipFiletest2 {
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();
}
public static void main(String[] args) {
// 解压到指定目录
try {
zipFiletest2.decompress("D:\\sumZip\\co.zip", "D:\\log");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}

  

参考文章:http://snowolf.iteye.com/blog/642492

java.util.zip压缩打包文件总结二: ZIP解压技术的更多相关文章

  1. java.util.zip压缩打包文件总结一:压缩文件及文件下面的文件夹

    一.简述 zip用于压缩和解压文件.使用到的类有:ZipEntry  ZipOutputStream 二.具体实现代码 package com.joyplus.test; import java.io ...

  2. python用zipfile模块打包文件或是目录、解压zip文件实例

    #!/usr/bin/env python # -*- coding: utf-8 -*- from zipfile import * import zipfile #解压zip文件 def unzi ...

  3. Linux tar (打包.压缩.解压缩)命令说明 | tar如何解压文件到指定的目录?

    打包举例:将 /usr/local/src/zlib-1.2.5目录下的文件打包成 zlib-1.2.5.tar.gz cd /usr/local/src tar -czvf ./zlib-1.2.5 ...

  4. java jar包解析:打包文件,引入文件

    java jar包解析:打包文件,引入文件 cmd下: jar命令:package包打包 javac命令:普通类文件打包 Hello.java: package org.lxh.demo; publi ...

  5. php读取excel,以及php打包文件夹为zip文件

    1.把文件下载到本地,放在在Apache环境下2.d.xlsx是某游戏的服务器名和玩家列表,本程序只适合此种xlsx文件结构,其他结构请修改index.php源码3.访问zip.php的功能是把生成的 ...

  6. java打包文件夹为zip文件

    //待压缩的文件目录 String sourceFile=sourceFilePath+"\\"+userName; //存放压缩文件的目录 String zipFilePath ...

  7. PHP实现zip压缩打包下载

    先来看PHP实现文件及文件夹的zip压缩 这里使用PHP扩展的ZipArchive类,在使用之前要将php.ini文件中的zlib.output_compression设置为On 代码如下: publ ...

  8. [转]C#压缩打包文件

    /// <summary> /// 压缩和解压文件 /// </summary> public class ZipClass { /// <summary> /// ...

  9. asp.net C#压缩打包文件例子

    /// <summary> /// 压缩和解压文件 /// </summary> public class ZipClass { /// <summary> /// ...

随机推荐

  1. HDU-1253 胜利大逃亡 (BFS)

    此题可以做为三维深搜模板题.. 胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  2. javascript 和jqurry详解

    javascript写的图表库,收费 highcharts jqurry有两个图标函数不收费,好用 Flot.PlotKit与MochiKit 官网下载 http://www.flotcharts.o ...

  3. JavaScript高级程序设计36.pdf

    TreeWalker TreeWalker是NodeIterator的更高级的版本,除了包括nextNode()和previousNode()在内的相同功能外,这个类型还提供了用于不同方向上遍历DOM ...

  4. 5 个在 Linux 中管理文件类型和系统时间的有用命令

    对于想学习 Linux 的初学者来说要适应使用命令行或者终端可能非常困难.由于终端比图形用户界面程序更能帮助用户控制 Linux 系统,我们必须习惯在终端中运行命令.因此为了有效记忆 Linux 不同 ...

  5. 【转】谁说Vim不是IDE?(二)

    谁说Vim不是IDE?(二)   环境配置 “如果你认为Vim只是一个文本编辑器,你就输了”——来自Vim老鸟 Vim以简洁的方式提供了丰富的配置功能,主要配置体系由一个文件和文件夹组成.在一台安装了 ...

  6. Gradle DSL method found: ‘android()’错误

    Gradle DSL method found: ‘android()’错误 和上个错误一样这个也是因为在新版本的Gradle中android()方法已经废弃,但是要注意android()只是在整个项 ...

  7. kafka broker 进入 conflicted ephemeral node 死循环

    转载请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/5621303.html 最近发现kafka一台服务器producer客户端写入时一直报错,查看该br ...

  8. SecureCRT 无法删除字符

    1. 2.

  9. 【OpenCV】基于kmeans的细胞检测方法

    问题是这样的,有一幅经过二值化处理之后的图像,我们希望统计其中细胞的个数,和不同粘连情况的细胞个数,比如,下图中有1个细胞组成连通区域的,也有2个细胞组成连通区域的,也有更多个细胞组成连通区域的,我们 ...

  10. ffmpeg的logo, delogo滤镜参数设置

    FFmpeg的添加logo,去logo滤镜的组合共有三种方式: 1. 只有添加logo滤镜 $ ./ffmpeg -i INPUT.FLV  \ -vf movie=/opt/logo.png[log ...