读书人

对指定资料或目录进行压缩和解压缩的工

发布时间: 2013-09-05 16:02:07 作者: rapoo

对指定文件或目录进行压缩和解压缩的工具类总结

对指定文件或目录进行压缩和解压缩的工具类总结,使用的都是zip压缩算法,使用ant包中的zip类,解决了压缩中文名文件的乱码问题,代码如下:

/** *  ClassName: ZipHelper.java *  Created on 2013 *  Copyrights 2013 hi.csdn.net/tjcyjd All rights reserved. *  site: http://hi.csdn.net/tjcyjd *  email: 908599713@qq.com */package com.kinder.common;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.zip.ZipInputStream;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;/** * 对指定文件或目录进行压缩和解压缩的工具类 *  * @author yjd */public class ZipHelper {private ZipHelper() {}/** * 把指定源文件压缩到目标文件中,使用的是zip格式<br/> *  * @param src *            要压缩的文件 * @param dest *            压缩后生成的文件(zip格式) */public static void zipFile(File src, File dest) throws RuntimeException {BufferedInputStream bis = null;ZipOutputStream zos = null;byte[] b = new byte[8192];try {bis = new BufferedInputStream(new FileInputStream(src));// 针对源文件创建带缓冲的字节输入流来读数据zos = new ZipOutputStream(new FileOutputStream(dest)); // 针对目标文件创建压缩输出流// 一个被压缩文件就是压缩文件中的一个条目zos.putNextEntry(new ZipEntry(src.getName())); // 开始写入新的 ZIP// 文件条目并将流定位到条目数据的开始处for (int len = 0; (len = bis.read(b)) != -1;) {zos.write(b, 0, len);}zos.flush(); // 刷新输出流} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);} finally {IOHelper.close(bis, zos);}}/** * 把指定目录下的所有子孙目录和文件全部压缩到指定目标文件(zip格式)中 ,目标文件名为"源目录名.zip" *  * @param srcDir * @param destBasePath */public static void zipDir(File srcDir, File destBasePath) {zipDir(srcDir, destBasePath, null);}/** * 把指定目录下的所有子孙目录和文件全部压缩到指定目标文件(zip格式)中 *  * @param srcDir *            要压缩的目录 * @param destBasePath *            存放zip文件的目录 * @param destFileName *            目标文件的名。如果没有提供,目标文件的名为"源目录名.zip" */public static void zipDir(File srcDir, File destBasePath,String destFileName) throws RuntimeException {if (!srcDir.exists() || !srcDir.isDirectory()) {return;}byte[] b = new byte[8192];ZipOutputStream zos = null;BufferedInputStream bis = null;try {if (null == destFileName || "".equals(destFileName)) {destFileName = srcDir.getName() + ".zip";}zos = new ZipOutputStream(new FileOutputStream(new File(destBasePath, destFileName)));List<File> subFiles = new ArrayList<File>();getAllSubFiles(srcDir, subFiles);if (subFiles.size() > 0) { // 有子文件for (File file : subFiles) { // 压缩每个子文件bis = new BufferedInputStream(new FileInputStream(file));// 往zip中添加一个条目String zipEntryName = file.getAbsolutePath();// 去掉指定目录的父目录路径名File parent = srcDir.getParentFile(); // 得到指定目录的父目录if (parent != null) {String parentName = parent.getAbsolutePath();zipEntryName = zipEntryName.substring(zipEntryName.indexOf(parentName)+ parentName.length() + 1);}zos.putNextEntry(new ZipEntry(zipEntryName));for (int len = 0; (len = bis.read(b)) != -1;) {zos.write(b, 0, len);}zos.flush();bis.close();}}} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);} finally {IOHelper.close(zos);}}/** * 解压缩指定压缩文件(zip格式) *  * @param src *            要解压缩的文件 * @param destBasePath *            解压缩后的文件存放的目录 */public static void unZip(File src, File destBasePath)throws RuntimeException {ZipInputStream zis = null;BufferedOutputStream bos = null;byte[] b = new byte[8192];try {zis = new ZipInputStream(new FileInputStream(src));// 把压缩文件中获取到每个条目for (java.util.zip.ZipEntry ze = null; (ze = zis.getNextEntry()) != null;) {// 压缩文件中的每个条目就对应目标目录下的一个文件File file = new File(destBasePath, ze.getName());File parentFile = file.getParentFile();if (!parentFile.exists()) { // 如果父目录下存在,就先创建file.getParentFile().mkdirs();}// 针对文件创建出一个输出流bos = new BufferedOutputStream(new FileOutputStream(file));for (int len = 0; (len = zis.read(b)) != -1;) {bos.write(b, 0, len);}bos.flush();bos.close();}} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);} finally {IOHelper.close(zis);}}/** * 递归获取指定目录下的所有子孙文件 *  * @param baseDir *            要递归的目录 * @param allSubFiles *            存放所有子孙文件的列表 */public static void getAllSubFiles(File baseDir, List<File> allSubFiles) {if (baseDir.exists()) { // baseDir存在if (baseDir.isDirectory()) { // 是目录File[] subFiles = baseDir.listFiles(); // 获取它的所有子目录和了文件int len = subFiles == null ? 0 : subFiles.length;for (int i = 0; i < len; i++) {getAllSubFiles(subFiles[i], allSubFiles); // 递归调用}} else {allSubFiles.add(baseDir);}}}}



读书人网 >编程

热点推荐