代码片段:

 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. springMVC 与 struts+hibernate+spring优缺点

    springMVC: Spring 框架是高度可配置的,而且包含多种视图技术,例如 JavaServer Pages(JSP)技术.Velocity.Tiles.iText 和POI.Spring M ...

  2. C语言abort函数

    C语言编程入门教程,C语言库函数的abort函数的作用是异常终止一个进程,意味着abort后面的代码将不再执行. #include<stdio.h> #include<stdlib. ...

  3. Android Studio 遇到的java.util.concurrent.ExecutionException:com.android.ide.common.process.ProcessExce问题

    在将一个Eclipse的项目转移到AndroidStudio的过程中,碰到了的问题如下: Error:Execution failed for task ':learnChinese:mergeDeb ...

  4. 迅为4412开发板Linux设备树的镜像烧写和源码简单优化教程

    1 烧写:   烧写和4412默认镜像的烧写类似,使用fastboot. 先更新uboot,用4412默认uboot更新支持设备树的uboot 用支持设备树的uboot烧写. 进入支持设备树的uboo ...

  5. 贴一发STL源码

    int my_lower_bound(int size, long long key){    int first = 0, middle;    int half, len;    len = si ...

  6. 换个语言学一下 Golang (5)——运算符

    运算符用于在程序运行时执行数学或逻辑运算. Go 语言内置的运算符有: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 接下来让我们来详细看看各个运算符的介绍. 算术运算符 下表 ...

  7. django 第一次运行出错

    直接运行整个项目正常,直接运行url文件报错 报错内容: E:\Python\python.exe D:/Python储存文件/ceshiweb/ceshiweb/urls.pyTraceback ( ...

  8. Ubuntu 18.04 上使用 OpenJDK 安装并运行 Tomcat

    在Linux上安装与卸载JDK和JRE,两种常用方法: 一.通过 apt-get 命令在线进行安装与卸载(会自动配置好环境变量) 二.通过下载并解压 .tar.gz 包进行手动安装与手动卸载(需要手动 ...

  9. shell脚本,awk实现行列转换

    [root@localhost study]# cat file 张三 语文 张三 数学 李四 语文 李四 数学 王五 语文 王五 数学 王五 英语 怎么实现为下面的排序??? 语文 数学 语文 数学 ...

  10. 关于Java IO流学习总结

    一.IO流的三种分类方式 1.按流的方向分为:输入流和输出流 2.按流的数据单位不同分为:字节流和字符流 3.按流的功能不同分为:节点流和处理流     二.IO流的四大抽象类: 字符流:Reader ...