JAVA 实现将文件或文件夹压缩成 zip,读取 zip 包下的文件。
该工具类功能:
- 支持压缩单个文件也支持压缩整个文件夹
- 支持压缩多级目录结构的文件夹
- 支持压缩空的文件夹
- 支持读取zip压缩包下的文件,包括多级目录结构的文件夹
一、代码:
public class ZipUtils {
/**
* buffer_size 大小
*/
private static final int BUFFER_SIZE = 1024 * 2;
/**
* 压缩文件
*
* @param srcDir 待压缩的文件/文件夹
* @param destDir 压缩后文件所在目录
*/
public static void zip(String srcDir, String destDir) {
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(destDir);
zos = new ZipOutputStream(fos);
File sourceFile = new File(srcDir);
zipFile(sourceFile, zos, sourceFile.getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 压缩单个文件
*
* @param sourceFile 源文件
* @param zos zip输出流
* @param name 压缩后文件名称
* @throws Exception
*/
private static void zipFile(File sourceFile, ZipOutputStream zos, String name) throws Exception {
if (sourceFile == null) {
return;
}
if (sourceFile.isFile()) {
FileInputStream fis = new FileInputStream(sourceFile);
zos.putNextEntry(new ZipEntry(name));
byte[] data = new byte[fis.available()];
fis.read(data);
zos.write(data);
zos.closeEntry();
fis.close();
} else {
File[] files = sourceFile.listFiles();
// 如果文件列表不为空递归调用压缩文件
if (files != null && files.length != 0) {
for (File file : files) {
zipFile(file, zos, name + "/" + file.getName());
}
} else {
// 如果文件为空,证明是一个空的文件夹,创建一个空的文件夹
zos.putNextEntry(new ZipEntry(name +"/"));
zos.closeEntry();
}
}
}
/**
* 解压缩包
*
* @param zipPath 压缩包路径
* @param destPath 文件解压的目录
*/
public static void unzip(String zipPath, String destPath) {
FileInputStream fis = null;
ZipInputStream zis = null;
File destFile;
try {
fis = new FileInputStream(zipPath);
zis = new ZipInputStream(fis);
ZipEntry zipEntry;
while( (zipEntry = zis.getNextEntry()) != null) {
destFile = new File(destPath + zipEntry.getName());
// 如果父目录不存在就创建
creatParentDir(destFile, zipEntry);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] data = new byte[BUFFER_SIZE];
int len = -1;
while ((len = zis.read(data)) != -1) {
fos.write(data, 0, len);
}
fos.close();
zis.closeEntry();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zis != null) {
try {
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 如果父目录不存在则创建
*
* @param destFile
* @param zipEntry
*/
private static void creatParentDir(File destFile, ZipEntry zipEntry) {
File parentFile = destFile.getParentFile();
parentFile.mkdirs();
//如果当前 zipEntry 是一个目录,那么在压缩包中这是一个空的目录,解压出来也要是一个目录
if (zipEntry.isDirectory()) {
destFile.mkdirs();
}
}
}
二、单元测试
@Test
public void zipSingleFile() {
// 压缩单个文件
String srcDir = "D:\\Test\\testDir\\zly.jpg";
String destDir = "D:\\Test\\testSingleFile.zip";
ZipUtils.zip(srcDir, destDir);
}
@Test
public void zip() {
// 压缩整个文件夹
String srcDir = "D:\\Test\\testDir";
String destDir = "D:\\Test\\test.zip";
ZipUtils.zip(srcDir, destDir);
}
@Test
public void unZipSingleFile() {
// 解压单个文件
String zipPath = "D:\\Test\\testSingleFile.zip";
String destPath = "D:\\Test\\testSingFile\\";
ZipUtils.unzip(zipPath, destPath);
}
@Test
public void unZip() {
// 解压整个文件夹
String zipPath = "D:\\Test\\test.zip";
String destPath = "D:\\Test\\test\\";
ZipUtils.unzip(zipPath, destPath);
}
以上就是用Java实现对文件的压缩以及解压操作,主要是对 ZipOutputStream 和 ZipEntry 的使用。