@@ -16,7 +16,7 @@ We reuse standard `java.util.zip.ZipOutputStream` and
16
16
1 . Collect all zip entries and their bytes for each input file in parallel.
17
17
For each input file:
18
18
- Get a ` ByteArrayOutputStream ` and a ` ZipOutputStream ` on top of it
19
- - Write an entry to a zip stream. Do not close it to avoid writing an unneeded central directory
19
+ - Write an entry to the zip stream skipping the central directory using ` SingleEntryZipOutputStream ` .
20
20
- Get the bytes from the byte stream
21
21
22
22
``` java
@@ -25,33 +25,35 @@ We reuse standard `java.util.zip.ZipOutputStream` and
25
25
// for each input file in parallel:
26
26
var out = new ByteArrayOutputStream ();
27
27
var zipEntry = new ZipEntry (filePathRelativeToZipRoot);
28
- var zip = new ZipOutputStream (out);
29
- try (var fileStream = Files . newInputStream(filePath)) {
30
- zip. putNextEntry(zipEntry);
31
- fileStream. transferTo(zip);
32
- zip. closeEntry();
28
+ try (var zip = new SingleEntryZipOutputStream (out)) {
29
+ try (var fileStream = Files . newInputStream(filePath)) {
30
+ zip. putNextEntry(zipEntry);
31
+ fileStream. transferTo(zip);
32
+ zip. closeEntry();
33
+ }
33
34
}
34
35
zipEntries. put(zipEntry, out. toByteArray());
35
36
```
36
37
37
38
2 . Write all entries and bytes sequentially to a target zip file:
38
39
- Get a ` FileOutputStream ` and a ` ZipOutputStream ` on top of it
39
- - Write bytes of all entries to a file stream updating zip stream state
40
- - Write the central directory by closing the zip stream
40
+ - Write bytes of all entries to the file stream updating the zip stream state
41
+ - Write the central directory by closing the zip stream as usual
41
42
42
43
``` java
43
44
try (var os = Files . newOutputStream(zipFile)) {
44
- var zip = new ZipOutputStream (os);
45
- var offset = 0L ;
46
- for (Map . Entry<ZipEntry , byte[]> o : zipEntries. entrySet()) {
47
- var zipEntry = o. getKey();
48
- var bytes = o. getValue();
49
- zip. xEntries. add(new XEntry (zipEntry, offset)); // via reflection
50
- os. write(bytes);
51
- offset += bytes. length;
45
+ try (var zip = new ZipOutputStream (os)) {
46
+ var offset = 0L ;
47
+ for (Map . Entry<ZipEntry , byte[]> o : zipEntries. entrySet()) {
48
+ var zipEntry = o. getKey();
49
+ var bytes = o. getValue();
50
+ zip. xEntries. add(new XEntry (zipEntry, offset)); // via reflection
51
+ os. write(bytes);
52
+ offset += bytes. length;
53
+ }
54
+ zip. offset = offset; // via reflection
55
+ // write the central directory on close
52
56
}
53
- zip. offset = offset; // via reflection
54
- zip. close();
55
57
}
56
58
```
57
59
0 commit comments