If we're creating a .jar file, use a JarOutputStream.
JOS stuffs some secret data into the zip extension slot, so we can't just create a .jar file with a ZipOutputStream and call it a day.
This commit is contained in:
@@ -11,6 +11,7 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.LineNumberReader;
|
import java.io.LineNumberReader;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -18,6 +19,7 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.jar.JarOutputStream;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
@@ -26,13 +28,13 @@ import com.threerings.getdown.util.ProgressObserver;
|
|||||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies a jardiff patch to a jar file.
|
* Applies a jardiff patch to a jar/zip file.
|
||||||
*/
|
*/
|
||||||
public class JarDiffPatcher implements JarDiffCodes
|
public class JarDiffPatcher implements JarDiffCodes
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Patches the specified jar file using the supplied patch file and writing
|
* Patches the specified jar file using the supplied patch file and writing the new jar file to
|
||||||
* the new jar file to the supplied target.
|
* the supplied target.
|
||||||
*
|
*
|
||||||
* @param jarPath the path to the original jar file.
|
* @param jarPath the path to the original jar file.
|
||||||
* @param diffPath the path to the jardiff patch file.
|
* @param diffPath the path to the jardiff patch file.
|
||||||
@@ -46,8 +48,8 @@ public class JarDiffPatcher implements JarDiffCodes
|
|||||||
{
|
{
|
||||||
File oldFile = new File(jarPath), diffFile = new File(diffPath);
|
File oldFile = new File(jarPath), diffFile = new File(diffPath);
|
||||||
try (ZipFile oldJar = new ZipFile(oldFile);
|
try (ZipFile oldJar = new ZipFile(oldFile);
|
||||||
ZipFile jarDiff = new ZipFile(diffFile);
|
ZipFile jarDiff = new ZipFile(diffFile);
|
||||||
ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(target))) {
|
ZipOutputStream jos = makeOutputStream(oldFile, target)) {
|
||||||
Set<String> ignoreSet = new HashSet<>();
|
Set<String> ignoreSet = new HashSet<>();
|
||||||
Map<String, String> renameMap = new HashMap<>();
|
Map<String, String> renameMap = new HashMap<>();
|
||||||
determineNameMapping(jarDiff, ignoreSet, renameMap);
|
determineNameMapping(jarDiff, ignoreSet, renameMap);
|
||||||
@@ -146,8 +148,8 @@ public class JarDiffPatcher implements JarDiffCodes
|
|||||||
for (String name : oldjarNames) {
|
for (String name : oldjarNames) {
|
||||||
ZipEntry entry = oldJar.getEntry(name);
|
ZipEntry entry = oldJar.getEntry(name);
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
// names originally retrieved from the JAR, so this should never happen
|
// names originally retrieved from the archive, so this should never happen
|
||||||
throw new AssertionError("JAR entry not found: " + name);
|
throw new AssertionError("Archive entry not found: " + name);
|
||||||
}
|
}
|
||||||
updateObserver(observer, currentEntry, size);
|
updateObserver(observer, currentEntry, size);
|
||||||
currentEntry++;
|
currentEntry++;
|
||||||
@@ -275,6 +277,15 @@ public class JarDiffPatcher implements JarDiffCodes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static ZipOutputStream makeOutputStream (File source, File target)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
FileOutputStream out = new FileOutputStream(target);
|
||||||
|
if (source.getName().endsWith(".jar")) return new JarOutputStream(out);
|
||||||
|
else if (source.getName().endsWith(".zip")) return new ZipOutputStream(out);
|
||||||
|
else throw new AssertionError("Unsupported source file '" + source + "'. Not a .jar or .zip?");
|
||||||
|
}
|
||||||
|
|
||||||
protected static final int DEFAULT_READ_SIZE = 2048;
|
protected static final int DEFAULT_READ_SIZE = 2048;
|
||||||
|
|
||||||
protected static byte[] newBytes = new byte[DEFAULT_READ_SIZE];
|
protected static byte[] newBytes = new byte[DEFAULT_READ_SIZE];
|
||||||
|
|||||||
Reference in New Issue
Block a user