代码片段:

 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. VUE学习,vue运行环境搭建遇见的小问题

    1.使用vscode来编辑项目,那么首先给它搭一个vue运行的环境,打开集成终端,使用npm install live-server -g安装live-server. problem1:cmd终端分行 ...

  2. openssl安装介绍

    #因CentOS7默认安装了openssl1.0版本,需要删除该版本,才能安装openssl.1.0.2l版本yum remove -y openssl openssl-devel cd /usr/l ...

  3. perl在linux下通过date获取当前时间

    perl处理文件的时候最好添加上 处理的时间戳,获取系统的时间又多种方法,但是反引号是最原始的,不需要其他外界条件和lib的支持. my $now = `date "+%F %T" ...

  4. hihoCoder #1068 : RMQ-ST算法(模板)

    AC G++ 826ms 146MB 思路: 时间复杂度O(nlogn). //#include <bits/stdc++.h> #include <iostream> #in ...

  5. 仿天猫淘宝的ShopNC好商城原生Android 客户端源码项目

    开发环境:Android Studio 2.0 | Gradle 2.0.0最后更新:2016-04-28 简介:基于好商城V4的Android客户端 目前已完成的功能(概述): 1.启动页 -> ...

  6. Mac终端给命令设置别名alias的办法

    在Mac里使用curl https://www.google.com,运行后得不到期望看到的google首页的HTML source code. vi ~/.bashrc, 输入下面两行内容. 以后每 ...

  7. 推荐一个markdown格式转html格式的开源JavaScript库

    这个markdown格式转html格式的开源JavaScript库在github上的地址: https://github.com/millerblack/markdown-js 从markdown 格 ...

  8. jquery的load方法

    load方法指定一个界面会显示在目标的标签内部 比如MVC的一个分部视图页面想要显示在某个标签里面,可以写成 $(标签ID).load(分部视图名称,data) 其中第二个参数可选,主要是一些需要传递 ...

  9. 从汇编看c++中临时对象的析构时机

    http://www.cnblogs.com/chaoguo1234/archive/2013/05/12/3074425.html c++中,临时对象一旦不需要,就会调用析构函数,释放其占有的资源: ...

  10. urllib基础-利用网站结构爬取网页-百度搜索

    有的时候爬取网页,可以利用网站额结构特点爬取网页 在百度搜索框中输入搜索内容,单击搜索,浏览器会发送一个带有参数的url请求.尝试删除其中的一些参数,只剩下wd这个参数.发现wd是搜索内容.这样程序可 ...