- 浏览: 122350 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (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)
最新评论
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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 665eclipse java代码格式化 javaformatter ... -
List to Map
2015-01-21 17:22 437/** * <p>将List变为Map</p ... -
读取access工具类-ReadAccessUtil2
2015-01-06 13:52 505import com.healthmarketscience. ... -
ParameterizedType获取java泛型参数类型
2014-11-14 15:15 663ParameterizedType获取java泛型参数类型 ... -
分页帮助类-oracle-java
2014-10-09 17:46 420/** * 分页帮助类 * * @author ver ... -
java自动实例化List AutoArrayList
2014-07-21 15:51 704import java.util.ArrayList; p ... -
单一登录过滤器
2014-06-20 17:13 363/** * 单一登录过滤器 * * @author v ... -
正则 java 查找 打印 所有匹配项
2014-06-18 11:26 598package com.test.regex; import ... -
第3方包读取Access
2014-05-26 16:07 625package com.test; import java. ... -
多线程加签验签例子.zip
2014-05-26 16:08 479多线程加签验签例子.zip -
DateTimeUtils
2014-05-26 16:08 616import java.text.DateFormat; im ... -
listToMap
2014-05-26 16:09 396/** * List实用类 * * @author v ... -
JdbcTemplateRowMapper
2015-05-19 15:44 536import java.lang.reflect.Constr ... -
KeyValuePair2
2015-05-19 15:44 468/** * 键值对 * * @author verno ... -
OrderUtil -排序工具
2014-05-22 09:35 613/** * 排序工具 * * @author vern ... -
TimerUtils
2014-05-22 09:34 434import org.slf4j.Logger; import ... -
Excel View 4 Spring
2014-05-22 09:29 302Excel View 4 Spring @RequestMa ... -
DOMUtils xml操作实用类
2014-04-07 21:41 776/* * Copyright 2001-2004 The A ... -
生成验证码 数学+字母
2014-04-01 10:08 398public static String getCharAnd ... -
ConnUtils2程序耗时输出 oracle.sql.Clob类型转换成String类型
2014-03-29 10:00 651package com.achievo.ems.web.ser ...
相关推荐
ZipUtils--压缩、解压缩 超棒 超棒超棒超棒超棒超棒
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)
ZipUtilsjava压缩文件源码--ZipUtils
java文件生成zip格式(ziputils.java)
java的ZipUtils工具类。可以应对多种方案的业务场景,磁盘压缩以及下载压缩,直接可用。
基于c++ mfc 解压 压缩源码,包括了以下相关类:zip.h、zip.cpp、unzip.h、unzip.cpp文件
ZipUtils
该资源经本人测试可以使用且将使用注意点写在txt文件中,只收取2积分真乃天地良心,配合本人博客使用更佳
由于项目的需要使用到文档的压缩和解压功能,期初打算使用zlib,发现其只是提供了底层的接口供使用,主要是用来对文字的压缩和解压。但是针对大文档的压缩和解压,以及多层文档结构需要自己重新造轮子。...
Zip Utils--简单优雅的C++接口,需注意的是接口参数格式必须是TCHAR!!!不然不能使用
java组件(DateUtils,EncryptUtil,StringUtils,WordEdit功能,ZipUtils,导出Excel,读写Excel文件)
用于压缩和解压缩文件和文件夹的简单界面
只支持Zip的解压缩,