Skip to content

Commit

Permalink
Support Zip.getEntryCompression() setting (#936)
Browse files Browse the repository at this point in the history
* Support Zip.getEntryCompression() setting

* Checkstyle

* Set for both the entry and ZOS
  • Loading branch information
modmuss50 authored Sep 9, 2023
1 parent 0dc2d67 commit ae159dd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.bundling.ZipEntryCompression;
import org.gradle.build.event.BuildEventsListenerRegistry;
import org.gradle.jvm.tasks.Jar;
import org.gradle.workers.WorkAction;
Expand Down Expand Up @@ -137,6 +138,7 @@ public final <P extends AbstractRemapParams> void submitWork(Class<? extends Abs
params.getArchiveReproducibleFileOrder().set(isReproducibleFileOrder());

params.getJarManifestService().set(jarManifestServiceProvider);
params.getEntryCompression().set(getEntryCompression());

if (getIncludesClientOnlyClasses().get()) {
final List<String> clientOnlyEntries = new ArrayList<>(getClientOnlyEntries(getClientSourceSet()));
Expand All @@ -160,6 +162,7 @@ public interface AbstractRemapParams extends WorkParameters {

Property<Boolean> getArchivePreserveFileTimestamps();
Property<Boolean> getArchiveReproducibleFileOrder();
Property<ZipEntryCompression> getEntryCompression();

Property<JarManifestService> getJarManifestService();
MapProperty<String, String> getManifestAttributes();
Expand Down Expand Up @@ -202,9 +205,10 @@ protected void modifyJarManifest() throws IOException {
protected void rewriteJar() throws IOException {
final boolean isReproducibleFileOrder = getParameters().getArchiveReproducibleFileOrder().get();
final boolean isPreserveFileTimestamps = getParameters().getArchivePreserveFileTimestamps().get();
final ZipEntryCompression compression = getParameters().getEntryCompression().get();

if (isReproducibleFileOrder || !isPreserveFileTimestamps) {
ZipReprocessorUtil.reprocessZip(outputFile.toFile(), isReproducibleFileOrder, isPreserveFileTimestamps);
if (isReproducibleFileOrder || !isPreserveFileTimestamps || compression != ZipEntryCompression.DEFLATED) {
ZipReprocessorUtil.reprocessZip(outputFile.toFile(), isReproducibleFileOrder, isPreserveFileTimestamps, compression);
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/net/fabricmc/loom/util/ZipReprocessorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import org.gradle.api.tasks.bundling.ZipEntryCompression;
import org.intellij.lang.annotations.MagicConstant;

public class ZipReprocessorUtil {
/**
* See {@link org.gradle.api.internal.file.archive.ZipCopyAction} about this.
Expand Down Expand Up @@ -92,6 +95,10 @@ private static int specialOrdering(String name1, String name2) {
}

public static void reprocessZip(File file, boolean reproducibleFileOrder, boolean preserveFileTimestamps) throws IOException {
reprocessZip(file, reproducibleFileOrder, preserveFileTimestamps, ZipEntryCompression.DEFLATED);
}

public static void reprocessZip(File file, boolean reproducibleFileOrder, boolean preserveFileTimestamps, ZipEntryCompression zipEntryCompression) throws IOException {
if (!reproducibleFileOrder && preserveFileTimestamps) {
return;
}
Expand All @@ -111,6 +118,8 @@ public static void reprocessZip(File file, boolean reproducibleFileOrder, boolea
final var outZip = new ByteArrayOutputStream(entries.length);

try (var zipOutputStream = new ZipOutputStream(outZip)) {
zipOutputStream.setMethod(zipOutputStreamCompressionMethod(zipEntryCompression));

for (ZipEntry entry : entries) {
ZipEntry newEntry = entry;

Expand All @@ -119,6 +128,7 @@ public static void reprocessZip(File file, boolean reproducibleFileOrder, boolea
setConstantFileTime(newEntry);
}

newEntry.setMethod(zipEntryCompressionMethod(zipEntryCompression));
copyZipEntry(zipOutputStream, newEntry, zipFile.getInputStream(entry));
}
}
Expand Down Expand Up @@ -177,4 +187,20 @@ private static void setConstantFileTime(ZipEntry entry) {
entry.setLastModifiedTime(FileTime.fromMillis(ZipReprocessorUtil.CONSTANT_TIME_FOR_ZIP_ENTRIES));
entry.setLastAccessTime(FileTime.fromMillis(ZipReprocessorUtil.CONSTANT_TIME_FOR_ZIP_ENTRIES));
}

@MagicConstant(valuesFromClass = ZipOutputStream.class)
private static int zipOutputStreamCompressionMethod(ZipEntryCompression compression) {
return switch (compression) {
case STORED -> ZipOutputStream.STORED;
case DEFLATED -> ZipOutputStream.DEFLATED;
};
}

@MagicConstant(valuesFromClass = ZipEntry.class)
private static int zipEntryCompressionMethod(ZipEntryCompression compression) {
return switch (compression) {
case STORED -> ZipEntry.STORED;
case DEFLATED -> ZipEntry.DEFLATED;
};
}
}

0 comments on commit ae159dd

Please sign in to comment.