protected void copyEntry()

in src/java/io/bazel/rulesscala/jar/JarHelper.java [192:237]


  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();
      }
    }
  }