-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtar.java
80 lines (73 loc) · 3.52 KB
/
tar.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
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.nio.file.Files;
public class tar {
public static void main(String args[]) throws Exception {
if (args.length == 0) {
System.out.println("Usage: tar <output archive> [input directory]");
System.exit(1);
}
File archive = new File(args[0]);
String inputDirectory = ".";
if (args.length > 1) {
inputDirectory = args[1];
}
compress(archive, new File(inputDirectory));
}
private static void compress(File archive, File inputDirectory) throws IOException {
TarArchiveOutputStream tarOutput = createArchive(archive);
addEntryToArchive(archive.getName(), tarOutput, inputDirectory, null);
tarOutput.close();
}
private static TarArchiveOutputStream createArchive(File archive) throws IOException {
OutputStream output = new BufferedOutputStream(new FileOutputStream(archive));
if (archive.getName().endsWith(".gz")) {
output = new GzipCompressorOutputStream(output);
}
TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(output);
tarOutput.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
tarOutput.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
tarOutput.setAddPaxHeadersForNonAsciiNames(true);
return tarOutput;
}
private static void addEntryToArchive(String archiveName, TarArchiveOutputStream tarOutput, File file, String inputDirectory) throws IOException {
String filePath = file.getName();
if (inputDirectory != null) {
filePath = inputDirectory + File.separator + filePath;
}
if (filePath.equals(archiveName) || filePath.equals("." + File.separator + archiveName)) {
System.out.println("Skipping the archive \"" + filePath + "\" itself...");
} else if (Files.isSymbolicLink(file.toPath())) {
throw new IOException("Symbolic link \"" + file.getName() + "\" is not supported.");
} else if (file.isDirectory()) {
addDirectoryToArchive(archiveName, tarOutput, file, filePath);
} else if (file.isFile()){
addFileToArchive(tarOutput, file, filePath);
} else {
throw new IOException("Unrecognized item \"" + file.getName() + "\" is not supported.");
}
}
private static void addDirectoryToArchive(String archiveName, TarArchiveOutputStream tarOutput, File file, String filePath) throws IOException {
System.out.println("Entering \"" + filePath + "\"...");
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
addEntryToArchive(archiveName, tarOutput, child, filePath);
}
}
}
private static void addFileToArchive(TarArchiveOutputStream tarOutput, File file, String filePath) throws IOException {
System.out.println("Packing \"" + filePath + "\"...");
tarOutput.putArchiveEntry(new TarArchiveEntry(file, filePath));
IOUtils.copy(new FileInputStream(file), tarOutput);
tarOutput.closeArchiveEntry();
}
}