8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
authorsherman
Tue, 18 Sep 2018 19:44:27 -0700
changeset 51795 feb4c9e03aed
parent 51794 4129f43607cb
child 51796 9d3a00c8c047
8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field Reviewed-by: bpb
src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java
test/jdk/jdk/nio/zipfs/ZipFSTester.java
--- a/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java	Tue Sep 18 22:46:35 2018 +0200
+++ b/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java	Tue Sep 18 19:44:27 2018 -0700
@@ -612,6 +612,10 @@
         }
     }
 
+    private int getCompressMethod(FileAttribute<?>... attrs) {
+         return defaultMethod;
+    }
+
     // Returns a Writable/ReadByteChannel for now. Might consdier to use
     // newFileChannel() instead, which dump the entry data into a regular
     // file on the default file system and create a FileChannel on top of
@@ -653,7 +657,7 @@
                     throw new NoSuchFileException(getString(path));
                 checkParents(path);
                 return new EntryOutputChannel(
-                    new Entry(path, Entry.NEW, false, defaultMethod));
+                    new Entry(path, Entry.NEW, false, getCompressMethod(attrs)));
 
             } finally {
                 endRead();
@@ -721,7 +725,7 @@
             final Entry u = isFCH ? e : new Entry(path, tmpfile, Entry.FILECH);
             if (forWrite) {
                 u.flag = FLAG_DATADESCR;
-                u.method = METHOD_DEFLATED;
+                u.method = getCompressMethod(attrs);
             }
             // is there a better way to hook into the FileChannel's close method?
             return new FileChannel() {
@@ -1407,7 +1411,7 @@
                 return;
             isClosed = true;
             e.size = e.csize = written;
-            e.size = crc.getValue();
+            e.crc = crc.getValue();
         }
     }
 
--- a/test/jdk/jdk/nio/zipfs/ZipFSTester.java	Tue Sep 18 22:46:35 2018 +0200
+++ b/test/jdk/jdk/nio/zipfs/ZipFSTester.java	Tue Sep 18 19:44:27 2018 -0700
@@ -73,7 +73,7 @@
  * @test
  * @bug 6990846 7009092 7009085 7015391 7014948 7005986 7017840 7007596
  *      7157656 8002390 7012868 7012856 8015728 8038500 8040059 8069211
- *      8131067 8034802
+ *      8131067 8034802 8210899
  * @summary Test Zip filesystem provider
  * @modules jdk.zipfs
  * @run main ZipFSTester
@@ -95,7 +95,6 @@
             test1(fs);
             test2(fs);   // more tests
         }
-
         testStreamChannel();
         testTime(jarFile);
         test8069211();
@@ -434,6 +433,28 @@
 
     // check the content of read from zipfs is equal to the "bytes"
     private static void checkRead(Path path, byte[] expected) throws IOException {
+
+        // fileAttribute
+        CRC32 crc32 = new CRC32();
+        crc32.update(expected);
+
+        if (((Long)Files.getAttribute(path, "zip:crc")).intValue() !=
+            (int)crc32.getValue()) {
+            System.out.printf(" getAttribute.crc <%s> failed %x vs %x ...%n",
+                              path.toString(),
+                              ((Long)Files.getAttribute(path, "zip:crc")).intValue(),
+                              (int)crc32.getValue());
+            throw new RuntimeException("CHECK FAILED!");
+        }
+
+        if (((Long)Files.getAttribute(path, "zip:size")).intValue() != expected.length) {
+            System.out.printf(" getAttribute.size <%s> failed %x vs %x ...%n",
+                              path.toString(),
+                              ((Long)Files.getAttribute(path, "zip:size")).intValue(),
+                              expected.length);
+            throw new RuntimeException("CHECK FAILED!");
+        }
+
         //streams
         try (InputStream is = Files.newInputStream(path)) {
             if (!Arrays.equals(is.readAllBytes(), expected)) {