Java 解压 .tar.gz 文件
在 Java 中解压 .tar.gz 文件,可以使用 Apache Commons Compress 库,它提供了对多种压缩格式的支持,包括 tar 和 gzip。

使用 Apache Commons Compress
添加依赖
在你的项目中添加 Apache Commons Compress 依赖:
Maven:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.23.0</version>
</dependency>
Gradle:
implementation 'org.apache.commons:commons-compress:1.23.0'
解压代码示例
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class TarGzExtractor {
public static void extractTarGz(String tarGzFilePath, String outputDir) throws IOException {
File outputFile = new File(outputDir);
if (!outputFile.exists()) {
outputFile.mkdirs();
}
try (InputStream fi = Files.newInputStream(Paths.get(tarGzFilePath));
InputStream bi = new BufferedInputStream(fi);
InputStream gzi = new GzipCompressorInputStream(bi);
TarArchiveInputStream tis = new TarArchiveInputStream(gzi)) {
TarArchiveEntry entry;
while ((entry = tis.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
File outputFileEntry = new File(outputDir, entry.getName());
if (!outputFileEntry.getParentFile().exists()) {
outputFileEntry.getParentFile().mkdirs();
}
try (OutputStream outputFileStream = Files.newOutputStream(outputFileEntry.toPath())) {
IOUtils.copy(tis, outputFileStream);
}
}
}
}
public static void main(String[] args) {
String tarGzFilePath = "path/to/your/file.tar.gz";
String outputDir = "path/to/output/directory";
try {
extractTarGz(tarGzFilePath, outputDir);
System.out.println("解压完成!");
} catch (IOException e) {
System.err.println("解压过程中出错: " + e.getMessage());
e.printStackTrace();
}
}
}
使用 Java 7+ NIO 和外部命令(不推荐)
如果你不想添加外部依赖,可以使用 Java 的 Process 类调用系统命令(如 tar),但这不推荐,因为它依赖于系统环境:

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TarGzExtractorWithCommand {
public static void extractTarGz(String tarGzFilePath, String outputDir) throws IOException {
ProcessBuilder processBuilder = new ProcessBuilder("tar", "-xzf", tarGzFilePath, "-C", outputDir);
Process process = processBuilder.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.err.println(line);
}
}
try {
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new IOException("解压失败,退出码: " + exitCode);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("解压过程被中断", e);
}
}
public static void main(String[] args) {
String tarGzFilePath = "path/to/your/file.tar.gz";
String outputDir = "path/to/output/directory";
try {
extractTarGz(tarGzFilePath, outputDir);
System.out.println("解压完成!");
} catch (IOException e) {
System.err.println("解压过程中出错: " + e.getMessage());
e.printStackTrace();
}
}
}
注意事项
- 路径处理:确保处理文件路径时使用正确的分隔符( 或
\\),特别是跨平台时。 - 异常处理:妥善处理 IO 异常,确保资源被正确关闭。
- 大文件:对于大文件,考虑使用缓冲流和分块读取以提高性能。
- 权限:确保程序有权限读取源文件和写入目标目录。
推荐使用第一种方法(Apache Commons Compress),因为它更可靠且不依赖于系统命令。

