Java Code Examples for java.util.jar.JarEntry#STORED

The following examples show how to use java.util.jar.JarEntry#STORED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Jar.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
private static JarEntry newJarEntry(Entry entry) {
    final JarEntry result = new JarEntry(entry.getName());
    if (result.getCreationTime() != null) {
        result.setCreationTime(entry.getCreationTime());
    }
    if (result.getLastModifiedTime() != null) {
        result.setLastModifiedTime(entry.getLastModifiedTime());
    }
    if (entry.getExtra() != null) {
        result.setExtra(entry.getExtra());
    }
    if (result.getComment() != null) {
        result.setComment(entry.getComment());
    }
    if (!entry.isDirectory()) {
        final int method = entry.getMethod();
        if (method == JarEntry.STORED || method == ZipEntry.DEFLATED) {
            result.setMethod(method);
        }
    }
    return result;
}
 
Example 2
Source File: JarHelper.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Writes an entry with specific contents to the jar. Directory entries must include the trailing
 * '/'.
 */
protected void writeEntry(JarOutputStream out, String name, byte[] content) throws IOException {
  if (names.add(name)) {
    // Create a new entry
    JarEntry entry = new JarEntry(name);
    entry.setTime(newEntryTimeMillis(name));
    int size = content.length;
    entry.setSize(size);
    if (size == 0) {
      entry.setMethod(JarEntry.STORED);
      entry.setCrc(0);
      out.putNextEntry(entry);
    } else {
      entry.setMethod(storageMethod);
      if (storageMethod == JarEntry.STORED) {
        CRC32 crc = new CRC32();
        crc.update(content);
        entry.setCrc(crc.getValue());
      }
      out.putNextEntry(entry);
      out.write(content);
    }
    out.closeEntry();
  }
}
 
Example 3
Source File: JarHelper.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a standard Java manifest entry into the JarOutputStream. This includes the directory
 * entry for the "META-INF" directory
 *
 * @param content the Manifest content to write to the manifest entry.
 * @throws IOException
 */
protected void writeManifestEntry(JarOutputStream out, byte[] content) throws IOException {
  int oldStorageMethod = storageMethod;
  // Do not compress small manifest files, the compressed one is frequently
  // larger than the original. The threshold of 256 bytes is somewhat arbitrary.
  if (content.length < 256) {
    storageMethod = JarEntry.STORED;
  }
  try {
    writeEntry(out, MANIFEST_DIR, new byte[] {});
    writeEntry(out, MANIFEST_NAME, content);
  } finally {
    storageMethod = oldStorageMethod;
  }
}
 
Example 4
Source File: JarHelper.java    From bazel with Apache License 2.0 4 votes vote down vote up
/**
 * Copies file or directory entries from the file system into the jar. Directory entries will be
 * detected and their names automatically '/' suffixed.
 */
protected void copyEntry(JarOutputStream out, String name, Path path) throws IOException {
  if (!names.contains(name)) {
    if (!Files.exists(path)) {
      throw new FileNotFoundException(path.toAbsolutePath() + " (No such file or directory)");
    }
    boolean isDirectory = Files.isDirectory(path);
    if (isDirectory && !name.endsWith("/")) {
      name = name + '/'; // always normalize directory names before checking set
    }
    if (names.add(name)) {
      if (verbose) {
        System.err.println("adding " + path);
      }
      // Create a new entry
      long size = isDirectory ? 0 : Files.size(path);
      JarEntry outEntry = new JarEntry(name);
      long newtime =
          normalize ? normalizedTimestamp(name) : Files.getLastModifiedTime(path).toMillis();
      outEntry.setTime(newtime);
      outEntry.setSize(size);
      if (size == 0L) {
        outEntry.setMethod(JarEntry.STORED);
        outEntry.setCrc(0);
        out.putNextEntry(outEntry);
      } else {
        outEntry.setMethod(storageMethod);
        if (storageMethod == JarEntry.STORED) {
          // ZipFile requires us to calculate the CRC-32 for any STORED entry.
          // It would be nicer to do this via DigestInputStream, but
          // the architecture of ZipOutputStream requires us to know the CRC-32
          // before we write the data to the stream.
          byte[] bytes = Files.readAllBytes(path);
          CRC32 crc = new CRC32();
          crc.update(bytes);
          outEntry.setCrc(crc.getValue());
          out.putNextEntry(outEntry);
          out.write(bytes);
        } else {
          out.putNextEntry(outEntry);
          Files.copy(path, out);
        }
      }
      out.closeEntry();
    }
  }
}
 
Example 5
Source File: JarHelper.java    From bazel with Apache License 2.0 2 votes vote down vote up
/**
 * Enables or disables compression for the Jar file entries.
 *
 * @param compression if true enables compressions for the Jar file entries.
 */
public void setCompression(boolean compression) {
  storageMethod = compression ? JarEntry.DEFLATED : JarEntry.STORED;
}