- 浏览: 129614 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (165)
- 数据库 (45)
- 架构 (0)
- java web前端+框架 (9)
- java web后端+框架 (56)
- Eclipse插件 (1)
- 解题思路 (2)
- Eclipse (2)
- linux (6)
- jquery (4)
- 正则 (3)
- jsp (1)
- javascript (8)
- oracle (39)
- 生活 (1)
- weblogic (5)
- tomcat (3)
- Jrebel (1)
- powerdesigner (1)
- svn (1)
- log4j (1)
- IDE (1)
- POI (2)
- jvm (2)
- ssh (1)
- http (1)
- notepad++ (2)
- 润乾 (1)
- 设计模式 (0)
- 实用类Utils (2)
- 算法 (1)
- xml (1)
- 实用类 (2)
最新评论
/**
* Utility to Zip and Unzip nested directories recursively.
*
* @author Robin Spark
*/
public class ZipUtils {
private static Logger logger = LoggerFactory.getLogger(ZipUtils.class);
/**
* Creates a zip file at the specified path with the contents of the
* specified directory. NB:
*
* @param sourceDirectoryPath
* The path of the directory where the archive will be created.
* eg. c:/temp
* @param targetZipFilePath
* The full path of the archive to create. eg.
* c:/temp/archive.zip
* @throws IOException
* If anything goes wrong
*/
public static void createZip(
String sourceDirectoryPath,
String targetZipFilePath) throws IOException
{
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
ZipArchiveOutputStream tOut = null;
try {
fOut = new FileOutputStream(new File(targetZipFilePath));
bOut = new BufferedOutputStream(fOut);
tOut = new ZipArchiveOutputStream(bOut);
addFileToZip(tOut, sourceDirectoryPath, "");
} finally {
tOut.finish();
tOut.close();
bOut.close();
fOut.close();
}
}
public static void createZip(
List<File> fileList,
String targetZipFilePath) throws IOException
{
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
ZipArchiveOutputStream tOut = null;
try {
fOut = new FileOutputStream(new File(targetZipFilePath));
bOut = new BufferedOutputStream(fOut);
tOut = new ZipArchiveOutputStream(bOut);
addFileToZip(tOut, fileList);
} finally {
tOut.finish();
tOut.close();
bOut.close();
fOut.close();
}
}
public static void createZip(
Map<String, File> fileMap,
String targetZipFilePath) throws Exception
{
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
ZipArchiveOutputStream tOut = null;
try {
fOut = new FileOutputStream(new File(targetZipFilePath));
bOut = new BufferedOutputStream(fOut);
tOut = new ZipArchiveOutputStream(bOut);
addFileToZip(tOut, fileMap);
} finally {
String encoding = tOut.getEncoding();
if (logger.isDebugEnabled()) {
logger.debug("tOut encoding:" + encoding);
}
if (logger.isDebugEnabled()) {
logger.debug("tOut.finish()---begin");
}
tOut.finish();
if (logger.isDebugEnabled()) {
logger.debug("tOut.finish()---end");
}
tOut.close();
bOut.close();
fOut.close();
}
}
private static void addFileToZip(
ZipArchiveOutputStream zOut,
List<File> fileList) throws IOException
{
for (File f : fileList) {
String entryName = f.getName();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);
zOut.putArchiveEntry(zipEntry);
FileInputStream fInputStream = null;
try {
fInputStream = new FileInputStream(f);
IOUtils.copy(fInputStream, zOut);
zOut.closeArchiveEntry();
} catch (Exception e) {
e.printStackTrace();
} finally {
// fInputStream.close();
org.apache.commons.io.IOUtils.closeQuietly(fInputStream);
}
}
}
private static void addFileToZip(
ZipArchiveOutputStream zOut,
Map<String, File> fileMap) throws Exception
{
if (MapUtils.isEmpty(fileMap)) {
return;
}
for (Entry<String, File> entry : fileMap.entrySet()) {
String entryName = entry.getKey();
File f = entry.getValue();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);
zOut.putArchiveEntry(zipEntry);
if (logger.isDebugEnabled()) {
logger.debug("put zipEntry:" + zipEntry.getName());
}
FileInputStream fInputStream = null;
try {
fInputStream = new FileInputStream(f);
IOUtils.copy(fInputStream, zOut);
zOut.closeArchiveEntry();
if (logger.isDebugEnabled()) {
logger.debug("close zipEntry:" + zipEntry.getName());
}
} catch (Exception e) {
throw e;
} finally {
fInputStream.close();
// org.apache.commons.io.IOUtils.closeQuietly(fInputStream);
}
}
}
/**
* Creates a zip entry for the path specified with a name built from the
* base passed in and the file/directory name. If the path is a directory, a
* recursive call is made such that the full directory is added to the zip.
*
* @param zOut
* The zip file's output stream
* @param path
* The filesystem path of the file/directory being added
* @param base
* The base prefix to for the name of the zip file entry
*
* @throws IOException
* If anything goes wrong
*/
private static void addFileToZip(
ZipArchiveOutputStream zOut,
String path,
String base) throws IOException
{
File f = new File(path);
String entryName = base + f.getName();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);
zOut.putArchiveEntry(zipEntry);
if (f.isFile()) {
FileInputStream fInputStream = null;
try {
fInputStream = new FileInputStream(f);
IOUtils.copy(fInputStream, zOut);
zOut.closeArchiveEntry();
} finally {
fInputStream.close();
}
} else {
zOut.closeArchiveEntry();
File[] children = f.listFiles();
if (children != null) {
for (File child : children) {
addFileToZip(zOut, child.getAbsolutePath(), entryName + "/");
}
}
}
}
/**
* Method for testing zipping and unzipping.
*
* @param args
*/
public static void main(
String[] args) throws Exception
{
// ZipUtils.createZip("c:/MyFile", "C:/解压缩MyFile-123.zip");
// List<File> fileList = new ArrayList<File>();
//
// fileList.add(new File("C:\\MyFile\\spy.log"));
//
// fileList.add(new File("C:\\MyFile\\中华人民共和国.log"));
Map<String, File> fileMap = new HashMap<String, File>();
fileMap.put("xxx.TXT", new File("C:\\MyFile\\spy.log"));
fileMap.put("yyy.LOG", new File("C:\\MyFile\\中华人民共和国.log"));
ZipUtils.createZip(fileMap, "C:/解压缩MyFile-12345.zip");
System.out.println("Done...");
}
}
* Utility to Zip and Unzip nested directories recursively.
*
* @author Robin Spark
*/
public class ZipUtils {
private static Logger logger = LoggerFactory.getLogger(ZipUtils.class);
/**
* Creates a zip file at the specified path with the contents of the
* specified directory. NB:
*
* @param sourceDirectoryPath
* The path of the directory where the archive will be created.
* eg. c:/temp
* @param targetZipFilePath
* The full path of the archive to create. eg.
* c:/temp/archive.zip
* @throws IOException
* If anything goes wrong
*/
public static void createZip(
String sourceDirectoryPath,
String targetZipFilePath) throws IOException
{
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
ZipArchiveOutputStream tOut = null;
try {
fOut = new FileOutputStream(new File(targetZipFilePath));
bOut = new BufferedOutputStream(fOut);
tOut = new ZipArchiveOutputStream(bOut);
addFileToZip(tOut, sourceDirectoryPath, "");
} finally {
tOut.finish();
tOut.close();
bOut.close();
fOut.close();
}
}
public static void createZip(
List<File> fileList,
String targetZipFilePath) throws IOException
{
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
ZipArchiveOutputStream tOut = null;
try {
fOut = new FileOutputStream(new File(targetZipFilePath));
bOut = new BufferedOutputStream(fOut);
tOut = new ZipArchiveOutputStream(bOut);
addFileToZip(tOut, fileList);
} finally {
tOut.finish();
tOut.close();
bOut.close();
fOut.close();
}
}
public static void createZip(
Map<String, File> fileMap,
String targetZipFilePath) throws Exception
{
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
ZipArchiveOutputStream tOut = null;
try {
fOut = new FileOutputStream(new File(targetZipFilePath));
bOut = new BufferedOutputStream(fOut);
tOut = new ZipArchiveOutputStream(bOut);
addFileToZip(tOut, fileMap);
} finally {
String encoding = tOut.getEncoding();
if (logger.isDebugEnabled()) {
logger.debug("tOut encoding:" + encoding);
}
if (logger.isDebugEnabled()) {
logger.debug("tOut.finish()---begin");
}
tOut.finish();
if (logger.isDebugEnabled()) {
logger.debug("tOut.finish()---end");
}
tOut.close();
bOut.close();
fOut.close();
}
}
private static void addFileToZip(
ZipArchiveOutputStream zOut,
List<File> fileList) throws IOException
{
for (File f : fileList) {
String entryName = f.getName();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);
zOut.putArchiveEntry(zipEntry);
FileInputStream fInputStream = null;
try {
fInputStream = new FileInputStream(f);
IOUtils.copy(fInputStream, zOut);
zOut.closeArchiveEntry();
} catch (Exception e) {
e.printStackTrace();
} finally {
// fInputStream.close();
org.apache.commons.io.IOUtils.closeQuietly(fInputStream);
}
}
}
private static void addFileToZip(
ZipArchiveOutputStream zOut,
Map<String, File> fileMap) throws Exception
{
if (MapUtils.isEmpty(fileMap)) {
return;
}
for (Entry<String, File> entry : fileMap.entrySet()) {
String entryName = entry.getKey();
File f = entry.getValue();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);
zOut.putArchiveEntry(zipEntry);
if (logger.isDebugEnabled()) {
logger.debug("put zipEntry:" + zipEntry.getName());
}
FileInputStream fInputStream = null;
try {
fInputStream = new FileInputStream(f);
IOUtils.copy(fInputStream, zOut);
zOut.closeArchiveEntry();
if (logger.isDebugEnabled()) {
logger.debug("close zipEntry:" + zipEntry.getName());
}
} catch (Exception e) {
throw e;
} finally {
fInputStream.close();
// org.apache.commons.io.IOUtils.closeQuietly(fInputStream);
}
}
}
/**
* Creates a zip entry for the path specified with a name built from the
* base passed in and the file/directory name. If the path is a directory, a
* recursive call is made such that the full directory is added to the zip.
*
* @param zOut
* The zip file's output stream
* @param path
* The filesystem path of the file/directory being added
* @param base
* The base prefix to for the name of the zip file entry
*
* @throws IOException
* If anything goes wrong
*/
private static void addFileToZip(
ZipArchiveOutputStream zOut,
String path,
String base) throws IOException
{
File f = new File(path);
String entryName = base + f.getName();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);
zOut.putArchiveEntry(zipEntry);
if (f.isFile()) {
FileInputStream fInputStream = null;
try {
fInputStream = new FileInputStream(f);
IOUtils.copy(fInputStream, zOut);
zOut.closeArchiveEntry();
} finally {
fInputStream.close();
}
} else {
zOut.closeArchiveEntry();
File[] children = f.listFiles();
if (children != null) {
for (File child : children) {
addFileToZip(zOut, child.getAbsolutePath(), entryName + "/");
}
}
}
}
/**
* Method for testing zipping and unzipping.
*
* @param args
*/
public static void main(
String[] args) throws Exception
{
// ZipUtils.createZip("c:/MyFile", "C:/解压缩MyFile-123.zip");
// List<File> fileList = new ArrayList<File>();
//
// fileList.add(new File("C:\\MyFile\\spy.log"));
//
// fileList.add(new File("C:\\MyFile\\中华人民共和国.log"));
Map<String, File> fileMap = new HashMap<String, File>();
fileMap.put("xxx.TXT", new File("C:\\MyFile\\spy.log"));
fileMap.put("yyy.LOG", new File("C:\\MyFile\\中华人民共和国.log"));
ZipUtils.createZip(fileMap, "C:/解压缩MyFile-12345.zip");
System.out.println("Done...");
}
}
发表评论
-
eclipse java代码格式化 javaformatter20150123.zip
2015-01-23 11:30 712eclipse java代码格式化 javaformatter ... -
List to Map
2015-01-21 17:22 484/** * <p>将List变为Map</p ... -
读取access工具类-ReadAccessUtil2
2015-01-06 13:52 551import com.healthmarketscience. ... -
ParameterizedType获取java泛型参数类型
2014-11-14 15:15 700ParameterizedType获取java泛型参数类型 ... -
分页帮助类-oracle-java
2014-10-09 17:46 459/** * 分页帮助类 * * @author ver ... -
java自动实例化List AutoArrayList
2014-07-21 15:51 739import java.util.ArrayList; p ... -
单一登录过滤器
2014-06-20 17:13 397/** * 单一登录过滤器 * * @author v ... -
正则 java 查找 打印 所有匹配项
2014-06-18 11:26 641package com.test.regex; import ... -
第3方包读取Access
2014-05-26 16:07 667package com.test; import java. ... -
多线程加签验签例子.zip
2014-05-26 16:08 512多线程加签验签例子.zip -
DateTimeUtils
2014-05-26 16:08 651import java.text.DateFormat; im ... -
listToMap
2014-05-26 16:09 437/** * List实用类 * * @author v ... -
JdbcTemplateRowMapper
2015-05-19 15:44 575import java.lang.reflect.Constr ... -
KeyValuePair2
2015-05-19 15:44 508/** * 键值对 * * @author verno ... -
OrderUtil -排序工具
2014-05-22 09:35 660/** * 排序工具 * * @author vern ... -
ZipUtils
2014-05-22 09:35 445import java.io.BufferedOutputSt ... -
TimerUtils
2014-05-22 09:34 479import org.slf4j.Logger; import ... -
Excel View 4 Spring
2014-05-22 09:29 339Excel View 4 Spring @RequestMa ... -
DOMUtils xml操作实用类
2014-04-07 21:41 804/* * Copyright 2001-2004 The A ... -
生成验证码 数学+字母
2014-04-01 10:08 440public static String getCharAnd ...
相关推荐
ZipUtils是一个工具类库,专门用于执行这样的任务。本文将深入探讨ZipUtils的功能、工作原理以及如何在实际开发中使用它。 ### 1. 压缩与解压缩基本概念 **压缩** 是将文件或文件夹转换为更小的存储格式的过程,...
在Java编程语言中,`ZipUtils`通常是一个自定义工具类,用于处理ZIP文件的压缩与解压缩操作。ZIP文件格式是一种广泛使用的归档格式,它允许将多个文件和目录打包到一个单一的ZIP文件中,便于存储和传输。本文将深入...
本篇将详细讲解如何使用Java来创建ZIP格式的文件,主要关注`ZipUtils.java`这个类的实现。 ZIP格式是一种流行的文件归档格式,它允许我们将多个文件和目录压缩到一个单一的文件中,从而节省存储空间并方便传输。在...
【ZipUtils】是一款实用的Java库,用于处理与ZIP文件相关的操作,如创建、读取、更新和解压缩ZIP档案。这款工具对于开发者来说尤其方便,因为它提供了简单易用的API,使得在Java项目中处理ZIP文件变得轻而易举。在本...
public class ZipUtils { private static final int BUFFER = 8192; private static void log(String msg){ System.out.println (msg); } private static String getFileName(String filePath){ int ...
ZipUtils(2)
在本篇文章中,我们将深入探讨名为"ZipUtils"的Java工具类,它提供了ZIP文件的压缩与解压功能,并通过测试代码进行验证。 首先,让我们了解一下`ZipUtils`类的核心功能。这个工具类通常包含两个主要方法:一个是...
《ZipUtils.zip——高效便捷的文件压缩与解压工具》 在日常的计算机操作和软件开发中,文件的压缩和解压是不可或缺的功能。ZipUtils.zip 提供了一种简便的方法来处理这一任务,使得开发者无需深入理解复杂的压缩...
Java的ZipUtils工具类是Java开发中用于处理ZIP文件的一个实用工具集,它极大地简化了对ZIP文件的创建、解压以及与磁盘文件交互的操作。在Java中,虽然标准库提供了一些基本的ZIP文件操作,如java.util.zip包中的...
在C++编程环境中,开发涉及文件压缩和解压缩功能时,常常会用到各种库来实现这一需求。本文将深入探讨基于C++ MFC(Microsoft Foundation Classes)的压缩与解压缩源码,主要关注`zip_utils`这个压缩包中包含的`zip....
ZipUtils
5. **ZipUtils**: 压缩和解压缩工具类,用于处理ZIP文件。它包含了压缩文件和目录到ZIP,以及从ZIP文件中提取内容的方法。这对于数据的归档、传输或分发很有帮助。 6. **导出Excel**: 这部分可能涉及到生成Excel...
ZipUtils是一个常用的工具类,用于方便地进行ZIP格式的文件压缩与解压。本文将深入探讨ZipUtilsJava源码的相关知识点,帮助你理解和应用这个工具。 首先,我们来了解ZIP文件格式。ZIP是一种广泛使用的文件存档格式...
《ZipUtils:开源压缩与解压缩工具的探索》 在数字化时代,文件的压缩与解压缩成为了日常工作中不可或缺的一部分。为了满足这一需求,各种压缩工具应运而生,其中“ZipUtils”是一款开源的解决方案,专为文件和...
此外,Apache Commons IO的`ZipUtils`类提供了静态方法,可以用来方便地进行ZIP文件的创建和解压。例如,要创建一个ZIP文件,你可以这样做: ```java File[] files = { ... }; File targetZip = new File("output....