博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
对指定文件或目录进行压缩和解压缩的工具类总结
阅读量:4220 次
发布时间:2019-05-26

本文共 4963 字,大约阅读时间需要 16 分钟。

对指定文件或目录进行压缩和解压缩的工具类总结,使用的都是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格式
* * @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
subFiles = new ArrayList
(); 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
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); } } }}

转载地址:http://aoemi.baihongyu.com/

你可能感兴趣的文章
hibernate中取得connection的方法
查看>>
如何使用log4j输出单个级别的log到指定文件
查看>>
表单元素与提示文字无法对齐的解决方法
查看>>
图片按钮消除边框
查看>>
windows xp 系统CMD命令大全
查看>>
xampp下Apache + Tomcat 集群配置的简单介绍(with sticky session)
查看>>
xampp(Apache + Tomcat)与主机的域名绑定
查看>>
增加windows下Tomcat运行时的内存
查看>>
tomcat群集中session共享的几个方案
查看>>
查找google谷歌北京IP地址的方法
查看>>
chrome的异常Uncaught ReferenceError: xl_chrome_menu is not defined
查看>>
Java不使用web容器,发布WebService应用
查看>>
Visual Studio 单元测试之三---压力测试
查看>>
Visual Studio 单元测试之五---数据库测试
查看>>
Visual Studio 单元测试之六---UI界面测试
查看>>
窗体间利用属性(property)来传递值时应注意的问题。
查看>>
动态代理技术的实现与理解
查看>>
使用Beyond Compare合并代码后出现乱码问题
查看>>
dmp数据文件导入问题
查看>>
使用Beyond Compare对比文件夹
查看>>