Exemple #1
0
  /**
   * Output the entries to the specified output folder on the file system.
   *
   * @param outputFolder The target output folder.
   * @throws java.io.IOException Write failure.
   */
  public void toFileSystem(File outputFolder) throws IOException {
    AssertArgument.isNotNull(outputFolder, "outputFolder");

    if (outputFolder.isFile()) {
      throw new IOException(
          "Cannot write Archive entries to '"
              + outputFolder.getAbsolutePath()
              + "'.  This is a normal file i.e. not a directory.");
    }
    if (!outputFolder.exists()) {
      outputFolder.mkdirs();
    }

    Set<Map.Entry<String, File>> entrySet = entries.entrySet();
    for (Map.Entry<String, File> entry : entrySet) {
      File archEntryFile = entry.getValue();
      byte[] fileBytes;
      File entryFile = new File(outputFolder, entry.getKey());

      if (archEntryFile != null) {
        fileBytes = FileUtils.readFile(archEntryFile);
        entryFile.getParentFile().mkdirs();
        FileUtils.writeFile(fileBytes, entryFile);
      } else {
        entryFile.mkdirs();
      }
    }
  }
Exemple #2
0
  /**
   * Get the value of the entry at the specified index in the archive.
   *
   * @param index The index.
   * @return The entry value at that index.
   */
  public byte[] getEntryValue(int index) {
    Set<Map.Entry<String, File>> entrySet = entries.entrySet();
    int i = 0;

    for (Map.Entry<String, File> entry : entrySet) {
      if (i == index) {
        File entryFile = entry.getValue();

        if (entryFile != null) {
          try {
            return FileUtils.readFile(entryFile);
          } catch (IOException e) {
            throw new IllegalStateException(
                "Unexpected error reading Archive file '" + entryFile.getAbsolutePath() + "'.", e);
          }
        } else {
          return null;
        }
      }

      i++;
    }

    throw new ArrayIndexOutOfBoundsException(index);
  }
Exemple #3
0
  private void writeEntriesToArchive(ZipOutputStream archiveStream) throws IOException {
    File manifestFile = entries.get(JarFile.MANIFEST_NAME);

    // Always write the jar manifest as the first entry, if it exists...
    if (manifestFile != null) {
      byte[] manifest = FileUtils.readFile(manifestFile);
      writeEntry(JarFile.MANIFEST_NAME, manifest, archiveStream);
    }

    Set<Map.Entry<String, File>> entrySet = entries.entrySet();
    for (Map.Entry<String, File> entry : entrySet) {
      if (!entry.getKey().equals(JarFile.MANIFEST_NAME)) {
        File file = entry.getValue();

        if (file != null && !file.isDirectory()) {
          writeEntry(entry.getKey(), FileUtils.readFile(file), archiveStream);
        } else {
          writeEntry(entry.getKey(), null, archiveStream);
        }
      }
    }
  }
Exemple #4
0
  /**
   * Get an Archive entries bytes.
   *
   * @param resName Entry resource name.
   * @return The bytes, or null if the entry is not in the Archive.
   */
  public byte[] getEntryBytes(String resName) {
    File entryFile = getEntry(resName);

    if (entryFile != null) {
      try {
        return FileUtils.readFile(entryFile);
      } catch (IOException e) {
        throw new IllegalStateException(
            "Unexpected error reading Archive file '" + entryFile.getAbsolutePath() + "'.", e);
      }
    } else {
      return null;
    }
  }
Exemple #5
0
  public Archive merge(Archive archive) {
    Map<String, File> entriesToMerge = archive.getEntries();
    for (Entry<String, File> entry : entriesToMerge.entrySet()) {
      File file = entry.getValue();

      if (file != null && !file.isDirectory()) {
        try {
          addEntry(entry.getKey(), FileUtils.readFile(file));
        } catch (IOException e) {
          throw new IllegalStateException(
              "Unexpected error reading Archive file '" + file.getAbsolutePath() + "'.", e);
        }
      } else {
        addEntry(entry.getKey(), (byte[]) null);
      }
    }
    return this;
  }