博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件压缩工具类(zip)
阅读量:4161 次
发布时间:2019-05-26

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

文件压缩工具类(zip)

推荐使用方法 FilePackUtil.pack(File src, File dest);

import java.io.*;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class FilePackUtil {
private static final int BUFFER_SIZE = 2 * 1024; public static void pack(File src, File dest) {
if (!dest.exists()) {
dest.mkdirs(); } FileOutputStream outputStream = null; try {
outputStream = new FileOutputStream(new File(dest, src.getName() + ".zip")); } catch (FileNotFoundException e) {
e.printStackTrace(); } pack(src, outputStream, true); } /** * 压缩成ZIP 方法1 * * @param srcFile 压缩文件夹路径 * @param out 压缩文件输出流 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * @throws RuntimeException 压缩失败会抛出运行时异常 */ public static void pack(File srcFile, OutputStream out, boolean KeepDirStructure) throws RuntimeException {
ZipOutputStream zos = null; try {
zos = new ZipOutputStream(out); compress(srcFile, zos, srcFile.getName(), KeepDirStructure); } catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e); } finally {
if (zos != null) {
try {
zos.close(); } catch (IOException e) {
e.printStackTrace(); } } } } /** * 压缩成ZIP 方法2 * * @param srcFiles 需要压缩的文件列表 * @param out 压缩文件输出流 * @throws RuntimeException 压缩失败会抛出运行时异常 */ public static void pack(List
srcFiles, OutputStream out) throws RuntimeException {
long start = System.currentTimeMillis(); ZipOutputStream zos = null; try {
zos = new ZipOutputStream(out); for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE]; zos.putNextEntry(new ZipEntry(srcFile.getName())); int len; FileInputStream in = new FileInputStream(srcFile); while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len); } zos.closeEntry(); in.close(); } long end = System.currentTimeMillis(); System.out.println("压缩完成,耗时:" + (end - start) + " ms"); } catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e); } finally {
if (zos != null) {
try {
zos.close(); } catch (IOException e) {
e.printStackTrace(); } } } } /** * 递归压缩方法 * * @param sourceFile 源文件 * @param zos zip输出流 * @param name 压缩后的名称 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * @throws Exception */ private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception {
byte[] buf = new byte[BUFFER_SIZE]; if (sourceFile.isFile()) {
// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字 zos.putNextEntry(new ZipEntry(name)); // copy文件到zip输出流中 int len; FileInputStream in = new FileInputStream(sourceFile); while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len); } // Complete the entry zos.closeEntry(); in.close(); } else {
File[] listFiles = sourceFile.listFiles(); if (listFiles == null || listFiles.length == 0) {
// 需要保留原来的文件结构时,需要对空文件夹进行处理 if (KeepDirStructure) {
// 空文件夹的处理 zos.putNextEntry(new ZipEntry(name + "/")); // 没有文件,不需要文件的copy zos.closeEntry(); } } else {
for (File file : listFiles) {
// 判断是否需要保留原来的文件结构 if (KeepDirStructure) {
// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠, // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了 compress(file, zos, name + "/" + file.getName(), KeepDirStructure); } else {
compress(file, zos, file.getName(), KeepDirStructure); } } } } }}

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

你可能感兴趣的文章
make -n(仅列出命令, 但不会执行)用于调试makefile
查看>>
go语言如何从终端逐行读取数据?------用bufio包
查看>>
go的值类型和引用类型------重要的概念
查看>>
求二叉树中结点的最大值(所有结点的值都是正整数)
查看>>
用go的flag包来解析命令行参数
查看>>
来玩下go的http get
查看>>
队列和栈的本质区别
查看>>
matlab中inline的用法
查看>>
如何用matlab求函数的最值?
查看>>
Git从入门到放弃
查看>>
java8采用stream对集合的常用操作
查看>>
EasySwift/YXJOnePixelLine 极其方便的画出真正的一个像素的线
查看>>
Ubuntu Linux系统下apt-get命令详解
查看>>
ubuntu 16.04 下重置 MySQL 5.7 的密码(忘记密码)
查看>>
Ubuntu Navicat for MySQL安装以及破解方案
查看>>
HTTPS那些事 用java实现HTTPS工作原理
查看>>
oracle函数trunc的使用
查看>>
MySQL 存储过程或者函数中传参数实现where id in(1,2,3,...)IN条件拼接
查看>>
java反编译
查看>>
Class.forName( )你搞懂了吗?——转
查看>>