使用 commons-compress 实现 Java 压缩解压缩 zip 文件
创建时间: 8-28 10:42
修改时间: 8-28 10:44
浏览: 8
首先引入依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.2</version>
</dependency>
新建一个工具类:
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;
public class ZipUtils {
/**
* 将文件压缩成zip
*
* @param sourceFile 需要压缩的目录 /temp/file
* @param targetFile 压缩文件地址 /temp/file.zip
*/
public static void compress(String sourceFile, String targetFile) throws FileNotFoundException {
long d1 = System.currentTimeMillis();
FileOutputStream fos = new FileOutputStream(targetFile);
try (ZipArchiveOutputStream aos = new ZipArchiveOutputStream(fos)) {
Path dirPath = Paths.get(sourceFile);
Files.walkFileTree(dirPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
ZipArchiveEntry entry = new ZipArchiveEntry(dir.toFile(), dirPath.relativize(dir).toString());
aos.putArchiveEntry(entry);
aos.closeArchiveEntry();
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
ZipArchiveEntry entry = new ZipArchiveEntry(
file.toFile(), dirPath.relativize(file).toString());
aos.putArchiveEntry(entry);
Files.copy(file, aos);
aos.closeArchiveEntry();
return super.visitFile(file, attrs);
}
});
} catch (IOException e) {
System.out.println("压缩失败,原因:" + e.getMessage());
}
System.out.println("压缩完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
}
/**
* 将zip文件解压到指定目录
*
* @param zipPath 源文件 /temp/file.zip
* @param descDir 解压目录 /temp/fileCopy
*/
public static void uncompress(String zipPath, String descDir) {
long d1 = System.currentTimeMillis();
try (InputStream fis = Files.newInputStream(Paths.get(zipPath));
InputStream bis = new BufferedInputStream(fis);
ZipArchiveInputStream ais = new ZipArchiveInputStream(bis)
) {
ZipArchiveEntry entry;
while (Objects.nonNull(entry = ais.getNextEntry())) {
if (!ais.canReadEntryData(entry)) {
continue;
}
String name = descDir + File.separator + entry.getName();
File f = new File(name);
if (entry.isDirectory()) {
if (!f.isDirectory() && !f.mkdirs()) {
f.mkdirs();
}
} else {
File parent = f.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("failed to create directory " + parent);
}
try (OutputStream o = Files.newOutputStream(f.toPath())) {
IOUtils.copy(ais, o);
}
}
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("解压失败,原因:" + e.getMessage());
}
System.out.println("解压完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
}
}
测试一下:
public static void main(String[] args) throws FileNotFoundException {
compress("D:\\temp\\新建文件夹", "D:\\temp\\新建文件夹.zip");
uncompress("D:\\temp\\新建文件夹.zip", "D:\\temp\\文件夹");
}
创建时间: 8-28 10:42
修改时间: 8-28 10:44
浏览: 8