Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8354342

ZipInputStream doesn't fill in crc32 when reading the central directory for uncompressed jars

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • None
    • core-libs
    • None

      ZipInputStream doesn't populate the crc in the ZipEntry for uncompressed entries before reading the data. It does populate the crc for compressed entries.

      The relevant logic is here: https://github.com/openjdk/jdk/blob/4890b74c048a1472b87687294c316ecfb324e4ba/src/java.base/share/classes/java/util/zip/ZipInputStream.java#L541-L551

      When iterating over the entries with ZipFile, ZipFile#getCrc returns crcs for entries regardless of compression.



      ```
      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Path;
      import java.util.zip.ZipEntry;
      import java.util.zip.ZipInputStream;

      public class Z {
        public static void main(String[] args) throws IOException {
          try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(Path.of(args[0])))) {
            while (true) {
              ZipEntry ze = zis.getNextEntry();
              if (ze == null) {
                break;
              }
              System.err.printf("before read %s %s\n", ze.getName(), ze.getCrc());
              var unused = zis.readAllBytes();
              System.err.printf("after read %s %s\n", ze.getName(), ze.getCrc());
            }
          }
        }
      }
      ```

      $ echo 'hello' > a.txt
      $ jar cf j.jar a.txt
      $ java Z.java j.jar
      ...
      before read a.txt -1
      after read a.txt 909783072

      $ jar -0cf j.jar a.txt
      $ java Z.java j.jar
      ...
      before read a.txt 909783072
      after read a.txt 909783072

            lancea Lance Andersen
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: