Java 解压 gzip 和 tar.gz 文件

在开发过程中有时候会需要解压 gzip 或者 tar.gz 文件,下面封装了一个工具类,可以解压 gzip 和 tar.gz 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.example.demo.common.utils;
/**
* Created by qianghaohao on 2021/5/23
*/

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 java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

/**
* @description:
* @author: qianghaohao
* @time: 2021/5/23
*/
public class FileUtils {
private static final int BUFFER_SIZE = 1024;

/**
* 解压 gzip 文件
*
* @param input
* @param output
*
*/
public static void decompressGzip(File input, File output) throws IOException {
try (GZIPInputStream in = new GZIPInputStream(new FileInputStream(input))) {
try (FileOutputStream out = new FileOutputStream(output)) {
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
}
}
}

/**
* 解压 tar.gz 文件到指定目录
*
* @param tarGzFile tar.gz 文件路径
* @param destDir 解压到 destDir 目录,如果没有则自动创建
*
*/
public static void extractTarGZ(File tarGzFile, String destDir) throws IOException {

GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(new FileInputStream(tarGzFile));
try (TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
TarArchiveEntry entry;

while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
if (entry.isDirectory()) {
File f = new File(destDir + "/" + entry.getName());
boolean created = f.mkdirs();
if (!created) {
System.out.printf("Unable to create directory '%s', during extraction of archive contents.\n",
f.getAbsolutePath());
}
} else {
int count;
byte [] data = new byte[BUFFER_SIZE];
FileOutputStream fos = new FileOutputStream(destDir + "/" + entry.getName(), false);
try (BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE)) {
while ((count = tarIn.read(data, 0, BUFFER_SIZE)) != -1) {
dest.write(data, 0, count);
}
}
}
}
}
}
}

使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void decompressGizpTest() throws IOException {
File input = new File("/xxx/output.gz");
File output = new File("/xxx/output");
FileUtils.decompressGzip(input, output);
}

@Test
public void decompressTarGizpTest() throws IOException {
File input = new File("/xxx/output.tar.gz");
FileUtils.extractTarGZ(input, "/tmp/");
}