import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
private static final int BUFFER_SIZE = 1024 * 2;
/**
* 压缩成ZIP 方法1
* @param files 需要压缩的文件集合
* @param zipFilePath 压缩文件路径
* @throws Exception
*/
public static void toZip(List<File> files, String zipFilePath) throws Exception {
ZipOutputStream zos = null;
try {
OutputStream out = new FileOutputStream(new File(zipFilePath));
zos = new ZipOutputStream(out);
for (File file : files) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(file.getName()));
FileInputStream in = new FileInputStream(file);
int len;
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zos != null) {
zos.close();
}
}
}
/**
* 压缩成ZIP 方法2
* @param fileDirPath 压缩文件夹路径
* @param zipFilePath 压缩文路径
* @param keepDirStructure 是否保留原来的目录结构,true:保留目录结构;false:所有文件跑到压缩包根目录下(注意:
* 不保留目录结构可能会出现同名文件,会压缩失败)
* @throws Exception
*/
public static void toZip(String fileDirPath, String zipFilePath, boolean keepDirStructure) throws Exception {
ZipOutputStream zos = null;
try {
OutputStream out = new FileOutputStream(new File(zipFilePath));
zos = new ZipOutputStream(out);
File dirFile = new File(fileDirPath);
compress(dirFile, zos, dirFile.getName(), keepDirStructure);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zos != null) {
zos.close();
}
}
}
/**
* 递归压缩方法
* @param sourceFile 源文件
* @param zos zip输出流
* @param name 压缩后的文件夹/文件名称
* @param keepDirStructure 是否保留原来的目录结构,true:保留目录结构;
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
* @throws Exception
*/
public 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);
}
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);
}
}
}
}
}
}
压缩文件ZipUtil
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Java对Zip文件的支持不是很强大,有一些需要自己实现的代码,我在网上找了很多代码,都不能用于生产,要不就是流没...
- 背景 写了一个基于ZLIB压缩库的小程序,实现了对数据或是文件的压缩与解压缩。在编程实现的过程中,遇到一些波折,主...
- 原文地址 linux tar压缩解压缩文件夹、文件命令详解 直接先上三个常用命令 1.压缩当前目录下文件夹/文件t...
- 1、ls/ll列出当前目录下文件(夹) 另外如果加上-R参数,可以同时递归显示子目录下的文件和文件夹,这在有些场景...