代码片段:

 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. 16年毕业的前端er在杭州求职ing

    来杭州也有一两个星期了,这个周末下雨,是在没地去,还是习惯性的打开电脑逛技术论坛,想想也是好久没有更新博文了... 背景 因为曾经看过一篇文章面试分享:一年经验初探阿里巴巴前端社招所以来杭州也是带有目 ...

  2. 自定义消息中如果需要定义WPARAM和LPARAM,该怎么使用和分配?

    写Windows程序不可避免要使用自定义的消息,也就是从WM_USER开始定义的消息.在定义一个消息后,往往我们还要定义针对该消息的WPARAM甚至是LPARAM.WPARAM和LPARAM是什么,可 ...

  3. 图片,二进制,oracle数据库

    图片在oracle数据库中一般以二进制存在,存储类型是blob,然而clob类型一般存储的是大于4000的字符,不能用来存储图像这样的二进制内容,下面展示一下实现图像,二进制,oracle 数据库的应 ...

  4. kafka 安装以及测试

    1,下载kafka 并进行解压 http://mirrors.cnnic.cn/apache/kafka/0.8.1.1/kafka_2.9.2-0.8.1.1.tgz 2,启动Zookeeper  ...

  5. eclipse中Lombok注解无效

    问题现象:eclipse中使用lombok的@Date,引用get方法时,报错. 解决方案: 在lombok官网(https://www.projectlombok.org/download)下载,或 ...

  6. 第二次团队作业-PANTHER考勤系统需求分析

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 这个作业要求在哪里 https://edu.cnblo ...

  7. Codeforces Round #277.5 (Div. 2)-A. SwapSort

    http://codeforces.com/problemset/problem/489/A A. SwapSort time limit per test 1 second memory limit ...

  8. 解决VS2013无法安装ArcObjects10.2的问题

    之前在网上看到的10.1在vs2012安装不上的问题,解决办法是改注册表HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\1 ...

  9. 697. Degree of an Array@python

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

  10. 【技巧:字符串同构】Avendesora

    判断字符串“同构”的技巧 题目大意 给定A,B两个序列,要求B在A中出现的次数以及位置.定义字符变换:把所有相同的字符变为另一种字符:两个字符串相等:当且仅当一个字符串可以在若干次字符变换之后变为另一 ...