Пример #1
0
  /** Updates an existing jar file. */
  boolean update(InputStream in, OutputStream out, InputStream newManifest, JarIndex jarIndex)
      throws IOException {
    ZipInputStream zis = new ZipInputStream(in);
    ZipOutputStream zos = new JarOutputStream(out);
    ZipEntry e = null;
    boolean foundManifest = false;
    boolean updateOk = true;

    if (jarIndex != null) {
      addIndex(jarIndex, zos);
    }

    // put the old entries first, replace if necessary
    while ((e = zis.getNextEntry()) != null) {
      String name = e.getName();

      boolean isManifestEntry = equalsIgnoreCase(name, MANIFEST_NAME);

      if ((jarIndex != null && equalsIgnoreCase(name, INDEX_NAME)) || (Mflag && isManifestEntry)) {
        continue;
      } else if (isManifestEntry && ((newManifest != null) || (ename != null) || (pname != null))) {
        foundManifest = true;
        if (newManifest != null) {
          // Don't read from the newManifest InputStream, as we
          // might need it below, and we can't re-read the same data
          // twice.
          FileInputStream fis = new FileInputStream(mname);
          boolean ambiguous = isAmbiguousMainClass(new Manifest(fis));
          fis.close();
          if (ambiguous) {
            return false;
          }
        }

        // Update the manifest.
        Manifest old = new Manifest(zis);
        if (newManifest != null) {
          old.read(newManifest);
        }
        if (!updateManifest(old, zos)) {
          return false;
        }
      } else {
        if (!entryMap.containsKey(name)) { // copy the old stuff
          // do our own compression
          ZipEntry e2 = new ZipEntry(name);
          e2.setMethod(e.getMethod());
          e2.setTime(e.getTime());
          e2.setComment(e.getComment());
          e2.setExtra(e.getExtra());
          if (e.getMethod() == ZipEntry.STORED) {
            e2.setSize(e.getSize());
            e2.setCrc(e.getCrc());
          }
          zos.putNextEntry(e2);
          copy(zis, zos);
        } else { // replace with the new files
          File f = entryMap.get(name);
          addFile(zos, f);
          entryMap.remove(name);
          entries.remove(f);
        }
      }
    }

    // add the remaining new files
    for (File f : entries) {
      addFile(zos, f);
    }
    if (!foundManifest) {
      if (newManifest != null) {
        Manifest m = new Manifest(newManifest);
        updateOk = !isAmbiguousMainClass(m);
        if (updateOk) {
          if (!updateManifest(m, zos)) {
            updateOk = false;
          }
        }
      } else if (ename != null || pname != null) {
        if (!updateManifest(new Manifest(), zos)) {
          updateOk = false;
        }
      }
    }
    zis.close();
    zos.close();
    return updateOk;
  }
Пример #2
0
 /**
  * 取得压缩文件对象的注释
  *
  * @param entry 压缩文件对象
  * @return 压缩文件对象的注释
  * @throws UnsupportedEncodingException
  */
 public static String getEntryComment(ZipEntry entry) throws UnsupportedEncodingException {
   return new String(entry.getComment().getBytes("GB2312"), "8859_1");
 }
  protected void processJarFile(final File file) throws Exception {

    if (verbose) {
      log("processing " + file.toURI());
    }

    final File tempFile =
        File.createTempFile(file.getName(), null, new File(file.getAbsoluteFile().getParent()));
    try {

      final ZipInputStream zip = new ZipInputStream(new FileInputStream(file));
      try {
        final FileOutputStream fout = new FileOutputStream(tempFile);
        try {
          final ZipOutputStream out = new ZipOutputStream(fout);

          ZipEntry entry;
          while ((entry = zip.getNextEntry()) != null) {

            byte bytes[] = getBytes(zip);

            if (!entry.isDirectory()) {

              final DataInputStream din = new DataInputStream(new ByteArrayInputStream(bytes));

              if (din.readInt() == CLASS_MAGIC) {

                bytes = process(bytes);

              } else {
                if (verbose) {
                  log("ignoring " + entry.toString());
                }
              }
            }

            final ZipEntry outEntry = new ZipEntry(entry.getName());
            outEntry.setMethod(entry.getMethod());
            outEntry.setComment(entry.getComment());
            outEntry.setSize(bytes.length);

            if (outEntry.getMethod() == ZipEntry.STORED) {
              final CRC32 crc = new CRC32();
              crc.update(bytes);
              outEntry.setCrc(crc.getValue());
              outEntry.setCompressedSize(bytes.length);
            }
            out.putNextEntry(outEntry);
            out.write(bytes);
            out.closeEntry();
            zip.closeEntry();
          }
          out.close();
        } finally {
          fout.close();
        }
      } finally {
        zip.close();
      }

      if (file.delete()) {

        final File newFile = new File(tempFile.getAbsolutePath());

        if (!newFile.renameTo(file)) {
          throw new IOException("can not rename " + tempFile + " to " + file);
        }

      } else {
        throw new IOException("can not delete " + file);
      }

    } finally {

      tempFile.delete();
    }
  }