-
Notifications
You must be signed in to change notification settings - Fork 1
/
untar.java
118 lines (110 loc) · 4.79 KB
/
untar.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
107
108
109
110
111
112
113
114
115
116
117
118
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.nio.file.Files;
public class untar {
public static void main(String args[]) throws Exception {
if (args.length == 0) {
System.out.println("Usage: untar l|x <input archive> [output directory]");
System.exit(1);
}
String command = args[0];
if (args.length < 2) {
System.out.println("Missing input archive.");
System.exit(1);
}
File archive = new File(args[1]);
switch (command) {
case "l":
list(archive);
break;
case "x":
String outputDirectoryArg = args.length > 2 ? args[2] : ".";
File outputDirectory = new File(outputDirectoryArg);
uncompress(archive, outputDirectory);
break;
default:
System.out.println("Unknown command.");
System.exit(1);
}
}
private static void list(File archive) throws IOException {
TarArchiveInputStream tarInput = openArchive(archive);
while (true) {
TarArchiveEntry tarEntry = tarInput.getNextTarEntry();
if (tarEntry == null) {
break;
}
String entryName = tarEntry.getName();
if (tarEntry.isSymbolicLink()) {
String linkName = tarEntry.getLinkName();
System.out.println("Symbolic link \"" + linkName + "\" to \"" + entryName + "\"");
} else if (tarEntry.isDirectory()) {
System.out.println("Directory \"" + entryName + "\"");
} else if (tarEntry.isFile()) {
System.out.println("File \"" + entryName + "\"");
} else {
throw new IOException("Unknown archive entry type.");
}
}
tarInput.close();
}
private static void uncompress(File archive, File outputDirectory) throws IOException {
TarArchiveInputStream tarInput = openArchive(archive);
outputDirectory.mkdirs();
while (true) {
TarArchiveEntry tarEntry = tarInput.getNextTarEntry();
if (tarEntry == null) {
break;
}
if (tarEntry.isSymbolicLink()) {
extractSymbolicLink(tarEntry, outputDirectory);
} else if (tarEntry.isDirectory()) {
extractDirectory(tarEntry, outputDirectory);
} else if (tarEntry.isFile()) {
extractFile(tarEntry, tarInput, outputDirectory);
} else {
throw new IOException("Unknown archive entry type.");
}
}
tarInput.close();
}
private static TarArchiveInputStream openArchive(File archive) throws IOException {
InputStream input = new BufferedInputStream(new FileInputStream(archive));
if (archive.getName().endsWith(".gz")) {
input = new GzipCompressorInputStream(input);
}
return new TarArchiveInputStream(input);
}
private static void extractSymbolicLink(TarArchiveEntry tarEntry, File outputDirectory) throws IOException {
String entryName = tarEntry.getName();
String linkName = tarEntry.getLinkName();
System.out.println("Linking \"" + linkName + "\" to \"" + entryName + "\"...");
File outputFile = new File(outputDirectory, entryName);
File parentDirectory = outputFile.getParentFile();
File linkFile = new File(parentDirectory, linkName);
Files.createSymbolicLink(outputFile.toPath(), linkFile.toPath().toAbsolutePath());
}
private static void extractDirectory(TarArchiveEntry tarEntry, File outputDirectory) throws IOException {
String entryName = tarEntry.getName();
System.out.println("Creating \"" + entryName + "\"...");
File outputFile = new File(outputDirectory, entryName);
outputFile.mkdirs();
}
private static void extractFile(TarArchiveEntry tarEntry, TarArchiveInputStream tarInput, File outputDirectory) throws IOException {
String entryName = tarEntry.getName();
System.out.println("Unpacking \"" + entryName + "\"...");
File outputFile = new File(outputDirectory, entryName);
File parentDirectory = outputFile.getParentFile();
parentDirectory.mkdirs();
outputFile.createNewFile();
IOUtils.copy(tarInput, new FileOutputStream(outputFile));
}
}