Skip to content

Commit

Permalink
Fix and test writing uncompressed entries to zip files. (#1139)
Browse files Browse the repository at this point in the history
* Fix and test writing uncompressed entries to zip files.

* Minor code cleanup
  • Loading branch information
modmuss50 committed Jul 4, 2024
1 parent 462ba97 commit fa8bf5e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/main/java/net/fabricmc/loom/util/ZipReprocessorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Calendar;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
Expand Down Expand Up @@ -122,7 +123,12 @@ public static void reprocessZip(Path file, boolean reproducibleFileOrder, boolea
}

newEntry.setMethod(zipEntryCompressionMethod(zipEntryCompression));
copyZipEntry(zipOutputStream, newEntry, zipFile.getInputStream(entry));

if (zipEntryCompression == ZipEntryCompression.STORED) {
copyUncompressedZipEntry(zipOutputStream, newEntry, zipFile.getInputStream(entry));
} else {
copyZipEntry(zipOutputStream, newEntry, zipFile.getInputStream(entry));
}
}
}
}
Expand Down Expand Up @@ -176,6 +182,21 @@ private static void copyZipEntry(ZipOutputStream zipOutputStream, ZipEntry entry
zipOutputStream.closeEntry();
}

private static void copyUncompressedZipEntry(ZipOutputStream zipOutputStream, ZipEntry entry, InputStream inputStream) throws IOException {
// We need to read the entire input stream to calculate the CRC32 checksum and the size of the entry.
final byte[] data = inputStream.readAllBytes();

var crc = new CRC32();
crc.update(data);
entry.setCrc(crc.getValue());
entry.setSize(data.length);
entry.setCompressedSize(data.length);

zipOutputStream.putNextEntry(entry);
zipOutputStream.write(data, 0, data.length);
zipOutputStream.closeEntry();
}

private static void setConstantFileTime(ZipEntry entry) {
// See https://github.com/openjdk/jdk/blob/master/test/jdk/java/util/zip/ZipFile/ZipEntryTimeBounds.java
entry.setTime(new GregorianCalendar(1980, Calendar.JANUARY, 1, 0, 0, 0).getTimeInMillis());
Expand Down
18 changes: 18 additions & 0 deletions src/test/groovy/net/fabricmc/loom/test/unit/ZipUtilsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import java.nio.file.Files
import java.time.ZoneId

import com.google.gson.JsonObject
import org.gradle.api.tasks.bundling.ZipEntryCompression
import spock.lang.Specification

import net.fabricmc.loom.util.Checksum
Expand Down Expand Up @@ -240,4 +241,21 @@ class ZipUtilsTest extends Specification {
then:
thrown FileSystemUtil.UnrecoverableZipException
}

def "reprocess uncompressed"() {
given:
// Create a reproducible input zip
def dir = Files.createTempDirectory("loom-zip-test")
def zip = Files.createTempFile("loom-zip-test", ".zip")
def fileInside = dir.resolve("text.txt")
Files.writeString(fileInside, "hello world")
ZipUtils.pack(dir, zip)

when:
ZipReprocessorUtil.reprocessZip(zip, true, false, ZipEntryCompression.STORED)

then:
ZipUtils.unpack(zip, "text.txt") == "hello world".bytes
Checksum.sha1Hex(zip) == "e699fa52a520553241aac798f72255ac0a912b05"
}
}

0 comments on commit fa8bf5e

Please sign in to comment.