Java 类com.intellij.util.lang.JarMemoryLoader 实例源码

项目:tools-idea    文件:ReorderJarsTest.java   
@Test
public void testPluginXml() throws Exception {
  String path = getTestDataPath() + "/ide/plugins/reorderJars";

  ReorderJarsMain.main(new String[] { path + "/zkmOrder.txt", path, myTempDirectory.getPath() } );

  File[] files = myTempDirectory.listFiles();
  assertNotNull(files);
  File file = files[0];
  assertEquals("zkm.jar", file.getName());

  JBZipFile zipFile = new JBZipFile(file);
  try {
    List<JBZipEntry> entries = zipFile.getEntries();
    System.out.println(entries);
    assertEquals(JarMemoryLoader.SIZE_ENTRY, entries.get(0).getName());
    assertEquals("META-INF/plugin.xml", entries.get(1).getName());
  }
  finally {
    zipFile.close();
  }
}
项目:tools-idea    文件:ReorderJarsMain.java   
public static void main(String[] args) {
  try {
    final String orderTxtPath = args[0];
    final String jarsPath = args[1];
    final String destinationPath = args[2];
    final String libPath = args.length > 3 ? args[3] : null;

    final Map<String, List<String>> toReorder = getOrder(new File(orderTxtPath));
    final Set<String> ignoredJars = libPath == null ? Collections.<String>emptySet() : loadIgnoredJars(libPath);

    for (String jarUrl : toReorder.keySet()) {
      if (ignoredJars.contains(StringUtil.trimStart(jarUrl, "/lib/"))) continue;
      if (jarUrl.startsWith("/lib/ant")) continue;

      final File jarFile = new File(jarsPath, jarUrl);
      if (!jarFile.isFile()) continue;

      final JBZipFile zipFile = new JBZipFile(jarFile);
      final List<JBZipEntry> entries = zipFile.getEntries();
      final List<String> orderedEntries = toReorder.get(jarUrl);
      Collections.sort(entries, new Comparator<JBZipEntry>() {
        public int compare(JBZipEntry o1, JBZipEntry o2) {
          if ("META-INF/plugin.xml".equals(o2.getName())) return Integer.MAX_VALUE;
          if ("META-INF/plugin.xml".equals(o1.getName())) return -Integer.MAX_VALUE;
          if (orderedEntries.contains(o1.getName())) {
            return orderedEntries.contains(o2.getName()) ? orderedEntries.indexOf(o1.getName()) - orderedEntries.indexOf(o2.getName()) : -1;
          }
          else {
            return orderedEntries.contains(o2.getName()) ? 1 : 0;
          }
        }
      });

      final File tempJarFile = FileUtil.createTempFile("__reorder__", "__reorder__");
      final JBZipFile file = new JBZipFile(tempJarFile);

      final JBZipEntry sizeEntry = file.getOrCreateEntry(JarMemoryLoader.SIZE_ENTRY);
      sizeEntry.setData(ZipShort.getBytes(orderedEntries.size()));

      for (JBZipEntry entry : entries) {
        final JBZipEntry zipEntry = file.getOrCreateEntry(entry.getName());
        zipEntry.setData(entry.getData());
      }
      file.close();

      final File resultJarFile = new File(destinationPath, jarUrl);
      final File resultDir = resultJarFile.getParentFile();
      if (!resultDir.isDirectory() && !resultDir.mkdirs()) {
        throw new IOException("Cannot create: " + resultDir);
      }
      try {
        FileUtil.rename(tempJarFile, resultJarFile);
      }
      catch (Exception e) {
        FileUtil.delete(resultJarFile);
        throw e;
      }
      FileUtil.delete(tempJarFile);
    }
  }
  catch (Throwable t) {
    t.printStackTrace();
    System.exit(1);
  }
}
项目:consulo    文件:ReorderJarsMain.java   
public static void main(String[] args) {
  try {
    final String orderTxtPath = args[0];
    final String jarsPath = args[1];
    final String destinationPath = args[2];
    final String libPath = args.length > 3 ? args[3] : null;

    final Map<String, List<String>> toReorder = getOrder(new File(orderTxtPath));
    final Set<String> ignoredJars = libPath == null ? Collections.<String>emptySet() : loadIgnoredJars(libPath);

    for (String jarUrl : toReorder.keySet()) {
      if (ignoredJars.contains(StringUtil.trimStart(jarUrl, "/lib/"))) continue;
      if (jarUrl.startsWith("/lib/ant")) continue;

      final File jarFile = new File(jarsPath, jarUrl);
      if (!jarFile.isFile()) continue;

      final JBZipFile zipFile = new JBZipFile(jarFile);
      final List<JBZipEntry> entries = zipFile.getEntries();
      final List<String> orderedEntries = toReorder.get(jarUrl);
      Collections.sort(entries, new Comparator<JBZipEntry>() {
        public int compare(JBZipEntry o1, JBZipEntry o2) {
          if ("META-INF/plugin.xml".equals(o2.getName())) return Integer.MAX_VALUE;
          if ("META-INF/plugin.xml".equals(o1.getName())) return -Integer.MAX_VALUE;
          if (orderedEntries.contains(o1.getName())) {
            return orderedEntries.contains(o2.getName()) ? orderedEntries.indexOf(o1.getName()) - orderedEntries.indexOf(o2.getName()) : -1;
          }
          else {
            return orderedEntries.contains(o2.getName()) ? 1 : 0;
          }
        }
      });

      final File tempJarFile = FileUtil.createTempFile("__reorder__", "__reorder__");
      final JBZipFile file = new JBZipFile(tempJarFile);

      final JBZipEntry sizeEntry = file.getOrCreateEntry(JarMemoryLoader.SIZE_ENTRY);
      sizeEntry.setData(ZipShort.getBytes(orderedEntries.size()));

      for (JBZipEntry entry : entries) {
        final JBZipEntry zipEntry = file.getOrCreateEntry(entry.getName());
        zipEntry.setData(entry.getData());
      }
      file.close();

      final File resultJarFile = new File(destinationPath, jarUrl);
      final File resultDir = resultJarFile.getParentFile();
      if (!resultDir.isDirectory() && !resultDir.mkdirs()) {
        throw new IOException("Cannot create: " + resultDir);
      }
      try {
        FileUtil.rename(tempJarFile, resultJarFile);
      }
      catch (Exception e) {
        FileUtil.delete(resultJarFile);
        throw e;
      }
      FileUtil.delete(tempJarFile);
    }
  }
  catch (Throwable t) {
    t.printStackTrace();
    System.exit(1);
  }
}