压缩工具类,提供压缩文件、解压文件的方法。

源码如下:(点击下载 - ZipUtils.java 、FolderUtils.javaant-1.7.0.jarcommons-io-2.4.jarcommons-lang-2.6.jar)

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream; /**
* 压缩工具类
*
*/
public class ZipUtils { private static final String DEFAULT_CHARSET = "UTF-8"; /**
* 压缩文件夹
*
* @param zipFileName
* 打包后文件的名称,含路径
* @param sourceFolder
* 需要打包的文件夹或者文件的路径
* @param zipPathName
* 打包目的文件夹名,为空则表示直接打包到根
*/
public static void zip(String zipFileName, String sourceFolder, String zipPathName) throws Exception {
ZipOutputStream out = null;
try {
File zipFile = new File(zipFileName); FolderUtils.mkdirs(zipFile.getParent());
out = new ZipOutputStream(zipFile);
out.setEncoding(DEFAULT_CHARSET);
if (StringUtils.isNotBlank(zipPathName)) {
zipPathName = FilenameUtils.normalizeNoEndSeparator(zipPathName, true) + "/";
} else {
zipPathName = "";
}
zip(out, sourceFolder, zipPathName);
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e);
} finally {
IOUtils.closeQuietly(out);
}
} /**
* 压缩文件夹
*
* @param zipFile
* a {@link java.lang.String} object.
* @param source
* a {@link java.lang.String} object.
*/
public static void zip(String zipFile, String source) throws Exception {
File file = new File(source);
zip(zipFile, source, file.isFile() ? StringUtils.EMPTY : file.getName());
} /**
* 压缩文件夹
*
* @param zipFile
* a {@link java.io.File} object.
* @param source
* a {@link java.io.File} object.
*/
public static void zip(File zipFile, File source) throws Exception {
zip(zipFile.getAbsolutePath(), source.getAbsolutePath());
} private static void zip(ZipOutputStream zos, String file, String pathName) throws IOException {
File file2zip = new File(file);
if (file2zip.isFile()) {
zos.putNextEntry(new ZipEntry(pathName + file2zip.getName()));
IOUtils.copy(new FileInputStream(file2zip.getAbsolutePath()), zos);
zos.flush();
zos.closeEntry();
} else {
File[] files = file2zip.listFiles();
if (ArrayUtils.isNotEmpty(files)) {
for (File f : files) {
if (f.isDirectory()) {
zip(zos, FilenameUtils.normalizeNoEndSeparator(f.getAbsolutePath(), true),
FilenameUtils.normalizeNoEndSeparator(pathName + f.getName(), true) + "/");
} else {
zos.putNextEntry(new ZipEntry(pathName + f.getName()));
IOUtils.copy(new FileInputStream(f.getAbsolutePath()), zos);
zos.flush();
zos.closeEntry();
}
}
}
}
} /**
* 解压
*
* @param fromZipFile
* zip文件路径
* @param unzipPath
* 解压路径
*/
@SuppressWarnings("unchecked")
public static final void unzip(String fromZipFile, String unzipPath) throws Exception { FileOutputStream fos = null;
InputStream is = null;
String path1 = StringUtils.EMPTY;
String tempPath = StringUtils.EMPTY; if (!new File(unzipPath).exists()) {
new File(unzipPath).mkdir();
}
ZipFile zipFile = null;
try {
zipFile = new ZipFile(fromZipFile, DEFAULT_CHARSET);
} catch (IOException e1) {
e1.printStackTrace();
throw new Exception(e1);
}
File temp = new File(unzipPath);
String strPath = temp.getAbsolutePath();
Enumeration<ZipEntry> enu = zipFile.getEntries();
ZipEntry zipEntry = null;
while (enu.hasMoreElements()) {
zipEntry = (ZipEntry) enu.nextElement();
path1 = zipEntry.getName();
if (zipEntry.isDirectory()) {
tempPath = FilenameUtils.normalizeNoEndSeparator(strPath + File.separator + path1, true);
File dir = new File(tempPath);
dir.mkdirs();
continue;
} else { BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
is = zipFile.getInputStream(zipEntry);
bis = new BufferedInputStream(is);
path1 = zipEntry.getName();
tempPath = FilenameUtils.normalizeNoEndSeparator(strPath + File.separator + path1, true);
FolderUtils.mkdirs(new File(tempPath).getParent());
fos = new FileOutputStream(tempPath);
bos = new BufferedOutputStream(fos); IOUtils.copy(bis, bos);
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e);
} finally {
IOUtils.closeQuietly(bis);
IOUtils.closeQuietly(bos);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(fos);
}
}
}
}
}

压缩工具类 - ZipUtils.java的更多相关文章

  1. Java 实现文件压缩工具类

    package com.wdxc.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileI ...

  2. 【C#】依赖于SharpZipLib的Zip压缩工具类

    上班第二天下班,课外作业,实现一个ZIP压缩的工具类.本来想用Package,但是写完了才发现不能解压其他工具压缩的zip包,比较麻烦,因此本工具类依赖了第三方的库(SharpZipLib  vers ...

  3. 最近工作用到压缩,写一个zip压缩工具类

    package test; import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream ...

  4. 使用 Arrays 类操作 Java 中的数组

    Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等(关于类和方法的相关内容在后面的章节中会详细讲解滴 ...

  5. 慕课网-Java入门第一季-6-7 使用 Arrays 类操作 Java 中的数组

    来源:http://www.imooc.com/code/1556 Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现 ...

  6. java能不能自己写一个类叫java.lang.System/String正确答案

    原文: http://www.wfuyu.com/php/22254.html 未做测试 ! 最近学习了下java类加载相干的知识.然后看到网上有1道面试题是 能不能自己写个类叫java.lang.S ...

  7. hadoop中Text类 与 java中String类的区别

    hadoop 中 的Text类与java中的String类感觉上用法是相似的,但两者在编码格式和访问方式上还是有些差别的,要说明这个问题,首先得了解几个概念: 字符集: 是一个系统支持的所有抽象字符的 ...

  8. Arrays 类操作 Java 的数组排序

    使用 Arrays 类操作 Java 中的数组 Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等( ...

  9. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

随机推荐

  1. js分页

    今天看了下妙味课堂的教程,写了下关于分页的js代码,写完的感觉就是有点小麻烦,需要很多if判断,思路要清晰 点击预览:http://peng666.github.io/blogs/page <! ...

  2. 防止IE7,8进入怪异模式

    在页头添加 <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  3. 【Python】网络编程

    1.TCP编程 2.SocketServer模块 3.Twisted框架 4.UDP编程 1.TCP编程--TCP是面向连接的,其一般的设计如下: # encoding:utf-8 ''' Creat ...

  4. bzoj 2956 数学展开,分段处理

    首先对于答案 ΣΣ(n mod i)*(m mod j) i<>j 也就是Σ(n mod i)Σ(m mod j)-Σ(n mod i)(m mod i) 将mod展开,我们可以得到有fl ...

  5. ALAssetsLibrary

    ALAssetsLibrary详解   ALAssetsLibrary类是代表系统中整个资源库,使用它可以访问资源库中的资源和保存照片,视频等功能. _library = [[ALAssetsLibr ...

  6. 关于High-Resolution Timer(了解)

    如果一个系统包含高精度性能计数器(HRPC,high-resolution performance counter)则此系统提供高精度定时器.你可以使用API函数QueryPerformanceFre ...

  7. [百度空间] [转]DLL地狱及其解决方案

    DLL地狱及其解决方案 原作者:Ivan S Zapreev 译者:陆其明概要 本文将要介绍DLL的向后兼容性问题,也就是著名的“DLL Hell”问题.首先我会列出自己的研究结果,其中包括其它一些研 ...

  8. Spring boot教程

    1.首先是新建Maven工程 2.引入Pom依赖 3.新建一个Controller 4.运行Main方法 5.浏览器访问 pom.xml <project xmlns="http:// ...

  9. Keil中的code关键字

    一般说来,我们在C语言中定义的每一个变量初始化后都会占用一定的内存(RAM)空间.但是在keil中提供了一个特殊的关键字“code”,这个关键字在标准C中是没有的.其语法举例如下: unsigned ...

  10. Sqli-labs less 58

    Less-58 执行sql语句后,并没有返回数据库当中的数据,所以我们这里不能使用union联合注入,这里使用报错注入. Payload:http://127.0.0.1/sqli-labs/Less ...