private void resolveLocalFileHeaderData(
     final Map<ZipEntry, NameAndComment> entriesWithoutUTF8Flag) throws IOException {
   for (final Entry ze : this.entries) {
     final OffsetEntry offsetEntry = ze.getOffsetEntry();
     final long offset = offsetEntry.headerOffset;
     this.archive.seek(offset + 26L);
     this.archive.readFully(this.SHORT_BUF);
     final int fileNameLen = ZipShort.getValue(this.SHORT_BUF);
     this.archive.readFully(this.SHORT_BUF);
     final int extraFieldLen = ZipShort.getValue(this.SHORT_BUF);
     int skipped;
     for (int lenToSkip = fileNameLen; lenToSkip > 0; lenToSkip -= skipped) {
       skipped = this.archive.skipBytes(lenToSkip);
       if (skipped <= 0) {
         throw new IOException("failed to skip file name in local file header");
       }
     }
     final byte[] localExtraData = new byte[extraFieldLen];
     this.archive.readFully(localExtraData);
     ze.setExtra(localExtraData);
     offsetEntry.dataOffset = offset + 26L + 2L + 2L + fileNameLen + extraFieldLen;
     if (entriesWithoutUTF8Flag.containsKey(ze)) {
       final NameAndComment nc = entriesWithoutUTF8Flag.get(ze);
       ZipUtil.setNameAndCommentFromExtraFields(ze, nc.name, nc.comment);
     }
     final String name = ze.getName();
     LinkedList<ZipEntry> entriesOfThatName = this.nameMap.get(name);
     if (entriesOfThatName == null) {
       entriesOfThatName = new LinkedList<ZipEntry>();
       this.nameMap.put(name, entriesOfThatName);
     }
     entriesOfThatName.addLast(ze);
   }
 }
Example #2
0
 private Map<String, Tag> findLastParent(final String[] parts) {
   Map<String, Tag> map = NBTStorage.this.root;
   for (int i = 0; i < parts.length - 1; ++i) {
     if (!map.containsKey(parts[i]) || !(map.get(parts[i]) instanceof CompoundTag)) {
       return null;
     }
     map = map.get(parts[i]).getValue();
   }
   return map;
 }
Example #3
0
 private void putTag(final String key, final Tag tag) {
   final String[] parts =
       (String[])
           Iterables.toArray(
               Splitter.on('.').split((CharSequence) this.createRelativeKey(key)),
               (Class) String.class);
   Map<String, Tag> parent = NBTStorage.this.root;
   for (int i = 0; i < parts.length - 1; ++i) {
     if (!parent.containsKey(parts[i]) || !(parent.get(parts[i]) instanceof CompoundTag)) {
       parent.put(parts[i], new CompoundTag(parts[i]));
     }
     parent = parent.get(parts[i]).getValue();
   }
   parent.put(tag.getName(), tag);
 }
Example #4
0
 private Tag findLastTag(final String key, final boolean relative) {
   final String[] parts =
       (String[])
           Iterables.toArray(
               Splitter.on('.')
                   .omitEmptyStrings()
                   .split((CharSequence) (relative ? this.createRelativeKey(key) : key)),
               (Class) String.class);
   if (parts.length == 0) {
     return new CompoundTag(NBTStorage.this.name, NBTStorage.this.root);
   }
   final Map<String, Tag> map = this.findLastParent(parts);
   if (!map.containsKey(parts[parts.length - 1])) {
     return null;
   }
   return map.get(parts[parts.length - 1]);
 }
 /**
  * Returns the instance associated to the given content encoding.
  *
  * @param contentEncoding content encoding (e.g. "gzip")
  * @return instance for content encoding
  */
 static CompressingStreamFactory getFactoryForContentEncoding(String contentEncoding) {
   assert FACTORY_MAP.containsKey(contentEncoding);
   return FACTORY_MAP.get(contentEncoding);
 }
 static boolean isSupportedRequestContentEncoding(String contentEncoding) {
   return NO_ENCODING.equals(contentEncoding) || FACTORY_MAP.containsKey(contentEncoding);
 }
Example #7
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;
  }