代码片段:

 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. 重置Mysql的root密码及用户权限设置

     一.重置Mysql的root密码 方法一: 直接进入localhost/phpmyadmin修改用户root的权限,设置密码: 方法二: 进入mysql控制台:mysql->use mysql ...

  2. Luogu P5327 [ZJOI2019]语言

    ZJOI2019Day2的温暖题,然后考场上只会大常数的\(O(n\log^3 n)\),就懒得写拿了60pts走人 首先我们简化题意,容易发现每个点能到达的点形成了一个联通块,我们只需要统计出这个联 ...

  3. pb2.text_format.Merge(f.read(), self.solver_param) AttributeError: 'module' object has no attribute 'text_format'

    http://blog.csdn.net/qq_33202928/article/details/72526710

  4. linux下怎么修改mysql的字符集编码

    安装完的MySQL的默认字符集为 latin1 ,为了要将其字符集改为用户所需要的(比如utf8),就必须改其相关的配置文件:由于linux下MySQL的默认安装目录分布在不同的文件下:不像windo ...

  5. hibernate4+spring3+struts2搭建框架实例

    1.所需要的JAR包 2.web.xml配置文件,这个和平时的配置是一样的 <?xml version="1.0" encoding="UTF-8"?&g ...

  6. Codeforces 1012A Photo of The Sky

    作为一个蒟蒻,\(\tt{CF}\)止步\(Div.2\;C\) 这个题主要考察思维,正解代码炒鸡短-- 以下大部分搬运自官方题解 题目大意: 给你一段长度为\(2n\)的数列,将这个数列分为两个可重 ...

  7. Spring根据XML配置文件注入对象类型属性

    这里有dao.service和Servlet三个地方 通过配过文件xml生成对象,并注入对象类型的属性,降低耦合 dao文件代码: package com.swift; public class Da ...

  8. CentOS7支持中文显示

    1.查看系统是否安装有中文语言包 locale -a | grep "zh_CN"     命令含义:列出所有可用的公共语言环境的名称,包含有"zh_CN" 若 ...

  9. (6)zabbix主机与组配置

    1. 创建主机方法 1.1 新建主机configuration(配置)->Hosts(主机)->Create host(创建主机) 见前面的博文 1.2 克隆/完全克隆主机 2. 主机参数 ...

  10. linux各种终端类型的区别和概念

    1 pty(虚拟终端或伪终端): 当我们远程telnet到主机或使用xterm时不也需要一个终端交互么?是的,这就是虚拟终端pty(pseudo-tty). 2 tty(终端设备的统称):tty一词源 ...