文章内容列表
- 使用ZipFile解压文件
- 使用ZipInputStream解压文件流
- 使用ZipOutputStream压缩批量文件
说明:本文代码使用的是jdk自带的zip包,如果涉及中文文件名的场景可以使用apache的commons-compress包,代码相差不多,maven依赖如下:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>
一、使用ZipFile解压文件
public void unzip() {
//targetPath输出文件路径
File targetFile = new File("targetPath");
// 如果目录不存在,则创建
if (!targetFile.exists()) {
targetFile.mkdirs();
}
//sourcePath压缩包文件路径
try (ZipFile zipFile = new ZipFile(new File("sourcePath"))) {
System.out.println("file nums:" + zipFile.size());
Enumeration enumeration = zipFile.entries();
while (enumeration.hasMoreElements()) {
//依次获取压缩包内的文件实体对象
ZipEntry entry = (ZipEntry) enumeration.nextElement();
System.out.println("this file size:" + entry.getSize());
String name = entry.getName();
if (entry.isDirectory()) {
continue;
}
try (BufferedInputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry))) {
// 需要判断文件所在的目录是否存在,处理压缩包里面有文件夹的情况
String outName = "targetPath" + "/" + name;
File outFile = new File(outName);
File tempFile = new File(outName.substring(0, outName.lastIndexOf("/")));
if (!tempFile.exists()) {
tempFile.mkdirs();
}
try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outFile))) {
int len;
byte[] buffer = new byte[1024];
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
二、使用ZipInputStream解压文件流
public void unzipInputStream() {
try (ZipInputStream zip = new ZipInputStream(new FileInputStream(new File("sourceFilePath")))) {
ZipEntry zipEntry = null;
while ((zipEntry = zip.getNextEntry()) != null) {
String fileName_zip = zipEntry.getName();
System.out.println(fileName_zip);
File file = new File("/targetPath/" + fileName_zip);
if (fileName_zip.endsWith("/")) {
file.mkdir();
} else {
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
byte[] byte_s = new byte[1024];
int num;
while ((num = zip.read(byte_s, 0, byte_s.length)) > 0) {
outputStream.write(byte_s, 0, num);
}
outputStream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
三、使用ZipOutputStream压缩批量文件
public void zip(Map<String, File> map) {
//targetFilePath为导出文件路径,e.g.:/tmp/test.zip
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(new File("targetFilePath"))))) {
map.forEach((fileName, file) -> {
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
//fileName必须带文件扩展名
ZipEntry entry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(entry);
int len;
byte[] buffer = new byte[1024];
while ((len = inputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
} catch (IOException e) {
LOG.error("批量文件压缩IO异常", e);
}
});
zipOutputStream.closeEntry();
} catch (Exception e) {
LOG.error("压缩excel文件失败", e);
e.printStackTrace();
}
}