📄 jardiff.java
字号:
JarFile2 file) throws IOException { writeEntry(jos, entry, file.getJarFile().getInputStream(entry)); } private static void writeEntry(JarOutputStream jos, JarEntry entry, InputStream data) throws IOException { jos.putNextEntry(entry); try { // Read the entry int size = data.read(newBytes); while (size != -1) { jos.write(newBytes, 0, size); size = data.read(newBytes); } } catch(IOException ioE) { throw ioE; } finally { try { data.close(); } catch(IOException e){ //Ignore } } } /** * JarFile2 wraps a JarFile providing some convenience methods. */ private static class JarFile2 { private JarFile _jar; private List _entries; private HashMap _nameToEntryMap; private HashMap _crcToEntryMap; public JarFile2(String path) throws IOException { _jar = new JarFile(new File(path)); index(); } public JarFile getJarFile() { return _jar; } public Iterator getJarEntries() { return _entries.iterator(); } public JarEntry getEntryByName(String name) { return (JarEntry)_nameToEntryMap.get(name); } /** * Returns true if the two InputStreams differ. */ private static boolean differs(InputStream oldIS, InputStream newIS) throws IOException { int newSize = 0; int oldSize; int total = 0; boolean retVal = false; try{ while (newSize != -1) { newSize = newIS.read(newBytes); oldSize = oldIS.read(oldBytes); if (newSize != oldSize) { if (_debug) { System.out.println("\tread sizes differ: " + newSize + " " + oldSize + " total " + total); } retVal = true; break; } if (newSize > 0) { while (--newSize >= 0) { total++; if (newBytes[newSize] != oldBytes[newSize]) { if (_debug) { System.out.println("\tbytes differ at " + total); } retVal = true; break; } if ( retVal ) { //Jump out break; } newSize = 0; } } } } catch(IOException ioE){ throw ioE; } finally { try { oldIS.close(); } catch(IOException e){ //Ignore } try { newIS.close(); } catch(IOException e){ //Ignore } } return retVal; } public String getBestMatch(JarFile2 file, JarEntry entry) throws IOException { // check for same name and same content, return name if found if (contains(file, entry)) { return (entry.getName()); } // return name of same content file or null return (hasSameContent(file,entry)); } public boolean contains(JarFile2 f, JarEntry e) throws IOException { JarEntry thisEntry = getEntryByName(e.getName()); // Look up name in 'this' Jar2File - if not exist return false if (thisEntry == null) return false; // Check CRC - if no match - return false if (thisEntry.getCrc() != e.getCrc()) return false; // Check contents - if no match - return false InputStream oldIS = getJarFile().getInputStream(thisEntry); InputStream newIS = f.getJarFile().getInputStream(e); boolean retValue = differs(oldIS, newIS); return !retValue; } public String hasSameContent(JarFile2 file, JarEntry entry) throws IOException { String thisName = null; Long crcL = new Long(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 = (LinkedList)_crcToEntryMap.get(crcL); // go through the list and check for content match ListIterator li = ll.listIterator(0); if (li != null) { while (li.hasNext()) { JarEntry thisEntry = (JarEntry)li.next(); // check for content match InputStream oldIS = getJarFile().getInputStream(thisEntry); InputStream newIS = file.getJarFile().getInputStream(entry); if (!differs(oldIS, newIS)) { thisName = thisEntry.getName(); return thisName; } } } } return thisName; } private void index() throws IOException { Enumeration entries = _jar.entries(); _nameToEntryMap = new HashMap(); _crcToEntryMap = new HashMap(); _entries = new ArrayList(); if (_debug) { System.out.println("indexing: " + _jar.getName()); } if (entries != null) { while (entries.hasMoreElements()) { JarEntry entry = (JarEntry)entries.nextElement(); long crc = entry.getCrc(); Long crcL = new Long(crc); if (_debug) { System.out.println("\t" + entry.getName() + " CRC " + crc); } _nameToEntryMap.put(entry.getName(), entry); _entries.add(entry); // generate the CRC to entries map if (_crcToEntryMap.containsKey(crcL)) { // key exist, add the entry to the correcponding // linked list // get the linked list LinkedList ll = (LinkedList)_crcToEntryMap.get(crcL); // put in the new entry ll.add(entry); // put it back in the hash map _crcToEntryMap.put(crcL, ll); } else { // create a new entry in the hashmap for the new key // first create the linked list and put in the new // entry LinkedList ll = new LinkedList(); ll.add(entry); // create the new entry in the hashmap _crcToEntryMap.put(crcL, ll); } } } } } private static void showHelp() { System.out.println("JarDiff: [-nonminimal (for backward compatibility with 1.0.1/1.0] [-creatediff | -applydiff] [-output file] old.jar new.jar"); } // -creatediff -applydiff -debug -output file public static void main(String[] args) throws IOException { boolean diff = true; boolean minimal = true; String outputFile = "out.jardiff"; for (int counter = 0; counter < args.length; counter++) { // for backward compatibilty with 1.0.1/1.0 if (args[counter].equals("-nonminimal") || args[counter].equals("-n")) { minimal = false; } else if (args[counter].equals("-creatediff") || args[counter].equals("-c")) { diff = true; } else if (args[counter].equals("-applydiff") || args[counter].equals("-a")) { diff = false; } else if (args[counter].equals("-debug") || args[counter].equals("-d")) { _debug = true; } else if (args[counter].equals("-output") || args[counter].equals("-o")) { if (++counter < args.length) { outputFile = args[counter]; } } else if (args[counter].equals("-applydiff") || args[counter].equals("-a")) { diff = false; } else { if ((counter + 2) != args.length) { showHelp(); System.exit(0); } if (diff) { try { OutputStream os = new FileOutputStream(outputFile); JarDiff.createPatch(args[counter], args[counter + 1], os, minimal); os.close(); } catch (IOException ioe) { try { System.out.println(getResources().getString("jardiff.error.create") + " " + ioe); } catch (MissingResourceException mre) { } } } else { try { OutputStream os = new FileOutputStream(outputFile); new JarDiffPatcher().applyPatch( null, args[counter], args[counter + 1], os); os.close(); } catch (IOException ioe) { try { System.out.println(getResources().getString("jardiff.error.apply") + " " + ioe); } catch (MissingResourceException mre) { } } } System.exit(0); } } showHelp(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -