java 解压Tar文件
时间: 2023-11-20 07:57:02 浏览: 139
以下是两种Java解压Tar文件的例子:
1.使用FileInputStream解压
```java
unTar(new FileInputStream(filePath), unCompressPath);
```
2.使用GzipCompressorInputStream解压
```java
unTar(new GzipCompressorInputStream(new FileInputStream(filePath)), unCompressPath);
```
其中,unTar方法需要自己实现,可以参考下面的代码:
```java
public static void unTar(InputStream inputStream, String destPath) throws IOException {
TarArchiveInputStream tarIn = new TarArchiveInputStream(inputStream);
TarArchiveEntry entry = null;
while ((entry = tarIn.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
new File(destPath, entry.getName()).mkdirs();
} else {
File tmpFile = new File(destPath, entry.getName());
File parentFile = tmpFile.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
OutputStream out = new FileOutputStream(tmpFile);
int length = 0;
byte[] b = new byte[2048];
while ((length = tarIn.read(b)) != -1) {
out.write(b, 0, length);
}
out.close();
}
}
tarIn.close();
}
```