java 如何解压文件
使用 java.util.zip 包解压文件
Java 内置的 java.util.zip 包提供了处理 ZIP 文件的工具类。以下是一个完整的解压示例:
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class UnzipExample {
public static void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) {
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zipIn, filePath);
} else {
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
}
}
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
byte[] bytesIn = new byte[4096];
int read;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
}
}
public static void main(String[] args) {
try {
unzip("example.zip", "output_folder");
} catch (IOException e) {
e.printStackTrace();
}
}
}
关键点
- 使用
ZipInputStream读取 ZIP 文件条目。 - 检查条目是否为目录,非目录时调用
extractFile写入文件。 - 缓冲区大小(
4096)可根据需求调整。
使用 Apache Commons Compress 库
对于更复杂的压缩格式(如 TAR、7z),推荐使用 Apache Commons Compress 库。
Maven 依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.25.0</version>
</dependency>
解压 ZIP 示例
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import java.io.*;
public class CommonsUnzip {
public static void unzip(String zipFile, String outputDir) throws IOException {
try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new FileInputStream(zipFile))) {
ZipArchiveEntry entry;
while ((entry = zis.getNextZipEntry()) != null) {
File outputFile = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
outputFile.mkdirs();
} else {
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
byte[] buffer = new byte[4096];
int len;
while ((len = zis.read(buffer)) > 0) {
os.write(buffer, 0, len);
}
}
}
}
}
}
}
处理路径安全
为避免 ZIP 路径遍历漏洞(如恶意 ZIP 包含 ../ 路径),需对输出路径做校验:
String canonicalDestPath = destDir.getCanonicalPath() + File.separator;
String canonicalEntryPath = new File(destDir, entry.getName()).getCanonicalPath();
if (!canonicalEntryPath.startsWith(canonicalDestPath)) {
throw new IOException("ZIP entry试图跳出目标目录: " + entry.getName());
}
其他格式支持
- GZIP:使用
GZIPInputStream解压单个.gz文件。 - TAR:结合
TarArchiveInputStream(需 Commons Compress)。
// GZIP 解压示例
try (GZIPInputStream gis = new GZIPInputStream(new FileInputStream("file.gz"));
FileOutputStream fos = new FileOutputStream("output.txt")) {
byte[] buffer = new byte[1024];
int len;
while ((len = gis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
通过以上方法,可以灵活处理常见压缩文件的解压需求。







