1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.compress.archivers.zip;
21
22 import static java.nio.charset.StandardCharsets.UTF_8;
23 import static org.apache.commons.compress.AbstractTest.getPath;
24 import static org.apache.commons.compress.archivers.zip.ZipArchiveEntryRequest.createZipArchiveEntryRequest;
25 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.nio.channels.SeekableByteChannel;
35 import java.nio.charset.StandardCharsets;
36 import java.nio.file.FileSystem;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.StandardOpenOption;
40 import java.security.NoSuchAlgorithmException;
41 import java.security.SecureRandom;
42 import java.util.ArrayList;
43 import java.util.Comparator;
44 import java.util.EnumSet;
45 import java.util.List;
46 import java.util.Random;
47 import java.util.UUID;
48 import java.util.stream.Collectors;
49 import java.util.stream.Stream;
50 import java.util.zip.Deflater;
51 import java.util.zip.ZipEntry;
52
53 import org.apache.commons.compress.AbstractTest;
54 import org.apache.commons.compress.archivers.ArchiveException;
55 import org.apache.commons.compress.archivers.ArchiveOutputStream;
56 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
57 import org.apache.commons.compress.parallel.InputStreamSupplier;
58 import org.apache.commons.io.IOUtils;
59 import org.apache.commons.io.file.PathUtils;
60 import org.junit.jupiter.api.AfterEach;
61 import org.junit.jupiter.api.BeforeEach;
62 import org.junit.jupiter.api.Test;
63
64 import com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder;
65
66 class ZipMemoryFileSystemTest {
67
68 static void println(final String x) {
69
70 }
71
72 private Path dir;
73
74 private InputStreamSupplier createPayloadSupplier(final ByteArrayInputStream payload) {
75 return () -> payload;
76 }
77
78 @BeforeEach
79 public void setup() throws IOException {
80 dir = Files.createTempDirectory(UUID.randomUUID().toString());
81 }
82
83 @AfterEach
84 public void tearDown() throws IOException {
85 try (Stream<Path> walk = Files.walk(dir)) {
86 walk.sorted(Comparator.reverseOrder()).peek(path -> println("Deleting: " + path.toAbsolutePath())).forEach(path -> {
87 try {
88 Files.deleteIfExists(path);
89 } catch (final IOException ignored) {
90
91 }
92 });
93 }
94 }
95
96 @Test
97 void testForPathsReturnCorrectClassInMemory() throws IOException {
98 final Path firstFile = getPath("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.z01");
99 final Path secondFile = getPath("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.z02");
100 final Path lastFile = getPath("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.zip");
101 final byte[] firstBytes = Files.readAllBytes(firstFile);
102 final byte[] secondBytes = Files.readAllBytes(secondFile);
103 final byte[] lastBytes = Files.readAllBytes(lastFile);
104 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
105 Files.write(fileSystem.getPath("split_zip_created_by_zip.z01"), firstBytes);
106 Files.write(fileSystem.getPath("split_zip_created_by_zip.z02"), secondBytes);
107 Files.write(fileSystem.getPath("split_zip_created_by_zip.zip"), lastBytes);
108 final ArrayList<Path> list = new ArrayList<>();
109 list.add(firstFile);
110 list.add(secondFile);
111
112 try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forPaths(lastFile, list)) {
113 assertInstanceOf(ZipSplitReadOnlySeekableByteChannel.class, channel);
114 }
115
116 try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forPaths(firstFile, secondFile, lastFile)) {
117 assertInstanceOf(ZipSplitReadOnlySeekableByteChannel.class, channel);
118 }
119 }
120 }
121
122 @Test
123 void testPositionToSomeZipSplitSegmentInMemory() throws IOException {
124 final byte[] firstBytes = AbstractTest.readAllBytes("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.z01");
125 final byte[] secondBytes = AbstractTest.readAllBytes("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.z02");
126 final byte[] lastBytes = AbstractTest.readAllBytes("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.zip");
127 final int firstFileSize = firstBytes.length;
128 final int secondFileSize = secondBytes.length;
129 final int lastFileSize = lastBytes.length;
130
131 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
132 final Path lastMemoryPath = fileSystem.getPath("split_zip_created_by_zip.zip");
133 Files.write(fileSystem.getPath("split_zip_created_by_zip.z01"), firstBytes);
134 Files.write(fileSystem.getPath("split_zip_created_by_zip.z02"), secondBytes);
135 Files.write(lastMemoryPath, lastBytes);
136 final Random random = new Random();
137 final int randomDiskNumber = random.nextInt(3);
138 final int randomOffset = randomDiskNumber < 2 ? random.nextInt(firstFileSize) : random.nextInt(lastFileSize);
139
140 try (ZipSplitReadOnlySeekableByteChannel channel = (ZipSplitReadOnlySeekableByteChannel) ZipSplitReadOnlySeekableByteChannel
141 .buildFromLastSplitSegment(lastMemoryPath)) {
142 channel.position(randomDiskNumber, randomOffset);
143 long expectedPosition = randomOffset;
144
145 expectedPosition += randomDiskNumber > 0 ? firstFileSize : 0;
146 expectedPosition += randomDiskNumber > 1 ? secondFileSize : 0;
147
148 assertEquals(expectedPosition, channel.position());
149 }
150 }
151
152 }
153
154 @Test
155 void testScatterFileInMemory() throws IOException {
156 final byte[] B_PAYLOAD = "RBBBBBBS".getBytes();
157 final byte[] A_PAYLOAD = "XAAY".getBytes();
158 final Path target = Files.createTempFile(dir, "scattertest", ".zip");
159 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
160 final Path scatterFile = fileSystem.getPath("scattertest.notzip");
161 try (ScatterZipOutputStream scatterZipOutputStream = ScatterZipOutputStream.pathBased(scatterFile)) {
162
163 final ZipArchiveEntry zab = new ZipArchiveEntry("b.txt");
164 zab.setMethod(ZipEntry.DEFLATED);
165 final ByteArrayInputStream payload = new ByteArrayInputStream(B_PAYLOAD);
166 scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zab, createPayloadSupplier(payload)));
167
168 final ZipArchiveEntry zae = new ZipArchiveEntry("a.txt");
169 zae.setMethod(ZipEntry.DEFLATED);
170 final ByteArrayInputStream payload1 = new ByteArrayInputStream(A_PAYLOAD);
171 scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zae, createPayloadSupplier(payload1)));
172
173 try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(target)) {
174 scatterZipOutputStream.writeTo(outputStream);
175 }
176 }
177
178 try (ZipFile zf = ZipFile.builder().setPath(target).get()) {
179 final ZipArchiveEntry b_entry = zf.getEntries("b.txt").iterator().next();
180 assertEquals(8, b_entry.getSize());
181 try (InputStream inputStream = zf.getInputStream(b_entry)) {
182 assertArrayEquals(B_PAYLOAD, IOUtils.toByteArray(inputStream));
183 }
184
185 final ZipArchiveEntry a_entry = zf.getEntries("a.txt").iterator().next();
186 assertEquals(4, a_entry.getSize());
187 try (InputStream inputStream = zf.getInputStream(a_entry)) {
188 assertArrayEquals(A_PAYLOAD, IOUtils.toByteArray(inputStream));
189 }
190 }
191 } finally {
192 PathUtils.delete(target);
193 }
194 }
195
196 @Test
197 void testScatterFileWithCompressionAndTargetInMemory() throws IOException {
198 final byte[] B_PAYLOAD = "RBBBBBBS".getBytes();
199 final byte[] A_PAYLOAD = "XAAY".getBytes();
200 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
201 final Path target = fileSystem.getPath("scattertest.zip");
202 final Path scatterFile = fileSystem.getPath("scattertest.notzip");
203 try (ScatterZipOutputStream scatterZipOutputStream = ScatterZipOutputStream.pathBased(scatterFile, Deflater.BEST_COMPRESSION)) {
204
205 final ZipArchiveEntry zab = new ZipArchiveEntry("b.txt");
206 zab.setMethod(ZipEntry.DEFLATED);
207 final ByteArrayInputStream payload = new ByteArrayInputStream(B_PAYLOAD);
208 scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zab, createPayloadSupplier(payload)));
209
210 final ZipArchiveEntry zae = new ZipArchiveEntry("a.txt");
211 zae.setMethod(ZipEntry.DEFLATED);
212 final ByteArrayInputStream payload1 = new ByteArrayInputStream(A_PAYLOAD);
213 scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zae, createPayloadSupplier(payload1)));
214
215 try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(target)) {
216 scatterZipOutputStream.writeTo(outputStream);
217 }
218 }
219 try (ZipFile zf = ZipFile.builder().setPath(target).get()) {
220 final ZipArchiveEntry b_entry = zf.getEntries("b.txt").iterator().next();
221 assertEquals(8, b_entry.getSize());
222 try (InputStream inputStream = zf.getInputStream(b_entry)) {
223 assertArrayEquals(B_PAYLOAD, IOUtils.toByteArray(inputStream));
224 }
225
226 final ZipArchiveEntry a_entry = zf.getEntries("a.txt").iterator().next();
227 assertEquals(4, a_entry.getSize());
228 try (InputStream inputStream = zf.getInputStream(a_entry)) {
229 assertArrayEquals(A_PAYLOAD, IOUtils.toByteArray(inputStream));
230 }
231 }
232
233 try (ZipFile zf = new ZipFile(Files.newByteChannel(target, StandardOpenOption.READ), target.getFileName().toString(), StandardCharsets.UTF_8.name(),
234 true)) {
235 final ZipArchiveEntry b_entry = zf.getEntries("b.txt").iterator().next();
236 assertEquals(8, b_entry.getSize());
237 try (InputStream inputStream = zf.getInputStream(b_entry)) {
238 assertArrayEquals(B_PAYLOAD, IOUtils.toByteArray(inputStream));
239 }
240
241 final ZipArchiveEntry a_entry = zf.getEntries("a.txt").iterator().next();
242 assertEquals(4, a_entry.getSize());
243 try (InputStream inputStream = zf.getInputStream(a_entry)) {
244 assertArrayEquals(A_PAYLOAD, IOUtils.toByteArray(inputStream));
245 }
246 }
247
248 try (ZipFile zf = new ZipFile(Files.newByteChannel(target, StandardOpenOption.READ), target.getFileName().toString(), StandardCharsets.UTF_8.name(),
249 true, false)) {
250 final ZipArchiveEntry b_entry = zf.getEntries("b.txt").iterator().next();
251 assertEquals(8, b_entry.getSize());
252 try (InputStream inputStream = zf.getInputStream(b_entry)) {
253 assertArrayEquals(B_PAYLOAD, IOUtils.toByteArray(inputStream));
254 }
255
256 final ZipArchiveEntry a_entry = zf.getEntries("a.txt").iterator().next();
257 assertEquals(4, a_entry.getSize());
258 try (InputStream inputStream = zf.getInputStream(a_entry)) {
259 assertArrayEquals(A_PAYLOAD, IOUtils.toByteArray(inputStream));
260 }
261 }
262
263 }
264 }
265
266 @Test
267 void testScatterFileWithCompressionInMemory() throws IOException {
268 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
269 final Path scatterFile = fileSystem.getPath("scattertest.notzip");
270 final Path target = Files.createTempFile(dir, "scattertest", ".zip");
271 final byte[] B_PAYLOAD = "RBBBBBBS".getBytes();
272 final byte[] A_PAYLOAD = "XAAY".getBytes();
273 try (ScatterZipOutputStream scatterZipOutputStream = ScatterZipOutputStream.pathBased(scatterFile, Deflater.BEST_COMPRESSION)) {
274
275 final ZipArchiveEntry zab = new ZipArchiveEntry("b.txt");
276 zab.setMethod(ZipEntry.DEFLATED);
277 final ByteArrayInputStream payload = new ByteArrayInputStream(B_PAYLOAD);
278 scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zab, createPayloadSupplier(payload)));
279
280 final ZipArchiveEntry zae = new ZipArchiveEntry("a.txt");
281 zae.setMethod(ZipEntry.DEFLATED);
282 final ByteArrayInputStream payload1 = new ByteArrayInputStream(A_PAYLOAD);
283 scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zae, createPayloadSupplier(payload1)));
284
285 try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(target)) {
286 scatterZipOutputStream.writeTo(outputStream);
287 }
288 }
289
290 try (ZipFile zf = ZipFile.builder().setPath(target).get()) {
291 final ZipArchiveEntry b_entry = zf.getEntries("b.txt").iterator().next();
292 assertEquals(8, b_entry.getSize());
293 try (InputStream inputStream = zf.getInputStream(b_entry)) {
294 assertArrayEquals(B_PAYLOAD, IOUtils.toByteArray(inputStream));
295 }
296
297 final ZipArchiveEntry a_entry = zf.getEntries("a.txt").iterator().next();
298 assertEquals(4, a_entry.getSize());
299 try (InputStream inputStream = zf.getInputStream(a_entry)) {
300 assertArrayEquals(A_PAYLOAD, IOUtils.toByteArray(inputStream));
301 }
302 }
303 }
304
305 }
306
307 @Test
308 void testZipFileInMemory() throws IOException {
309 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
310 final Path scatterFile = fileSystem.getPath("scattertest.notzip");
311 final Path target = fileSystem.getPath("scattertest.zip");
312 final byte[] B_PAYLOAD = "RBBBBBBS".getBytes();
313 final byte[] A_PAYLOAD = "XAAY".getBytes();
314 try (ScatterZipOutputStream scatterZipOutputStream = ScatterZipOutputStream.pathBased(scatterFile, Deflater.BEST_COMPRESSION)) {
315
316 final ZipArchiveEntry zab = new ZipArchiveEntry("b.txt");
317 zab.setMethod(ZipEntry.DEFLATED);
318 final ByteArrayInputStream payload = new ByteArrayInputStream(B_PAYLOAD);
319 scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zab, createPayloadSupplier(payload)));
320
321 final ZipArchiveEntry zae = new ZipArchiveEntry("a.txt");
322 zae.setMethod(ZipEntry.DEFLATED);
323 final ByteArrayInputStream payload1 = new ByteArrayInputStream(A_PAYLOAD);
324 scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zae, createPayloadSupplier(payload1)));
325
326 try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(target)) {
327 scatterZipOutputStream.writeTo(outputStream);
328 }
329 }
330
331 try (ZipFile zf = new ZipFile(target)) {
332 final ZipArchiveEntry b_entry = zf.getEntries("b.txt").iterator().next();
333 assertEquals(8, b_entry.getSize());
334 try (InputStream inputStream = zf.getInputStream(b_entry)) {
335 assertArrayEquals(B_PAYLOAD, IOUtils.toByteArray(inputStream));
336 }
337
338 final ZipArchiveEntry a_entry = zf.getEntries("a.txt").iterator().next();
339 assertEquals(4, a_entry.getSize());
340 try (InputStream inputStream = zf.getInputStream(a_entry)) {
341 assertArrayEquals(A_PAYLOAD, IOUtils.toByteArray(inputStream));
342 }
343 }
344 }
345 }
346
347 @Test
348 void testZipFromMemoryFileSystemFile() throws IOException, NoSuchAlgorithmException {
349 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
350 final Path textFileInMemSys = fileSystem.getPath("test.txt");
351 final byte[] bytes = new byte[100 * 1024];
352 SecureRandom.getInstanceStrong().nextBytes(bytes);
353 Files.write(textFileInMemSys, bytes);
354
355 final Path zipInLocalSys = Files.createTempFile(dir, "commons-compress-memoryfs", ".zip");
356 try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(zipInLocalSys.toFile())) {
357 final ZipArchiveEntry entry = new ZipArchiveEntry(textFileInMemSys, textFileInMemSys.getFileName().toString());
358 entry.setSize(Files.size(textFileInMemSys));
359 outputStream.putArchiveEntry(entry);
360 outputStream.write(textFileInMemSys);
361 outputStream.closeArchiveEntry();
362 outputStream.finish();
363 assertEquals(Files.size(zipInLocalSys), outputStream.getBytesWritten());
364 }
365 }
366 }
367
368 @Test
369 void testZipFromMemoryFileSystemOutputStream() throws IOException, ArchiveException {
370 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
371 final Path path = fileSystem.getPath("test.txt");
372 Files.write(path, "Test".getBytes(UTF_8));
373 final Path f = Files.createTempFile(dir, "commons-compress-memoryfs", ".zip");
374 try (OutputStream out = Files.newOutputStream(f);
375 ArchiveOutputStream<ZipArchiveEntry> outputStream = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(ArchiveStreamFactory.ZIP, out)) {
376 final ZipArchiveEntry entry = new ZipArchiveEntry(path, path.getFileName().toString());
377 entry.setSize(Files.size(path));
378 outputStream.putArchiveEntry(entry);
379 outputStream.write(path);
380 outputStream.closeArchiveEntry();
381 assertEquals(Files.size(f), outputStream.getBytesWritten());
382 }
383 }
384 }
385
386 @Test
387 void testZipFromMemoryFileSystemPath() throws IOException, NoSuchAlgorithmException {
388 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
389 final Path textFileInMemSys = fileSystem.getPath("test.txt");
390 final byte[] bytes = new byte[100 * 1024];
391 SecureRandom.getInstanceStrong().nextBytes(bytes);
392 Files.write(textFileInMemSys, bytes);
393 final Path zipInLocalSys = Files.createTempFile(dir, "commons-compress-memoryfs", ".zip");
394 try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(zipInLocalSys)) {
395 final ZipArchiveEntry entry = new ZipArchiveEntry(textFileInMemSys, textFileInMemSys.getFileName().toString());
396 entry.setSize(Files.size(textFileInMemSys));
397 outputStream.putArchiveEntry(entry);
398 outputStream.write(textFileInMemSys);
399 outputStream.closeArchiveEntry();
400 outputStream.finish();
401 assertEquals(Files.size(zipInLocalSys), outputStream.getBytesWritten());
402 }
403 }
404 }
405
406 @Test
407 void testZipFromMemoryFileSystemSeekableByteChannel() throws IOException, NoSuchAlgorithmException {
408 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
409 final Path textFileInMemSys = fileSystem.getPath("test.txt");
410 final byte[] bytes = new byte[100 * 1024];
411 SecureRandom.getInstanceStrong().nextBytes(bytes);
412 Files.write(textFileInMemSys, bytes);
413 final Path zipInLocalSys = Files.createTempFile(dir, "commons-compress-memoryfs", ".zip");
414 try (SeekableByteChannel byteChannel = Files.newByteChannel(zipInLocalSys,
415 EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING));
416 ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(byteChannel)) {
417 final ZipArchiveEntry entry = new ZipArchiveEntry(textFileInMemSys, textFileInMemSys.getFileName().toString());
418 entry.setSize(Files.size(textFileInMemSys));
419 outputStream.putArchiveEntry(entry);
420 outputStream.write(textFileInMemSys);
421 outputStream.closeArchiveEntry();
422 outputStream.finish();
423 assertEquals(Files.size(zipInLocalSys), outputStream.getBytesWritten());
424 }
425 }
426 }
427
428 @Test
429 void testZipFromMemoryFileSystemSplitFile() throws IOException, NoSuchAlgorithmException {
430 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
431 final Path textFileInMemSys = fileSystem.getPath("test.txt");
432 final byte[] bytes = new byte[100 * 1024];
433 SecureRandom.getInstanceStrong().nextBytes(bytes);
434 Files.write(textFileInMemSys, bytes);
435 final Path zipInLocalSys = Files.createTempFile(dir, "commons-compress-memoryfs", ".zip");
436 try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(zipInLocalSys.toFile(), 64 * 1024L)) {
437 final ZipArchiveEntry entry = new ZipArchiveEntry(textFileInMemSys, textFileInMemSys.getFileName().toString());
438 entry.setSize(Files.size(textFileInMemSys));
439 outputStream.putArchiveEntry(entry);
440 outputStream.write(textFileInMemSys);
441 outputStream.closeArchiveEntry();
442 outputStream.finish();
443 final List<Path> splitZips;
444 try (Stream<Path> paths = Files.walk(dir, 1)) {
445 splitZips = paths.filter(Files::isRegularFile).peek(path -> println("Found: " + path.toAbsolutePath())).collect(Collectors.toList());
446 }
447 assertEquals(splitZips.size(), 2);
448 assertEquals(Files.size(splitZips.get(0)) + Files.size(splitZips.get(1)) - 4, outputStream.getBytesWritten());
449 }
450 }
451
452 }
453
454 @Test
455 void testZipToMemoryFileSystemOutputStream() throws IOException, ArchiveException {
456 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
457 final Path p = fileSystem.getPath("target.zip");
458
459 try (OutputStream out = Files.newOutputStream(p);
460 ArchiveOutputStream<ZipArchiveEntry> zipOut = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(ArchiveStreamFactory.ZIP, out)) {
461 final String content = "Test";
462 final ZipArchiveEntry entry = new ZipArchiveEntry("test.txt");
463 entry.setSize(content.length());
464 zipOut.putArchiveEntry(entry);
465
466 zipOut.writeUtf8("Test");
467 zipOut.closeArchiveEntry();
468
469 assertTrue(Files.exists(p));
470 assertEquals(Files.size(p), zipOut.getBytesWritten());
471 }
472 }
473 }
474
475 @Test
476 void testZipToMemoryFileSystemPath() throws IOException {
477 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
478 final Path zipInMemSys = fileSystem.getPath("target.zip");
479
480 try (ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(zipInMemSys)) {
481 final String content = "Test";
482 final ZipArchiveEntry entry = new ZipArchiveEntry("test.txt");
483 entry.setSize(content.length());
484 zipOut.putArchiveEntry(entry);
485
486 zipOut.writeUtf8("Test");
487 zipOut.closeArchiveEntry();
488
489 assertTrue(Files.exists(zipInMemSys));
490 assertEquals(Files.size(zipInMemSys), zipOut.getBytesWritten());
491 }
492 }
493 }
494
495 @Test
496 void testZipToMemoryFileSystemSeekableByteChannel() throws IOException {
497 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
498 final Path zipInMemSys = fileSystem.getPath("target.zip");
499
500 try (SeekableByteChannel byteChannel = Files.newByteChannel(zipInMemSys,
501 EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE_NEW));
502 ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(byteChannel)) {
503 final String content = "Test";
504 final ZipArchiveEntry entry = new ZipArchiveEntry("test.txt");
505 entry.setSize(content.length());
506 zipOut.putArchiveEntry(entry);
507
508 zipOut.writeUtf8("Test");
509 zipOut.closeArchiveEntry();
510
511 assertTrue(Files.exists(zipInMemSys));
512 assertEquals(Files.size(zipInMemSys), zipOut.getBytesWritten());
513 }
514 }
515 }
516
517 @Test
518 void testZipToMemoryFileSystemSplitPath() throws IOException, NoSuchAlgorithmException {
519 try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
520 final Path zipInMemSys = fileSystem.getPath("target.zip");
521 final byte[] bytes = new byte[100 * 1024];
522 SecureRandom.getInstanceStrong().nextBytes(bytes);
523
524 try (ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(zipInMemSys, 64 * 1024L)) {
525 final ZipArchiveEntry entry = new ZipArchiveEntry("test.txt");
526 entry.setSize(bytes.length);
527 zipOut.putArchiveEntry(entry);
528
529 zipOut.write(bytes);
530
531 zipOut.closeArchiveEntry();
532 zipOut.finish();
533
534 final List<Path> splitZips;
535 try (Stream<Path> paths = Files.walk(fileSystem.getPath("."), 1)) {
536 splitZips = paths.filter(Files::isRegularFile).peek(path -> println("Found: " + path.toAbsolutePath())).collect(Collectors.toList());
537 }
538 assertEquals(splitZips.size(), 2);
539 assertEquals(Files.size(splitZips.get(0)) + Files.size(splitZips.get(1)) - 4, zipOut.getBytesWritten());
540 }
541 }
542
543 }
544 }