-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzzip
More file actions
executable file
·47 lines (44 loc) · 1.43 KB
/
Copy pathzzip
File metadata and controls
executable file
·47 lines (44 loc) · 1.43 KB
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
#!/usr/bin/env -S java --source 25
String version = "2026-02-27.1";
void main(String... args) {
if (args.length == 0 || args[0].equals("-help")) {
IO.println("Usage: zzip <directory>");
IO.println(" Zips a directory into <directory>.zip");
IO.println(" -help Show this help");
IO.println(" -version Show version");
return;
}
if (args[0].equals("-version")) {
IO.println("zzip " + version);
return;
}
var dir = Path.of(args[0]);
if (!Files.isDirectory(dir)) {
System.err.println("Not a directory: " + dir);
System.exit(1);
}
var zipFile = Path.of(dir.getFileName() + ".zip");
try (var fos = Files.newOutputStream(zipFile);
var zos = new ZipOutputStream(fos)) {
try (var paths = Files.walk(dir)) {
paths
.filter(Files::isRegularFile)
.forEach(path -> addEntry(zos, dir, path));
}
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
}
IO.println(zipFile.toAbsolutePath());
}
void addEntry(ZipOutputStream zos, Path base, Path file) {
try {
var entryName = base.relativize(file).toString();
var entry = new ZipEntry(entryName);
zos.putNextEntry(entry);
Files.copy(file, zos);
zos.closeEntry();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}