-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressExample.java
More file actions
240 lines (212 loc) · 9.73 KB
/
CompressExample.java
File metadata and controls
240 lines (212 loc) · 9.73 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.example;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.compressors.zstandard.ZstdUtils;
import org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream;
import org.apache.commons.compress.compressors.brotli.BrotliUtils;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* Example class using Apache Commons Compress.
* This demonstrates various compression and archive operations
* that should trigger different capability detections.
*/
public class CompressExample {
/**
* Create a ZIP archive from a directory.
* Should trigger CAPABILITY_FILES.
*/
public void createZipArchive(Path sourceDir, Path outputFile) throws IOException {
try (ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(
Files.newOutputStream(outputFile));
var paths = Files.walk(sourceDir)) {
for (Path path : paths.filter(Files::isRegularFile).toList()) {
ZipArchiveEntry entry = new ZipArchiveEntry(sourceDir.relativize(path).toString());
entry.setSize(Files.size(path));
zipOut.putArchiveEntry(entry);
Files.copy(path, zipOut);
zipOut.closeArchiveEntry();
}
}
}
/**
* Extract files from an archive.
* Should trigger CAPABILITY_FILES.
*/
public void extractArchive(Path archiveFile, Path destDir) throws Exception {
Files.createDirectories(destDir);
try (InputStream fileIn = Files.newInputStream(archiveFile);
ArchiveInputStream<?> archiveIn = new ArchiveStreamFactory()
.createArchiveInputStream(fileIn)) {
ArchiveEntry entry;
while ((entry = archiveIn.getNextEntry()) != null) {
if (!archiveIn.canReadEntryData(entry)) {
continue;
}
// in case of refactoring we want to avid path traversals
Path outputPath = destDir.resolve(entry.getName()).normalize();
if (!outputPath.startsWith(destDir)) {
throw new IOException("Archive entry is outside of the target directory: " + entry.getName());
}
if (entry.isDirectory()) {
Files.createDirectories(outputPath);
} else {
Files.createDirectories(outputPath.getParent());
try (OutputStream out = Files.newOutputStream(outputPath)) {
archiveIn.transferTo(out);
}
}
}
}
}
/**
* Compress a file using GZIP.
* Should trigger CAPABILITY_FILES.
*/
public void compressFileGzip(Path inputFile, Path outputFile) throws IOException {
try (InputStream in = Files.newInputStream(inputFile);
OutputStream out = Files.newOutputStream(outputFile);
GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(out)) {
in.transferTo(gzipOut);
}
}
/**
* Create a TAR archive.
* Should trigger CAPABILITY_FILES.
*/
public void createTarArchive(Path sourceDir, Path outputFile) throws IOException {
try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(
Files.newOutputStream(outputFile));
var paths = Files.walk(sourceDir)) {
tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
for (Path path : paths.filter(Files::isRegularFile).toList()) {
TarArchiveEntry entry = new TarArchiveEntry(
path.toFile(),
sourceDir.relativize(path).toString());
tarOut.putArchiveEntry(entry);
Files.copy(path, tarOut);
tarOut.closeArchiveEntry();
}
}
}
/**
* Check available compression formats.
* This triggers CAPABILITY_REFLECT by checking for optional compression libraries.
*/
public void checkAvailableFormats() {
System.out.println("Checking available compression formats:");
// These methods use Class.forName internally to check for optional libraries
// This triggers CAPABILITY_REFLECT
boolean zstdAvailable = ZstdUtils.isZstdCompressionAvailable();
boolean brotliAvailable = BrotliUtils.isBrotliCompressionAvailable();
System.out.println(" Zstandard (zstd) support: " + zstdAvailable);
System.out.println(" Brotli support: " + brotliAvailable);
}
/**
* Compress a file using Zstandard compression via Apache Commons Compress.
* This should trigger CAPABILITY_CGO because zstd-jni (an optional dependency)
* loads native libraries.
*/
private void compressFileZstd(Path inputFile, Path outputFile) throws IOException {
try (InputStream in = Files.newInputStream(inputFile);
OutputStream out = Files.newOutputStream(outputFile);
ZstdCompressorOutputStream zstdOut = new ZstdCompressorOutputStream(out)) {
byte[] buffer = new byte[8192];
int len;
while ((len = in.read(buffer)) != -1) {
zstdOut.write(buffer, 0, len);
}
}
long originalSize = Files.size(inputFile);
long compressedSize = Files.size(outputFile);
System.out.println("Zstd compression ratio: " +
((double) compressedSize / originalSize * 100) + "%");
}
/**
* List contents of an archive without extracting.
* Should trigger CAPABILITY_FILES.
*/
public void listArchiveContents(Path archiveFile) throws Exception {
try (InputStream fileIn = Files.newInputStream(archiveFile);
ArchiveInputStream<?> archiveIn = new ArchiveStreamFactory()
.createArchiveInputStream(fileIn)) {
ArchiveEntry entry;
System.out.println("Archive contents:");
while ((entry = archiveIn.getNextEntry()) != null) {
System.out.printf(" %s %s (%d bytes)%n",
entry.isDirectory() ? "DIR " : "FILE",
entry.getName(),
entry.getSize());
}
}
}
public static void main(String[] args) {
CompressExample example = new CompressExample();
try {
System.out.println("Apache Commons Compress Example");
System.out.println("================================");
// Always check available formats - this triggers CAPABILITY_REFLECT
example.checkAvailableFormats();
// Test Zstandard if available - this triggers CAPABILITY_CGO via native library loading
if (ZstdUtils.isZstdCompressionAvailable()) {
System.out.println("\nTesting Zstandard compression (native library)...");
try {
// Create a test file to compress
String testContent = "This is a test for Zstandard compression with native libraries.";
Path testFile = Path.of("test.txt");
Files.write(testFile, testContent.getBytes());
} catch (Exception e) {
System.out.println("Zstandard test failed: " + e.getMessage());
}
}
// Example operations using static /tmp directories
Path tmpDir = Path.of("/tmp/compress-example");
Files.createDirectories(tmpDir);
Path sourceDir = tmpDir.resolve("source");
Path extractDir = tmpDir.resolve("extract");
Path zipFile = tmpDir.resolve("archive.zip");
Path gzFile = tmpDir.resolve("file.gz");
Path inputFile = tmpDir.resolve("input.txt");
if (args.length > 0) {
switch (args[0]) {
case "zip":
example.createZipArchive(sourceDir, zipFile);
System.out.println("Created ZIP archive: " + zipFile);
break;
case "extract":
example.extractArchive(zipFile, extractDir);
System.out.println("Extracted archive to: " + extractDir);
break;
case "gzip":
example.compressFileGzip(inputFile, gzFile);
System.out.println("Compressed file with GZIP: " + gzFile);
break;
case "list":
example.listArchiveContents(zipFile);
break;
default:
printUsage();
}
} else {
printUsage();
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
private static void printUsage() {
System.out.println("Usage:");
System.out.println(" zip <source-dir> <output.zip> - Create ZIP archive");
System.out.println(" extract <archive> <dest-dir> - Extract archive");
System.out.println(" gzip <input-file> <output.gz> - Compress with GZIP");
System.out.println(" list <archive> - List archive contents");
}
}