test/jdk/jdk/nio/zipfs/UpdateEntryTest.java
author lancea
Tue, 29 Oct 2019 14:22:18 -0400
changeset 58845 e492513d3630
parent 57842 abf6ee4c477c
permissions -rw-r--r--
8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file Reviewed-by: clanger, bpb, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57842
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
     1
/*
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
     4
 *
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
     7
 * published by the Free Software Foundation.
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
     8
 *
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    13
 * accompanied this code).
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    14
 *
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    18
 *
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    21
 * questions.
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    22
 *
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    23
 */
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    24
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    25
import org.testng.annotations.Test;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    26
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    27
import java.io.IOException;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    28
import java.io.InputStream;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    29
import java.io.OutputStream;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    30
import java.nio.charset.StandardCharsets;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    31
import java.nio.file.FileSystem;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    32
import java.nio.file.FileSystems;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    33
import java.nio.file.Files;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    34
import java.nio.file.Path;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    35
import java.util.Arrays;
58845
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 57842
diff changeset
    36
import java.util.Map;
57842
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    37
import java.util.spi.ToolProvider;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    38
import java.util.zip.CRC32;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    39
import java.util.zip.ZipEntry;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    40
import java.util.zip.ZipFile;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    41
import java.util.zip.ZipOutputStream;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    42
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    43
import static org.testng.Assert.*;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    44
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    45
/**
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    46
 * @test
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    47
 * @bug 8229887
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    48
 * @summary Validate ZIP FileSystem can replace existing STORED and DEFLATED entries
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    49
 * @modules jdk.zipfs
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    50
 * @run testng UpdateEntryTest
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    51
 */
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    52
@Test
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    53
public class UpdateEntryTest {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    54
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    55
    private static final Path HERE = Path.of(".");
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    56
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    57
    // Use the ToolProvider interface for accessing the jar tool
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    58
    private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    59
            .orElseThrow(() -> new RuntimeException("jar tool not found")
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    60
            );
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    61
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    62
    /**
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    63
     * Represents an entry in a ZIP file. An entry encapsulates a name, a
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    64
     * compression method, and its contents/data.
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    65
     */
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    66
    static class Entry {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    67
        private final String name;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    68
        private final int method;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    69
        private final byte[] bytes;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    70
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    71
        Entry(String name, int method, String contents) {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    72
            this.name = name;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    73
            this.method = method;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    74
            this.bytes = contents.getBytes(StandardCharsets.UTF_8);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    75
        }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    76
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    77
        static Entry of(String name, int method, String contents) {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    78
            return new Entry(name, method, contents);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    79
        }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    80
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    81
        /**
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    82
         * Returns a new Entry with the same name and compression method as this
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    83
         * Entry but with the given content.
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    84
         */
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    85
        Entry content(String contents) {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    86
            return new Entry(name, method, contents);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    87
        }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    88
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    89
        /**
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    90
         * Writes this entry to the given ZIP output stream.
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    91
         */
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    92
        ZipEntry put(ZipOutputStream zos) throws IOException {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    93
            ZipEntry e = new ZipEntry(name);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    94
            e.setMethod(method);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    95
            e.setTime(System.currentTimeMillis());
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    96
            if (method == ZipEntry.STORED) {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    97
                var crc = new CRC32();
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    98
                crc.update(bytes);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
    99
                e.setCrc(crc.getValue());
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   100
                e.setSize(bytes.length);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   101
            }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   102
            zos.putNextEntry(e);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   103
            zos.write(bytes);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   104
            return e;
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   105
        }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   106
    }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   107
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   108
    /**
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   109
     * Validate that you can replace an existing entry in a JAR file that
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   110
     * was added with the STORED(no-compression) option
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   111
     */
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   112
    public void testReplaceStoredEntry() throws IOException {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   113
        String jarFileName = "updateStoredEntry.jar";
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   114
        String storedFileName = "storedFile.txt";
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   115
        String replacedValue = "bar";
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   116
        Path zipFile = Path.of(jarFileName);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   117
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   118
        // Create JAR file with a STORED(non-compressed) entry
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   119
        Files.writeString(Path.of(storedFileName), "foobar");
58845
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 57842
diff changeset
   120
        JAR_TOOL.run(System.out, System.err,
57842
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   121
                "cM0vf", jarFileName, storedFileName);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   122
58845
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 57842
diff changeset
   123
        // Replace the STORED entry using the default(DEFLATED) compression
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 57842
diff changeset
   124
        // method.
57842
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   125
        try (FileSystem fs = FileSystems.newFileSystem(zipFile)) {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   126
            Files.writeString(fs.getPath(storedFileName), replacedValue);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   127
        }
58845
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 57842
diff changeset
   128
        Entry e1 = Entry.of(storedFileName, ZipEntry.DEFLATED, replacedValue);
57842
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   129
        verify(zipFile, e1);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   130
    }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   131
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   132
    /**
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   133
     * Test updating an entry that is STORED (not compressed)
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   134
     */
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   135
    public void test1() throws IOException {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   136
        Entry e1 = Entry.of("foo", ZipEntry.STORED, "hello");
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   137
        Entry e2 = Entry.of("bar", ZipEntry.STORED, "world");
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   138
        test(e1, e2);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   139
    }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   140
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   141
    /**
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   142
     * Test updating an entry that is DEFLATED (compressed)
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   143
     */
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   144
    public void test2() throws IOException {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   145
        Entry e1 = Entry.of("foo", ZipEntry.DEFLATED, "hello");
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   146
        Entry e2 = Entry.of("bar", ZipEntry.STORED, "world");
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   147
        test(e1, e2);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   148
    }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   149
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   150
    private void test(Entry e1, Entry e2) throws IOException {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   151
        Path zipfile = Files.createTempFile(HERE, "test", "zip");
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   152
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   153
        // create zip file
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   154
        try (OutputStream out = Files.newOutputStream(zipfile);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   155
             ZipOutputStream zos = new ZipOutputStream(out)) {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   156
            e1.put(zos);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   157
            e2.put(zos);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   158
        }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   159
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   160
        verify(zipfile, e1, e2);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   161
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   162
        String newContents = "hi";
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   163
58845
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 57842
diff changeset
   164
        // Set the required compression method
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 57842
diff changeset
   165
        Map<String, Boolean> map = Map.of("noCompression",
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 57842
diff changeset
   166
                e1.method != ZipEntry.DEFLATED);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 57842
diff changeset
   167
57842
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   168
        // replace contents of e1
58845
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 57842
diff changeset
   169
        try (FileSystem fs = FileSystems.newFileSystem(zipfile, map)) {
57842
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   170
            Path foo = fs.getPath(e1.name);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   171
            Files.writeString(foo, newContents);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   172
        }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   173
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   174
        verify(zipfile, e1.content(newContents), e2);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   175
    }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   176
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   177
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   178
    /**
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   179
     * Verify that the given path is a zip files containing exactly the
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   180
     * given entries.
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   181
     */
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   182
    private static void verify(Path zipfile, Entry... entries) throws IOException {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   183
        // check entries with zip API
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   184
        try (ZipFile zf = new ZipFile(zipfile.toFile())) {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   185
            // check entry count
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   186
            assertTrue(zf.size() == entries.length);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   187
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   188
            // check compression method and content of each entry
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   189
            for (Entry e : entries) {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   190
                ZipEntry ze = zf.getEntry(e.name);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   191
                assertTrue(ze != null);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   192
                assertTrue(ze.getMethod() == e.method);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   193
                try (InputStream in = zf.getInputStream(ze)) {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   194
                    byte[] bytes = in.readAllBytes();
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   195
                    assertTrue(Arrays.equals(bytes, e.bytes));
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   196
                }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   197
            }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   198
        }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   199
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   200
        // check entries with FileSystem API
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   201
        try (FileSystem fs = FileSystems.newFileSystem(zipfile)) {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   202
            // check entry count
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   203
            Path top = fs.getPath("/");
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   204
            long count = Files.find(top, Integer.MAX_VALUE,
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   205
                    (path, attrs) -> attrs.isRegularFile()).count();
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   206
            assertTrue(count == entries.length);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   207
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   208
            // check content of each entry
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   209
            for (Entry e : entries) {
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   210
                Path file = fs.getPath(e.name);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   211
                byte[] bytes = Files.readAllBytes(file);
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   212
                assertTrue(Arrays.equals(bytes, e.bytes));
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   213
            }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   214
        }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   215
    }
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents:
diff changeset
   216
}