mirror of
https://github.com/architectury/architectury-loom.git
synced 2026-04-03 05:57:42 -05:00
Fix and test writing uncompressed entries to zip files. (#1139)
* Fix and test writing uncompressed entries to zip files. * Minor code cleanup
This commit is contained in:
@@ -32,6 +32,7 @@ import java.nio.file.StandardCopyOption;
|
||||
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;
|
||||
@@ -122,7 +123,12 @@ public class ZipReprocessorUtil {
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -176,6 +182,21 @@ public class ZipReprocessorUtil {
|
||||
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());
|
||||
|
||||
Reference in New Issue
Block a user