|
3 | 3 | import net.neoforged.neoform.runtime.engine.ProcessingEnvironment; |
4 | 4 |
|
5 | 5 | import java.io.BufferedInputStream; |
6 | | -import java.io.BufferedOutputStream; |
7 | 6 | import java.io.IOException; |
| 7 | +import java.nio.file.FileSystems; |
8 | 8 | import java.nio.file.Files; |
9 | | -import java.util.List; |
10 | | -import java.util.zip.ZipException; |
| 9 | +import java.util.Map; |
11 | 10 | import java.util.zip.ZipInputStream; |
12 | | -import java.util.zip.ZipOutputStream; |
13 | 11 |
|
14 | | -// TODO: would be good to "merge" with MergeWithSourcesAction? |
15 | 12 | public class MergeZipsAction extends BuiltInAction { |
16 | 13 | @Override |
17 | 14 | public void run(ProcessingEnvironment environment) throws IOException, InterruptedException { |
18 | 15 | var classesFile = environment.getRequiredInputPath("classes"); |
19 | 16 | var extraClassesFile = environment.getRequiredInputPath("classes2"); |
20 | 17 | var output = environment.getOutputPath("output"); |
21 | 18 |
|
22 | | - try (var os = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(output)))) { |
| 19 | + // Copy the largest jar then use NIO to insert extra entries. |
| 20 | + // This is faster than working with ZipFile streams which inflate and deflate all the entries. |
| 21 | + Files.copy(classesFile, output); |
| 22 | + try (var zfs = FileSystems.newFileSystem(output, Map.of("create", false))) { |
| 23 | + var zfsRoot = zfs.getPath("/"); |
23 | 24 |
|
24 | | - boolean first = true; |
25 | | - for (var sourceZip : List.of(classesFile, extraClassesFile)) { |
26 | | - try (var in = new ZipInputStream(new BufferedInputStream(Files.newInputStream(sourceZip)))) { |
27 | | - for (var entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) { |
28 | | - if (!first && entry.isDirectory()) { |
29 | | - // Skip directories in case they would be duplicated |
30 | | - // TODO: a bit crude |
31 | | - continue; |
32 | | - } |
33 | | - os.putNextEntry(entry); |
34 | | - in.transferTo(os); |
35 | | - os.closeEntry(); |
| 25 | + // Copy the extra classes to the output zip |
| 26 | + try (var in = new ZipInputStream(new BufferedInputStream(Files.newInputStream(extraClassesFile)))) { |
| 27 | + for (var entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) { |
| 28 | + if (entry.isDirectory()) { |
| 29 | + continue; |
36 | 30 | } |
| 31 | + var targetPath = zfsRoot.resolve(entry.getName()); |
| 32 | + Files.copy(in, targetPath); |
37 | 33 | } |
38 | | - first = false; |
39 | 34 | } |
40 | 35 | } |
41 | 36 | } |
|
0 commit comments