代码片段:

 package org.yu.units;

 import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; /**
* @author Hai E-mail:256051@qq.com
* @version 创建时间:2017年10月20日 上午10:30:03 类说明
*/
/**
* @author HH
*
*/
public class zipFile { public static void main(String... args) {
extractZipFile("e:\\xx\\nbproject.zip","e:\\xx", true);
} public static boolean extractZipFile(String zipFilePath, String path, boolean overwrite) {
return extractZipFile(new File(zipFilePath), path, overwrite);
} public static boolean extractZipFile(File zipFilePath, String destDirectory, boolean overwrite) {
InputStream inputStream = null;
ZipInputStream zipInputStream = null;
boolean status = true; try {
inputStream = new FileInputStream(zipFilePath); zipInputStream = new ZipInputStream(inputStream);
final byte[] data = new byte[1024]; while (true) {
ZipEntry zipEntry = null;
FileOutputStream outputStream = null; try {
zipEntry = zipInputStream.getNextEntry(); if (zipEntry == null) {
break;
} final String destination;
if (destDirectory.endsWith(File.separator)) {
destination = destDirectory + zipEntry.getName();
} else {
destination = destDirectory + File.separator + zipEntry.getName();
} if (overwrite == false) {
if (isFileOrDirectoryExist(destination)) {
continue;
}
} if (zipEntry.isDirectory()) {
createCompleteDirectoryHierarchyIfDoesNotExist(destination);
} else {
final File file = new File(destination);
// Ensure directory is there before we write the file.
createCompleteDirectoryHierarchyIfDoesNotExist(file.getParentFile()); int size = zipInputStream.read(data); if (size > 0) {
outputStream = new FileOutputStream(destination); do {
outputStream.write(data, 0, size);
size = zipInputStream.read(data);
} while (size >= 0);
}
}
} catch (IOException exp) {
exp.printStackTrace();
status = false;
break;
} finally {
close(outputStream);
closeEntry(zipInputStream);
} } // while(true)
} catch (IOException exp) {
exp.printStackTrace();
status = false;
} finally {
close(zipInputStream);
close(inputStream);
}
return status;
} public static boolean createCompleteDirectoryHierarchyIfDoesNotExist(String directory) {
return createCompleteDirectoryHierarchyIfDoesNotExist(new File(directory));
} private static boolean createCompleteDirectoryHierarchyIfDoesNotExist(File f) {
if (f == null)
return true; if (false == createCompleteDirectoryHierarchyIfDoesNotExist(f.getParentFile())) {
return false;
} final String path = f.getAbsolutePath(); return createDirectoryIfDoesNotExist(path);
} private static boolean createDirectoryIfDoesNotExist(String directory) {
java.io.File f = new java.io.File(directory); if (f.exists() == false) {
if (f.mkdir()) {
return true;
} else {
return false;
}
} return true;
} /**
* Performs close operation on Closeable stream, without the need of
* writing cumbersome try...catch block.
*
* @param closeable The closeable stream.
*/
public static void close(Closeable closeable) {
// Instead of returning boolean, we will just simply swallow any
// exception silently. This is because this method will usually be
// invoked within finally block. If we are having control statement
// (return, break, continue) within finally block, a lot of surprise may
// happen.
// http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java
if (null != closeable) {
try {
closeable.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} /**
* Performs close operation on ZIP input stream, without the need of
* writing cumbersome try...catch block.
*
* @param zipInputStream The ZIP input stream.
*/
public static void closeEntry(ZipInputStream zipInputStream) {
// Instead of returning boolean, we will just simply swallow any
// exception silently. This is because this method will usually be
// invoked within finally block. If we are having control statement
// (return, break, continue) within finally block, a lot of surprise may
// happen.
// http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java
if (null != zipInputStream) {
try {
zipInputStream.closeEntry();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} public static boolean isFileOrDirectoryExist(String fileOrDirectory) {
java.io.File f = new java.io.File(fileOrDirectory);
return f.exists();
}
}

JAVA如何解压缩ZIP文档的更多相关文章

  1. 笔记:I/O流-ZIP文档

    ZIP文档以压缩格式存储了一个或多个文件,每个ZIP文档都有一个头,包含诸如每个文件名字和所使用的压缩方法等信息,在 Java 中可以使用 ZipInputStream 来读入ZIP 文档,getNe ...

  2. IO流-ZIP文档

    java中通常使用ZipInputStream来读ZIP文档 ZIP文档(通常)以压缩格式存储了一个或多个文件,每个ZIP文档都有一个包含诸如文件 名字和所使用的压缩方法等信息的头.在Java中,可以 ...

  3. Java 后台创建word 文档

    ---恢复内容开始--- Java 后台创建 word 文档 自己总结  网上查阅的文档 分享POI 教程地址:http://www.tuicool.com/articles/emqaEf6 方式一. ...

  4. I/O流、ZIP文档

    1) ZIP文档通常以压缩格式存储一个或多个文档.在Java中可以用ZipInputStream读入ZIP文档(即解压文件流),用ZipOutputStream写入ZIP文档(即压缩文件流),无论解压 ...

  5. 【.NET深呼吸】Zip文件操作(2):动态生成Zip文档

    通过前面一篇烂文的介绍,大伙儿知道,ZipArchive类表示一个zip文档实例,除了用上一篇文章中所列的方法来读写zip文件外,还可以直接通过ZipArchive类,动态生成zip文件. 文件流操作 ...

  6. 【.NET深呼吸】Zip文件操作(1):创建和读取zip文档

    .net的IO操作支持对zip文件的创建.读写和更新.使用起来也比较简单,.net的一向作风,东西都准备好了,至于如何使用,请看着办. 要对zip文件进行操作,主要用到以下三个类: 1.ZipFile ...

  7. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

  8. Java解析word,获取文档中图片位置

    前言(背景介绍): Apache POI是Apache基金会下一个开源的项目,用来处理office系列的文档,能够创建和解析word.excel.ppt格式的文档. 其中对word文档的处理有两个技术 ...

  9. 《Java开发学习大纲文档》V7.0

    <Java开发学习大纲文档>V7.0简介: 本文档是根据企业开发所需要掌握的知识点大纲进行总结汇编,是Java开发工程师必备知识体系,系统化学习针对性非常强,逻辑分析能力非常清晰;技术方面 ...

随机推荐

  1. mongodb 正则

    正则表达式常用来在所有语言中搜索字符串的任何模式或文字.MongoDB还提供了正则表达式功能的字符串模式使用正则表达式$regex操作符.MongoDB使用PCRE(Perl兼容正则表达式)为正则表达 ...

  2. drupal基本知识介绍

    2. Drupal 安装在安装Drupal前,你需要在服务器上先搭建一个PHP+MySQL环境.专业网站一般是安装LAMP(Linux+Apache+MySQL+PHP).环境的搭建可参考如下文章:  ...

  3. STL使用迭代器逆向删除

    网上有很多这种例子: void erase(vector<int> &v) { for(vector<int>::reverse_iterator ri=v.rbegi ...

  4. [BZOJ2434][Noi2011]阿狸的打字机 AC自动机+树状数组+离线

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2434 题目中这种多个串匹配的问题,一下子就想到了AC自动机.然后发现如果要建立AC自动机, ...

  5. java.lang.IllegalAccessException: Class XX can not access a member of class XXX with modifiers "private static"

    当前需求: 利用反射获取某一属性值运行结果:java.lang.IllegalAccessException: Class com.example.demo.test.Reflect can not ...

  6. C# for循环的嵌套 作用域

    for() {   循环体可以套无数个for循环 } 比如:for() { for() { for() {... ...这里面可以镶嵌无数个for循环} } } 也可以这样 for() { for() ...

  7. 【春节版】年度精品 XP,32/64位Win7,32/64位Win8,32/64位Win10系统

    本系统是10月5日最新完整版本的Windows10 安装版镜像,win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为win10 Edge浏览器中国默认主页和搜索引擎 ...

  8. 洛谷 P2353 背单词

    题目背景 小明对英语一窍不通,令老师十分头疼.于是期末考试前夕,小明被逼着开始背单词…… 题目描述 老师给了小明一篇长度为N的英语文章,然后让小明背M个单词.为了确保小明不会在背单词时睡着,老师会向他 ...

  9. 当互联网遇上家装,十大家装O2O混战

    2015年已过去大半,装修O2O就出现了新的局面:为数众多的家居网络平台在家装O2O领域还未站稳脚跟,新的入局者就打出超低价格登场.新老O2O家装大战迅速展开,除了拼价格还拼品牌和体验,家装O2O的好 ...

  10. 数据库_8_SQL基本操作——数据操作

    SQL基本操作——数据操作 一.新增数据(两种方案) 方案1: 给全表字段插入数据,不需要指定字段列表,要求数据的值出现的顺序必须与表中设计的字段出现的顺序一致,凡是非数值数据,都需要使用引号(建议是 ...