Skip to content

Commit 1dd2086

Browse files
committed
Use NIO trick once again
1 parent e8bf026 commit 1dd2086

1 file changed

Lines changed: 14 additions & 19 deletions

File tree

src/main/java/net/neoforged/neoform/runtime/actions/MergeZipsAction.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,34 @@
33
import net.neoforged.neoform.runtime.engine.ProcessingEnvironment;
44

55
import java.io.BufferedInputStream;
6-
import java.io.BufferedOutputStream;
76
import java.io.IOException;
7+
import java.nio.file.FileSystems;
88
import java.nio.file.Files;
9-
import java.util.List;
10-
import java.util.zip.ZipException;
9+
import java.util.Map;
1110
import java.util.zip.ZipInputStream;
12-
import java.util.zip.ZipOutputStream;
1311

14-
// TODO: would be good to "merge" with MergeWithSourcesAction?
1512
public class MergeZipsAction extends BuiltInAction {
1613
@Override
1714
public void run(ProcessingEnvironment environment) throws IOException, InterruptedException {
1815
var classesFile = environment.getRequiredInputPath("classes");
1916
var extraClassesFile = environment.getRequiredInputPath("classes2");
2017
var output = environment.getOutputPath("output");
2118

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("/");
2324

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;
3630
}
31+
var targetPath = zfsRoot.resolve(entry.getName());
32+
Files.copy(in, targetPath);
3733
}
38-
first = false;
3934
}
4035
}
4136
}

0 commit comments

Comments
 (0)