-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtar_tool.java
106 lines (94 loc) · 4.4 KB
/
tar_tool.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.io.*;
import java.nio.file.*;
import java.util.zip.*;
public class ArchiveUtility {
// Create a .tar archive
public static void createTarArchive(String archiveName, String... files) throws IOException {
try (FileOutputStream fos = new FileOutputStream(archiveName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
TarOutputStream tos = new TarOutputStream(bos)) {
for (String file : files) {
Path filePath = Paths.get(file);
TarEntry entry = new TarEntry(filePath.toFile(), filePath.getFileName().toString());
tos.putNextEntry(entry);
Files.copy(filePath, tos);
tos.closeEntry();
}
}
}
// Create a .tar.gz archive
public static void createTarGzArchive(String archiveName, String... files) throws IOException {
try (FileOutputStream fos = new FileOutputStream(archiveName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
GZIPOutputStream gzos = new GZIPOutputStream(bos);
TarOutputStream tos = new TarOutputStream(gzos)) {
for (String file : files) {
Path filePath = Paths.get(file);
TarEntry entry = new TarEntry(filePath.toFile(), filePath.getFileName().toString());
tos.putNextEntry(entry);
Files.copy(filePath, tos);
tos.closeEntry();
}
}
}
// Create a .tar.bz2 archive
public static void createTarBz2Archive(String archiveName, String... files) throws IOException {
try (FileOutputStream fos = new FileOutputStream(archiveName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BZip2CompressorOutputStream bz2os = new BZip2CompressorOutputStream(bos);
TarOutputStream tos = new TarOutputStream(bz2os)) {
for (String file : files) {
Path filePath = Paths.get(file);
TarEntry entry = new TarEntry(filePath.toFile(), filePath.getFileName().toString());
tos.putNextEntry(entry);
Files.copy(filePath, tos);
tos.closeEntry();
}
}
}
// Extract a .tar archive
public static void extractTarArchive(String archiveName, String targetDirectory) throws IOException {
try (FileInputStream fis = new FileInputStream(archiveName);
BufferedInputStream bis = new BufferedInputStream(fis);
TarInputStream tis = new TarInputStream(bis)) {
TarEntry entry;
while ((entry = tis.getNextEntry()) != null) {
Path filePath = Paths.get(targetDirectory, entry.getName());
Files.createDirectories(filePath.getParent());
Files.copy(tis, filePath, StandardCopyOption.REPLACE_EXISTING);
}
}
}
// Extract a .tar.gz archive
public static void extractTarGzArchive(String archiveName, String targetDirectory) throws IOException {
try (FileInputStream fis = new FileInputStream(archiveName);
BufferedInputStream bis = new BufferedInputStream(fis);
GzipCompressorInputStream gzis = new GzipCompressorInputStream(bis);
TarInputStream tis = new TarInputStream(gzis)) {
TarEntry entry;
while ((entry = tis.getNextEntry()) != null) {
Path filePath = Paths.get(targetDirectory, entry.getName());
Files.createDirectories(filePath.getParent());
Files.copy(tis, filePath, StandardCopyOption.REPLACE_EXISTING);
}
}
}
public static void main(String[] args) {
try {
// Create .tar archive
createTarArchive("archive.tar", "file1.txt", "file2.txt");
// Create .tar.gz archive
createTarGzArchive("archive.tar.gz", "file1.txt", "file2.txt");
// Create .tar.bz2 archive
createTarBz2Archive("archive.tar.bz2", "file1.txt", "file2.txt");
// Extract .tar archive
extractTarArchive("archive.tar", ".");
// Extract .tar archive to target directory
extractTarArchive("archive.tar", "/path/to/targetdirectory/");
// Extract .tar.gz archive
extractTarGzArchive("archive.tar.gz", ".");
} catch (IOException e) {
e.printStackTrace();
}
}
}