From c17882f43205c0add2322dfc26e79b6f76a1a64d Mon Sep 17 00:00:00 2001 From: sergiorussia <22471371+sergiorussia@users.noreply.github.com> Date: Mon, 29 Apr 2019 23:30:50 +0300 Subject: [PATCH] zip files support --- .../threerings/getdown/data/PathBuilder.java | 4 +- .../com/threerings/getdown/data/Resource.java | 42 +++---- .../com/threerings/getdown/tools/Differ.java | 39 +++---- .../com/threerings/getdown/tools/JarDiff.java | 109 +++++++++--------- .../getdown/tools/JarDiffPatcher.java | 83 ++++++------- .../com/threerings/getdown/tools/Patcher.java | 18 ++- .../com/threerings/getdown/util/FileUtil.java | 49 +++++--- 7 files changed, 166 insertions(+), 178 deletions(-) diff --git a/core/src/main/java/com/threerings/getdown/data/PathBuilder.java b/core/src/main/java/com/threerings/getdown/data/PathBuilder.java index b0a1dc9..57e9275 100644 --- a/core/src/main/java/com/threerings/getdown/data/PathBuilder.java +++ b/core/src/main/java/com/threerings/getdown/data/PathBuilder.java @@ -10,7 +10,7 @@ import java.io.IOException; import java.util.LinkedHashSet; import java.util.List; import java.util.concurrent.TimeUnit; -import java.util.jar.JarFile; +import java.util.zip.ZipFile; import com.threerings.getdown.cache.GarbageCollector; import com.threerings.getdown.cache.ResourceCache; @@ -112,7 +112,7 @@ public class PathBuilder if (!unpackedIndicator.exists()) { try { - FileUtil.unpackJar(new JarFile(cachedFile), cachedParent, false); + FileUtil.unpackJar(new ZipFile(cachedFile), cachedParent, false); unpackedIndicator.createNewFile(); } catch (IOException ioe) { log.warning("Failed to unpack native jar", diff --git a/core/src/main/java/com/threerings/getdown/data/Resource.java b/core/src/main/java/com/threerings/getdown/data/Resource.java index 3e2f446..e8f8f1d 100644 --- a/core/src/main/java/com/threerings/getdown/data/Resource.java +++ b/core/src/main/java/com/threerings/getdown/data/Resource.java @@ -5,7 +5,10 @@ package com.threerings.getdown.data; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.security.MessageDigest; import java.util.Collections; @@ -13,13 +16,12 @@ import java.util.Comparator; import java.util.EnumSet; import java.util.List; import java.util.Locale; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import com.threerings.getdown.util.FileUtil; import com.threerings.getdown.util.ProgressObserver; import com.threerings.getdown.util.StringUtil; - import static com.threerings.getdown.Log.log; /** @@ -66,23 +68,22 @@ public class Resource implements Comparable // if this is a jar, we need to compute the digest in a "timestamp and file order" agnostic // manner to properly correlate jardiff patched jars with their unpatched originals if (isJar || isPacked200Jar){ - File tmpJarFile = null; - JarFile jar = null; + ZipFile jar = null; try { // if this is a compressed jar file, uncompress it to compute the jar file digest if (isPacked200Jar){ - tmpJarFile = new File(target.getPath() + ".tmp"); - FileUtil.unpackPacked200Jar(target, tmpJarFile); - jar = new JarFile(tmpJarFile); + File tmpJarFile = new File(target.getPath() + ".tmp"); + tmpJarFile.deleteOnExit(); + jar = FileUtil.unpackPacked200Jar(target, tmpJarFile); } else{ - jar = new JarFile(target); + jar = new ZipFile(target); } - List entries = Collections.list(jar.entries()); + List entries = Collections.list(jar.entries()); Collections.sort(entries, ENTRY_COMP); int eidx = 0; - for (JarEntry entry : entries) { + for (ZipEntry entry : entries) { // old versions of the digest code skipped metadata if (version < 2) { if (entry.getName().startsWith("META-INF")) { @@ -108,9 +109,6 @@ public class Resource implements Comparable log.warning("Error closing jar", "path", target, "jar", jar, "error", ioe); } } - if (tmpJarFile != null) { - FileUtil.deleteHarder(tmpJarFile); - } } } else { @@ -302,7 +300,7 @@ public class Resource implements Comparable throw new IOException("Requested to unpack non-jar file '" + _local + "'."); } if (_isJar) { - try (JarFile jar = new JarFile(_local)) { + try (ZipFile jar = new ZipFile(_local)) { FileUtil.unpackJar(jar, _unpacked, _attrs.contains(Attr.CLEAN)); } } else { @@ -341,11 +339,7 @@ public class Resource implements Comparable @Override public boolean equals (Object other) { - if (other instanceof Resource) { - return _path.equals(((Resource)other)._path); - } else { - return false; - } + return other instanceof Resource && _path.equals(((Resource) other)._path); } @Override public int hashCode () @@ -368,7 +362,7 @@ public class Resource implements Comparable protected static boolean isJar (String path) { - return path.endsWith(".jar") || path.endsWith(".jar_new"); + return path.endsWith(".jar") || path.endsWith(".jar_new") || path.endsWith(".zip"); } protected static boolean isPacked200Jar (String path) @@ -384,8 +378,8 @@ public class Resource implements Comparable protected boolean _isJar, _isPacked200Jar; /** Used to sort the entries in a jar file. */ - protected static final Comparator ENTRY_COMP = new Comparator() { - @Override public int compare (JarEntry e1, JarEntry e2) { + protected static final Comparator ENTRY_COMP = new Comparator() { + @Override public int compare (ZipEntry e1, ZipEntry e2) { return e1.getName().compareTo(e2.getName()); } }; diff --git a/core/src/main/java/com/threerings/getdown/tools/Differ.java b/core/src/main/java/com/threerings/getdown/tools/Differ.java index c2e740b..31a0f0a 100644 --- a/core/src/main/java/com/threerings/getdown/tools/Differ.java +++ b/core/src/main/java/com/threerings/getdown/tools/Differ.java @@ -11,15 +11,12 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; - +import java.security.MessageDigest; import java.util.ArrayList; import java.util.Enumeration; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; -import java.util.jar.JarOutputStream; import java.util.zip.ZipEntry; - -import java.security.MessageDigest; +import java.util.zip.ZipFile; +import java.util.zip.ZipOutputStream; import com.threerings.getdown.data.Application; import com.threerings.getdown.data.Digest; @@ -91,15 +88,15 @@ public class Differ } } - protected void createPatch (File patch, ArrayList orsrcs, - ArrayList nrsrcs, boolean verbose) + protected static void createPatch(File patch, ArrayList orsrcs, + ArrayList nrsrcs, boolean verbose) throws IOException { int version = Digest.VERSION; MessageDigest md = Digest.getMessageDigest(version); try (FileOutputStream fos = new FileOutputStream(patch); BufferedOutputStream buffered = new BufferedOutputStream(fos); - JarOutputStream jout = new JarOutputStream(buffered)) { + ZipOutputStream jout = new ZipOutputStream(buffered)) { // for each file in the new application, it either already exists // in the old application, or it is new @@ -168,17 +165,16 @@ public class Differ } } - protected File rebuildJar (File target) - throws IOException + protected static File rebuildJar(File target) throws IOException { File temp = File.createTempFile("differ", "jar"); - try (JarFile jar = new JarFile(target); - FileOutputStream tempFos = new FileOutputStream(temp); - BufferedOutputStream tempBos = new BufferedOutputStream(tempFos); - JarOutputStream jout = new JarOutputStream(tempBos)) { + try (ZipFile jar = new ZipFile(target); + FileOutputStream tempFos = new FileOutputStream(temp); + BufferedOutputStream tempBos = new BufferedOutputStream(tempFos); + ZipOutputStream jout = new ZipOutputStream(tempBos)) { byte[] buffer = new byte[4096]; - for (Enumeration< JarEntry > iter = jar.entries(); iter.hasMoreElements();) { - JarEntry entry = iter.nextElement(); + for (Enumeration iter = jar.entries(); iter.hasMoreElements();) { + ZipEntry entry = iter.nextElement(); entry.setCompressedSize(-1); jout.putNextEntry(entry); try (InputStream in = jar.getInputStream(entry)) { @@ -193,8 +189,7 @@ public class Differ return temp; } - protected void jarDiff (File ofile, File nfile, JarOutputStream jout) - throws IOException + protected static void jarDiff(File ofile, File nfile, ZipOutputStream jout) throws IOException { JarDiff.createPatch(ofile.getPath(), nfile.getPath(), jout, false); } @@ -202,8 +197,7 @@ public class Differ public static void main (String[] args) { if (args.length < 2) { - System.err.println( - "Usage: Differ [-verbose] new_vers_dir old_vers_dir"); + System.err.println("Usage: Differ [-verbose] new_vers_dir old_vers_dir"); System.exit(255); } Differ differ = new Differ(); @@ -222,8 +216,7 @@ public class Differ } } - protected static void pipe (File file, JarOutputStream jout) - throws IOException + protected static void pipe (File file, ZipOutputStream jout) throws IOException { try (FileInputStream fin = new FileInputStream(file)) { StreamUtil.copy(fin, jout); diff --git a/core/src/main/java/com/threerings/getdown/tools/JarDiff.java b/core/src/main/java/com/threerings/getdown/tools/JarDiff.java index 1cea0ea..90ddf0c 100644 --- a/core/src/main/java/com/threerings/getdown/tools/JarDiff.java +++ b/core/src/main/java/com/threerings/getdown/tools/JarDiff.java @@ -41,9 +41,17 @@ package com.threerings.getdown.tools; -import java.io.*; +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.StringWriter; +import java.io.Writer; import java.util.*; -import java.util.jar.*; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipOutputStream; import static java.nio.charset.StandardCharsets.UTF_8; @@ -66,18 +74,16 @@ public class JarDiff implements JarDiffCodes private static boolean _debug; /** - * Creates a patch from the two passed in files, writing the result to os. + * Creates a patch from the two passed in files, writing the result to {@code os}. */ public static void createPatch (String oldPath, String newPath, OutputStream os, boolean minimal) throws IOException { - try (JarFile2 oldJar = new JarFile2(oldPath); - JarFile2 newJar = new JarFile2(newPath)) { - - HashMap moved = new HashMap<>(); - HashSet implicit = new HashSet<>(); - HashSet moveSrc = new HashSet<>(); - HashSet newEntries = new HashSet<>(); + try (JarFile2 oldJar = new JarFile2(oldPath); JarFile2 newJar = new JarFile2(newPath)) { + final Map moved = new HashMap<>(); + final Set implicit = new HashSet<>(); + final Set moveSrc = new HashSet<>(); + final Set newEntries = new HashSet<>(); // FIRST PASS // Go through the entries in new jar and @@ -86,7 +92,7 @@ public class JarDiff implements JarDiffCodes // and new.jar ) // and for files that cannot be implicitly moved, we will either // find out whether it is moved or new (modified) - for (JarEntry newEntry : newJar) { + for (ZipEntry newEntry : newJar) { String newname = newEntry.getName(); // Return best match of contents, will return a name match if possible @@ -122,7 +128,6 @@ public class JarDiff implements JarDiffCodes // for backward compatibility if (_debug) { - System.out.println("NEW: "+ newname); } newEntries.add(newname); @@ -136,12 +141,9 @@ public class JarDiff implements JarDiffCodes } // Check if this disables an implicit 'move ' if (implicit.contains(oldname) && minimal) { - if (_debug) { System.err.println("implicit.remove " + oldname); - System.err.println("moved.put " + oldname + " " + oldname); - } implicit.remove(oldname); moved.put(oldname, oldname); @@ -153,8 +155,8 @@ public class JarDiff implements JarDiffCodes // SECOND PASS: = - - // - - ArrayList deleted = new ArrayList<>(); - for (JarEntry oldEntry : oldJar) { + final List deleted = new ArrayList<>(); + for (ZipEntry oldEntry : oldJar) { String oldName = oldEntry.getName(); if (!implicit.contains(oldName) && !moveSrc.contains(oldName) && !newEntries.contains(oldName)) { @@ -180,7 +182,7 @@ public class JarDiff implements JarDiffCodes } } - JarOutputStream jos = new JarOutputStream(os); + ZipOutputStream jos = new ZipOutputStream(os); // Write out all the MOVEs and REMOVEs createIndex(jos, deleted, moved); @@ -199,12 +201,11 @@ public class JarDiff implements JarDiffCodes } /** - * Writes the index file out to jos. - * oldEntries gives the names of the files that were removed, - * movedMap maps from the new name to the old name. + * Writes the index file out to {@code jos}. + * {@code oldEntries} gives the names of the files that were removed, + * {@code movedMap} maps from the new name to the old name. */ - private static void createIndex (JarOutputStream jos, List oldEntries, - Map movedMap) + private static void createIndex (ZipOutputStream jos, List oldEntries, Map movedMap) throws IOException { StringWriter writer = new StringWriter(); @@ -220,22 +221,22 @@ public class JarDiff implements JarDiffCodes } // And those that have moved - for (String newName : movedMap.keySet()) { - String oldName = movedMap.get(newName); + for (Map.Entry entry : movedMap.entrySet()) { + String oldName = entry.getValue(); writer.write(MOVE_COMMAND); writer.write(" "); writeEscapedString(writer, oldName); writer.write(" "); - writeEscapedString(writer, newName); + writeEscapedString(writer, entry.getKey()); writer.write("\r\n"); } - jos.putNextEntry(new JarEntry(INDEX_NAME)); + jos.putNextEntry(new ZipEntry(INDEX_NAME)); byte[] bytes = writer.toString().getBytes(UTF_8); jos.write(bytes, 0, bytes.length); } - protected static Writer writeEscapedString (Writer writer, String string) + protected static void writeEscapedString (Writer writer, String string) throws IOException { int index = 0; @@ -261,10 +262,9 @@ public class JarDiff implements JarDiffCodes writer.write(string); } - return writer; } - private static void writeEntry (JarOutputStream jos, JarEntry entry, JarFile2 file) + private static void writeEntry (ZipOutputStream jos, ZipEntry entry, JarFile2 file) throws IOException { try (InputStream data = file.getJarFile().getInputStream(entry)) { @@ -278,31 +278,31 @@ public class JarDiff implements JarDiffCodes } /** - * JarFile2 wraps a JarFile providing some convenience methods. + * JarFile2 wraps a ZipFile providing some convenience methods. */ - private static class JarFile2 implements Iterable, Closeable + private static class JarFile2 implements Iterable, Closeable { - private JarFile _jar; - private List _entries; - private HashMap _nameToEntryMap; - private HashMap> _crcToEntryMap; + private ZipFile _jar; + private List _entries; + private HashMap _nameToEntryMap; + private HashMap> _crcToEntryMap; public JarFile2 (String path) throws IOException { - _jar = new JarFile(new File(path)); + _jar = new ZipFile(new File(path)); index(); } - public JarFile getJarFile () { + public ZipFile getJarFile () { return _jar; } - // from interface Iterable + // from interface Iterable @Override - public Iterator iterator () { + public Iterator iterator () { return _entries.iterator(); } - public JarEntry getEntryByName (String name) { + public ZipEntry getEntryByName (String name) { return _nameToEntryMap.get(name); } @@ -350,7 +350,7 @@ public class JarDiff implements JarDiffCodes return retVal; } - public String getBestMatch (JarFile2 file, JarEntry entry) throws IOException { + public String getBestMatch (JarFile2 file, ZipEntry entry) throws IOException { // check for same name and same content, return name if found if (contains(file, entry)) { return (entry.getName()); @@ -360,9 +360,8 @@ public class JarDiff implements JarDiffCodes return (hasSameContent(file,entry)); } - public boolean contains (JarFile2 f, JarEntry e) throws IOException { - - JarEntry thisEntry = getEntryByName(e.getName()); + public boolean contains (JarFile2 f, ZipEntry e) throws IOException { + ZipEntry thisEntry = getEntryByName(e.getName()); // Look up name in 'this' Jar2File - if not exist return false if (thisEntry == null) @@ -379,17 +378,17 @@ public class JarDiff implements JarDiffCodes } } - public String hasSameContent (JarFile2 file, JarEntry entry) throws IOException { + public String hasSameContent (JarFile2 file, ZipEntry entry) throws IOException { String thisName = null; - Long crcL = Long.valueOf(entry.getCrc()); + Long crcL = entry.getCrc(); // check if this jar contains files with the passed in entry's crc if (_crcToEntryMap.containsKey(crcL)) { // get the Linked List with files with the crc - LinkedList ll = _crcToEntryMap.get(crcL); + LinkedList ll = _crcToEntryMap.get(crcL); // go through the list and check for content match - ListIterator li = ll.listIterator(0); + ListIterator li = ll.listIterator(0); while (li.hasNext()) { - JarEntry thisEntry = li.next(); + ZipEntry thisEntry = li.next(); // check for content match try (InputStream oldIS = getJarFile().getInputStream(thisEntry); InputStream newIS = file.getJarFile().getInputStream(entry)) { @@ -404,7 +403,7 @@ public class JarDiff implements JarDiffCodes } private void index () throws IOException { - Enumeration entries = _jar.entries(); + Enumeration entries = _jar.entries(); _nameToEntryMap = new HashMap<>(); _crcToEntryMap = new HashMap<>(); @@ -414,9 +413,9 @@ public class JarDiff implements JarDiffCodes } if (entries != null) { while (entries.hasMoreElements()) { - JarEntry entry = entries.nextElement(); + ZipEntry entry = entries.nextElement(); long crc = entry.getCrc(); - Long crcL = Long.valueOf(crc); + Long crcL = crc; if (_debug) { System.out.println("\t" + entry.getName() + " CRC " + crc); } @@ -427,13 +426,13 @@ public class JarDiff implements JarDiffCodes // generate the CRC to entries map if (_crcToEntryMap.containsKey(crcL)) { // key exist, add the entry to the correcponding linked list - LinkedList ll = _crcToEntryMap.get(crcL); + LinkedList ll = _crcToEntryMap.get(crcL); ll.add(entry); _crcToEntryMap.put(crcL, ll); } else { // create a new entry in the hashmap for the new key - LinkedList ll = new LinkedList(); + LinkedList ll = new LinkedList(); ll.add(entry); _crcToEntryMap.put(crcL, ll); } diff --git a/core/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java b/core/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java index b5a0a17..af42173 100644 --- a/core/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java +++ b/core/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java @@ -11,22 +11,18 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; - import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; - -import java.util.jar.JarEntry; -import java.util.jar.JarFile; -import java.util.jar.JarOutputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipOutputStream; import com.threerings.getdown.util.ProgressObserver; - import static java.nio.charset.StandardCharsets.UTF_8; /** @@ -45,25 +41,23 @@ public class JarDiffPatcher implements JarDiffCodes * * @throws IOException if any problem occurs during patching. */ - public void patchJar (String jarPath, String diffPath, File target, ProgressObserver observer) + public static void patchJar(String jarPath, String diffPath, File target, ProgressObserver observer) throws IOException { File oldFile = new File(jarPath), diffFile = new File(diffPath); - - try (JarFile oldJar = new JarFile(oldFile); - JarFile jarDiff = new JarFile(diffFile); - JarOutputStream jos = new JarOutputStream(new FileOutputStream(target))) { - - Set ignoreSet = new HashSet<>(); - Map renameMap = new HashMap<>(); + try (ZipFile oldJar = new ZipFile(oldFile); + ZipFile jarDiff = new ZipFile(diffFile); + ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(target))) { + final Set ignoreSet = new HashSet<>(); + final Map renameMap = new HashMap<>(); determineNameMapping(jarDiff, ignoreSet, renameMap); // get all keys in renameMap String[] keys = renameMap.keySet().toArray(new String[renameMap.size()]); // Files to implicit move - Set oldjarNames = new HashSet<>(); - Enumeration oldEntries = oldJar.entries(); + final Set oldjarNames = new HashSet<>(); + Enumeration oldEntries = oldJar.entries(); if (oldEntries != null) { while (oldEntries.hasMoreElements()) { oldjarNames.add(oldEntries.nextElement().getName()); @@ -83,10 +77,10 @@ public class JarDiffPatcher implements JarDiffCodes size -= ignoreSet.size(); // Add content from JARDiff - Enumeration entries = jarDiff.entries(); + Enumeration entries = jarDiff.entries(); if (entries != null) { while (entries.hasMoreElements()) { - JarEntry entry = entries.nextElement(); + ZipEntry entry = entries.nextElement(); if (!INDEX_NAME.equals(entry.getName())) { updateObserver(observer, currentEntry, size); currentEntry++; @@ -114,15 +108,14 @@ public class JarDiffPatcher implements JarDiffCodes // Apply move command String oldName = renameMap.get(newName); - // Get source JarEntry - JarEntry oldEntry = oldJar.getJarEntry(oldName); + // Get source ZipEntry + ZipEntry oldEntry = oldJar.getEntry(oldName); if (oldEntry == null) { - String moveCmd = MOVE_COMMAND + oldName + " " + newName; - throw new IOException("error.badmove: " + moveCmd); + throw new IOException("error.badmove: " + MOVE_COMMAND + oldName + " " + newName); } - // Create dest JarEntry - JarEntry newEntry = new JarEntry(newName); + // Create dest ZipEntry + ZipEntry newEntry = new ZipEntry(newName); newEntry.setTime(oldEntry.getTime()); newEntry.setSize(oldEntry.getSize()); newEntry.setCompressedSize(oldEntry.getCompressedSize()); @@ -149,33 +142,29 @@ public class JarDiffPatcher implements JarDiffCodes } // implicit move - Iterator iEntries = oldjarNames.iterator(); - if (iEntries != null) { - while (iEntries.hasNext()) { - String name = iEntries.next(); - JarEntry entry = oldJar.getJarEntry(name); - if (entry == null) { - // names originally retrieved from the JAR, so this should never happen - throw new AssertionError("JAR entry not found: " + name); - } - updateObserver(observer, currentEntry, size); - currentEntry++; - writeEntry(jos, entry, oldJar); + for (String name : oldjarNames) { + ZipEntry entry = oldJar.getEntry(name); + if (entry == null) { + // names originally retrieved from the JAR, so this should never happen + throw new AssertionError("JAR entry not found: " + name); } + updateObserver(observer, currentEntry, size); + currentEntry++; + writeEntry(jos, entry, oldJar); } updateObserver(observer, currentEntry, size); } } - protected void updateObserver (ProgressObserver observer, double currentSize, double size) + protected static void updateObserver(ProgressObserver observer, double currentSize, double size) { if (observer != null) { observer.progress((int)(100*currentSize/size)); } } - protected void determineNameMapping ( - JarFile jarDiff, Set ignoreSet, Map renameMap) + protected static void determineNameMapping( + ZipFile jarDiff, Set ignoreSet, Map renameMap) throws IOException { InputStream is = jarDiff.getInputStream(jarDiff.getEntry(INDEX_NAME)); @@ -183,8 +172,7 @@ public class JarDiffPatcher implements JarDiffCodes throw new IOException("error.noindex"); } - LineNumberReader indexReader = - new LineNumberReader(new InputStreamReader(is, UTF_8)); + LineNumberReader indexReader = new LineNumberReader(new InputStreamReader(is, UTF_8)); String line = indexReader.readLine(); if (line == null || !line.equals(VERSION_HEADER)) { throw new IOException("jardiff.error.badheader: " + line); @@ -219,12 +207,11 @@ public class JarDiffPatcher implements JarDiffCodes } } - protected List getSubpaths (String path) + protected static List getSubpaths(String path) { int index = 0; int length = path.length(); - ArrayList sub = new ArrayList<>(); - + final List sub = new ArrayList<>(); while (index < length) { while (index < length && Character.isWhitespace (path.charAt(index))) { @@ -264,7 +251,7 @@ public class JarDiffPatcher implements JarDiffCodes return sub; } - protected void writeEntry (JarOutputStream jos, JarEntry entry, JarFile file) + protected static void writeEntry(ZipOutputStream jos, ZipEntry entry, ZipFile file) throws IOException { try (InputStream data = file.getInputStream(entry)) { @@ -272,10 +259,10 @@ public class JarDiffPatcher implements JarDiffCodes } } - protected void writeEntry (JarOutputStream jos, JarEntry entry, InputStream data) + protected static void writeEntry(ZipOutputStream jos, ZipEntry entry, InputStream data) throws IOException { - jos.putNextEntry(new JarEntry(entry.getName())); + jos.putNextEntry(new ZipEntry(entry.getName())); // Read the entry int size = data.read(newBytes); diff --git a/core/src/main/java/com/threerings/getdown/tools/Patcher.java b/core/src/main/java/com/threerings/getdown/tools/Patcher.java index 4ead59b..922ebb0 100644 --- a/core/src/main/java/com/threerings/getdown/tools/Patcher.java +++ b/core/src/main/java/com/threerings/getdown/tools/Patcher.java @@ -9,16 +9,13 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; - import java.util.Enumeration; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import com.threerings.getdown.util.FileUtil; import com.threerings.getdown.util.ProgressObserver; import com.threerings.getdown.util.StreamUtil; - import static com.threerings.getdown.Log.log; /** @@ -55,10 +52,10 @@ public class Patcher _obs = obs; _plength = patch.length(); - try (JarFile file = new JarFile(patch)) { - Enumeration entries = file.entries(); // old skool! + try (ZipFile file = new ZipFile(patch)) { + Enumeration entries = file.entries(); while (entries.hasMoreElements()) { - JarEntry entry = entries.nextElement(); + ZipEntry entry = entries.nextElement(); String path = entry.getName(); long elength = entry.getCompressedSize(); @@ -96,7 +93,7 @@ public class Patcher return path.substring(0, path.length() - suffix.length()); } - protected void createFile (JarFile file, ZipEntry entry, File target) + protected void createFile (ZipFile file, ZipEntry entry, File target) { // create our copy buffer if necessary if (_buffer == null) { @@ -124,8 +121,7 @@ public class Patcher } } - protected void patchFile (JarFile file, ZipEntry entry, - File appdir, String path) + protected void patchFile (ZipFile file, ZipEntry entry, File appdir, String path) { File target = new File(appdir, path); File patch = new File(appdir, entry.getName()); @@ -158,7 +154,7 @@ public class Patcher // now apply the patch to create the new target file patcher = new JarDiffPatcher(); - patcher.patchJar(otarget.getPath(), patch.getPath(), target, obs); + JarDiffPatcher.patchJar(otarget.getPath(), patch.getPath(), target, obs); } catch (IOException ioe) { if (patcher == null) { diff --git a/core/src/main/java/com/threerings/getdown/util/FileUtil.java b/core/src/main/java/com/threerings/getdown/util/FileUtil.java index ec9d887..08f4ac4 100644 --- a/core/src/main/java/com/threerings/getdown/util/FileUtil.java +++ b/core/src/main/java/com/threerings/getdown/util/FileUtil.java @@ -5,10 +5,26 @@ package com.threerings.getdown.util; -import java.io.*; -import java.util.*; -import java.util.jar.*; +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Deque; +import java.util.Enumeration; +import java.util.List; +import java.util.jar.JarFile; +import java.util.jar.JarOutputStream; +import java.util.jar.Pack200; import java.util.zip.GZIPInputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import com.threerings.getdown.Log; import static com.threerings.getdown.Log.log; @@ -16,7 +32,7 @@ import static com.threerings.getdown.Log.log; /** * File related utilities. */ -public class FileUtil +public final class FileUtil { /** * Gets the specified source file to the specified destination file by hook or crook. Windows @@ -104,7 +120,9 @@ public class FileUtil { List lines = new ArrayList<>(); try (BufferedReader bin = new BufferedReader(in)) { - for (String line = null; (line = bin.readLine()) != null; lines.add(line)) {} + for (String line; (line = bin.readLine()) != null;) { + lines.add(line); + } } return lines; } @@ -114,28 +132,28 @@ public class FileUtil * @param cleanExistingDirs if true, all files in all directories contained in {@code jar} will * be deleted prior to unpacking the jar. */ - public static void unpackJar (JarFile jar, File target, boolean cleanExistingDirs) - throws IOException + public static void unpackJar (ZipFile jar, File target, boolean cleanExistingDirs) throws IOException { if (cleanExistingDirs) { - Enumeration entries = jar.entries(); + Enumeration entries = jar.entries(); while (entries.hasMoreElements()) { - JarEntry entry = (JarEntry)entries.nextElement(); + ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) { File efile = new File(target, entry.getName()); if (efile.exists()) { for (File f : efile.listFiles()) { - if (!f.isDirectory()) - f.delete(); + if (!f.isDirectory()) { + f.delete(); + } } } } } } - Enumeration entries = jar.entries(); + Enumeration entries = jar.entries(); while (entries.hasMoreElements()) { - JarEntry entry = (JarEntry)entries.nextElement(); + ZipEntry entry = entries.nextElement(); File efile = new File(target, entry.getName()); // if we're unpacking a normal jar file, it will have special path @@ -169,10 +187,10 @@ public class FileUtil * Unpacks a pack200 packed jar file from {@code packedJar} into {@code target}. If {@code * packedJar} has a {@code .gz} extension, it will be gunzipped first. */ - public static void unpackPacked200Jar (File packedJar, File target) throws IOException + public static JarFile unpackPacked200Jar (File packedJar, File target) throws IOException { try (InputStream packJarIn = new FileInputStream(packedJar); - JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(target))) { + JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(target))) { boolean gz = (packedJar.getName().endsWith(".gz") || packedJar.getName().endsWith(".gz_new")); try (InputStream packJarIn2 = (gz ? new GZIPInputStream(packJarIn) : packJarIn)) { @@ -180,6 +198,7 @@ public class FileUtil unpacker.unpack(packJarIn2, jarOut); } } + return new JarFile(target); } /**