test/jdk/jdk/nio/zipfs/CopyMoveTests.java
author lancea
Tue, 05 Nov 2019 14:54:44 -0500
changeset 58941 00878bee8f4b
parent 58845 e492513d3630
permissions -rw-r--r--
8233622: (zipfs) open/test/jdk/jdk/nio/zipfs/CopyMoveTests.java has incorrect bug label Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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:
diff changeset
     1
/*
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
     4
 *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
     8
 *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    13
 * accompanied this code).
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    14
 *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    18
 *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    21
 * questions.
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    22
 *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    23
 */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    24
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    25
import org.testng.annotations.DataProvider;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    26
import org.testng.annotations.Test;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    27
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    28
import java.io.IOException;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    29
import java.io.InputStream;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    30
import java.nio.charset.StandardCharsets;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    31
import java.nio.file.*;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    32
import java.security.SecureRandom;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    33
import java.util.Arrays;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    34
import java.util.Map;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    35
import java.util.zip.ZipEntry;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    36
import java.util.zip.ZipFile;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    37
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    38
import static java.lang.String.format;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    39
import static java.util.stream.Collectors.joining;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    40
import static org.testng.Assert.*;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    41
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    42
/**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    43
 * @test
58941
00878bee8f4b 8233622: (zipfs) open/test/jdk/jdk/nio/zipfs/CopyMoveTests.java has incorrect bug label
lancea
parents: 58845
diff changeset
    44
 * @bug 8231766
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:
diff changeset
    45
 * @summary Test Files::copy and Files::move with Zip FS
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    46
 * @modules jdk.zipfs
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    47
 * @run testng/othervm CopyMoveTests
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    48
 */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    49
public class CopyMoveTests {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    50
    // Enable debugging output
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    51
    private static final boolean DEBUG = false;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    52
    // Path to current directory
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    53
    private static final Path HERE = Path.of(".");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    54
    // Value to use when creating Zip Entries
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    55
    private static final String ZIP_FILE_VALUE = "US Open 2019";
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    56
    // Value used to create the OS file to be copied into/from a Zip File
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    57
    private static final String OS_FILE_VALUE = "Hello World!";
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    58
    private static final SecureRandom random = new SecureRandom();
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    59
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    60
    /*
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    61
     * DataProvider used to verify that a FileAlreadyExistsException is
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    62
     * thrown with copying a file without the REPLACE_EXISTING option
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    63
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    64
    @DataProvider(name = "zipfsMap")
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    65
    private Object[][] zipfsMap() {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    66
        return new Object[][]{
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    67
                {Map.of("create", "true"), 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:
diff changeset
    68
                {Map.of("create", "true", "noCompression", "true"),
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    69
                        ZipEntry.STORED},
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    70
                {Map.of("create", "true", "noCompression", "false"),
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    71
                        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:
diff changeset
    72
        };
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    73
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    74
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    75
    /*
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    76
     * DataProvider used to verify that an entry may be copied or moved within
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    77
     * a Zip file system with the correct 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:
diff changeset
    78
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    79
    @DataProvider(name = "copyMoveMap")
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    80
    private Object[][] copyMoveMap() {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    81
        return new Object[][]{
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    82
                {Map.of("create", "true"), ZipEntry.DEFLATED, ZipEntry.STORED},
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    83
                {Map.of("create", "true", "noCompression", "true"),
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    84
                        ZipEntry.STORED, 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:
diff changeset
    85
                {Map.of("create", "true", "noCompression", "false"),
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    86
                        ZipEntry.DEFLATED, ZipEntry.STORED}
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    87
        };
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    88
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    89
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    90
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    91
     * Validate that an entry that is copied within a Zip file is copied with
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    92
     * the correct 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:
diff changeset
    93
     *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    94
     * @param createMap           Zip FS properties to use when creating the Zip File
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    95
     * @param compression         The compression used when writing the initial entries
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    96
     * @param expectedCompression The compression to be used when copying the entry
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    97
     * @throws Exception If an error occurs
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    98
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
    99
    @Test(dataProvider = "copyMoveMap", enabled = true)
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   100
    public void copyTest(Map<String, String> createMap, int 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:
diff changeset
   101
                         int expectedCompression) throws Exception {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   102
        Entry e0 = Entry.of("Entry-0", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   103
        Entry e1 = Entry.of("Entry-1", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   104
        Entry e00 = Entry.of("Entry-00", expectedCompression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   105
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   106
        Path zipFile = generatePath(HERE, "test", ".zip");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   107
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   108
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   109
        // Create the Zip File with the initial entries
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   110
        createZipFile(zipFile, createMap, e0, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   111
        try (FileSystem zipfs = FileSystems.newFileSystem(zipFile,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   112
                Map.of("noCompression", expectedCompression == ZipEntry.STORED))) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   113
            Files.copy(zipfs.getPath(e0.name), zipfs.getPath(e00.name));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   114
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   115
        // Verify entries e0, e1 and e00 exist
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   116
        verify(zipFile, e0, e1, e00);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   117
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   118
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   119
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   120
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   121
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   122
     * Validate that an entry that is copied from one Zip file to another,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   123
     * is copied with the correct 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:
diff changeset
   124
     *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   125
     * @param createMap           Zip FS properties to use when creating the Zip File
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   126
     * @param compression         The compression used when writing the initial entries
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   127
     * @param expectedCompression The compression to be used when copying the entry
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   128
     * @throws Exception If an error occurs
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   129
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   130
    @Test(dataProvider = "copyMoveMap", enabled = true)
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   131
    public void copyZipToZipTest(Map<String, String> createMap, int 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:
diff changeset
   132
                            int expectedCompression) throws Exception {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   133
        Entry e0 = Entry.of("Entry-0", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   134
        Entry e1 = Entry.of("Entry-1", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   135
        Entry e00 = Entry.of("Entry-00", expectedCompression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   136
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   137
        Path zipFile = generatePath(HERE, "test", ".zip");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   138
        Path zipFile2 = generatePath(HERE, "test", ".zip");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   139
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   140
        Files.deleteIfExists(zipFile2);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   141
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   142
        createZipFile(zipFile, createMap, e0, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   143
        try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, createMap);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   144
             FileSystem zipfsTarget = FileSystems.newFileSystem(zipFile2,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   145
                     Map.of("create", "true", "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:
diff changeset
   146
                             expectedCompression == ZipEntry.STORED))) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   147
            Files.copy(zipfs.getPath(e0.name), zipfsTarget.getPath(e00.name));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   148
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   149
        // Only 1 entry copied to the secondary Zip file
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   150
        verify(zipFile2, e00);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   151
        // Verify  entries e0 and e1 remain in the original Zip file
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   152
        verify(zipFile, e0, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   153
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   154
        Files.deleteIfExists(zipFile2);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   155
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   156
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   157
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   158
     * Validate that an external file copied to a Zip file is copied with
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   159
     * the correct 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:
diff changeset
   160
     *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   161
     * @param createMap           Zip FS properties to use when creating the Zip File
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   162
     * @param compression         The compression used when writing the initial entries
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   163
     * @param expectedCompression The compression to be used when copying the entry
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   164
     * @throws Exception If an error occurs
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   165
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   166
    @Test(dataProvider = "copyMoveMap", enabled = true)
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   167
    public void copyFromOsTest(Map<String, String> createMap, int 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:
diff changeset
   168
                           int expectedCompression) throws Exception {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   169
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   170
        Path osFile = generatePath(HERE, "test", ".txt");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   171
        Files.deleteIfExists(osFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   172
        Files.writeString(osFile, OS_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   173
        Entry e0 = Entry.of("Entry-0", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   174
        Entry e1 = Entry.of("Entry-1", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   175
        Entry e00 = Entry.of("Entry-00", expectedCompression, OS_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   176
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   177
        Path zipFile = generatePath(HERE, "test", ".zip");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   178
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   179
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   180
        createZipFile(zipFile, createMap, e0, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   181
        try (FileSystem zipfs = FileSystems.newFileSystem(zipFile,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   182
                Map.of("noCompression", expectedCompression == ZipEntry.STORED))) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   183
            Files.copy(osFile, zipfs.getPath(e00.name));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   184
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   185
        verify(zipFile, e0, e1, e00);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   186
        Files.deleteIfExists(osFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   187
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   188
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   189
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   190
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   191
     * Validate that an entry that is copied from a Zip file to an OS file contains
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   192
     * the correct bytes and the file remains in the Zip file
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   193
     *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   194
     * @param createMap           Zip FS properties to use when creating the Zip File
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   195
     * @param compression         The compression used when writing the initial entries
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   196
     * @param expectedCompression The compression to be used when moving the entry
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   197
     * @throws Exception If an error occurs
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   198
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   199
    @Test(dataProvider = "copyMoveMap", enabled = true)
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   200
    public void CopyFromZipTest(Map<String, String> createMap, int 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:
diff changeset
   201
                                int expectedCompression) throws Exception {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   202
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   203
        Entry e0 = Entry.of("Entry-0", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   204
        Entry e1 = Entry.of("Entry-1", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   205
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   206
        Path zipFile = generatePath(HERE, "test", ".zip");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   207
        Path osFile = generatePath(HERE, "test", ".txt");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   208
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   209
        Files.deleteIfExists(osFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   210
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   211
        createZipFile(zipFile, createMap, e0, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   212
        try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, Map.of())) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   213
            Files.copy(zipfs.getPath(e0.name), osFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   214
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   215
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   216
        // Entries e0 and e1 should exist
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   217
        verify(zipFile, e0, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   218
        // Check to see if the file exists and the bytes match
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   219
        assertTrue(Files.isRegularFile(osFile));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   220
        assertEquals(Files.readAllBytes(osFile), e0.bytes);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   221
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   222
        Files.deleteIfExists(osFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   223
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   224
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   225
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   226
     * Validate that an entry that is moved within a Zip file is moved with
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   227
     * the correct 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:
diff changeset
   228
     *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   229
     * @param createMap           Zip FS properties to use when creating the Zip File
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   230
     * @param compression         The compression used when writing the initial entries
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   231
     * @param expectedCompression The compression to be used when moving the entry
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   232
     * @throws Exception If an error occurs
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   233
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   234
    @Test(dataProvider = "copyMoveMap", enabled = true)
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   235
    public void moveTest(Map<String, String> createMap, int 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:
diff changeset
   236
                         int expectedCompression) throws Exception {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   237
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   238
        Entry e0 = Entry.of("Entry-0", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   239
        Entry e1 = Entry.of("Entry-1", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   240
        Entry e00 = Entry.of("Entry-00", expectedCompression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   241
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   242
        Path zipFile = generatePath(HERE, "test", ".zip");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   243
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   244
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   245
        createZipFile(zipFile, createMap, e0, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   246
        try (FileSystem zipfs = FileSystems.newFileSystem(zipFile,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   247
                Map.of("noCompression", expectedCompression == ZipEntry.STORED))) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   248
            Files.move(zipfs.getPath(e0.name), zipfs.getPath(e00.name));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   249
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   250
        // Entry e0 should not exist but Entry e00 should
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   251
        verify(zipFile, e1, e00);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   252
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   253
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   254
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   255
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   256
     * Validate that an entry that is moved one Zip file to another is moved with
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   257
     * the correct 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:
diff changeset
   258
     *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   259
     * @param createMap           Zip FS properties to use when creating the Zip File
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   260
     * @param compression         The compression used when writing the initial entries
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   261
     * @param expectedCompression The compression to be used when moving the entry
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   262
     * @throws Exception If an error occurs
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   263
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   264
    @Test(dataProvider = "copyMoveMap", enabled = true)
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   265
    public void moveZipToZipTest(Map<String, String> createMap, int 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:
diff changeset
   266
                            int expectedCompression) throws Exception {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   267
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   268
        Entry e0 = Entry.of("Entry-0", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   269
        Entry e1 = Entry.of("Entry-1", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   270
        Entry e00 = Entry.of("Entry-00", expectedCompression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   271
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   272
        Path zipFile = generatePath(HERE, "test", ".zip");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   273
        Path zipFile2 = generatePath(HERE, "test", ".zip");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   274
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   275
        Files.deleteIfExists(zipFile2);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   276
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   277
        createZipFile(zipFile, createMap, e0, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   278
        try (FileSystem zipfs = FileSystems.newFileSystem(zipFile,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   279
                Map.of("noCompression", expectedCompression == ZipEntry.STORED));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   280
             FileSystem zipfsTarget = FileSystems.newFileSystem(zipFile2,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   281
                     Map.of("create", "true", "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:
diff changeset
   282
                             expectedCompression == ZipEntry.STORED))) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   283
            Files.move(zipfs.getPath(e0.name), zipfsTarget.getPath(e00.name));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   284
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   285
        // Only Entry e00 should exist
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   286
        verify(zipFile2, e00);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   287
        // Only Entry e1 should exist
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   288
        verify(zipFile, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   289
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   290
        Files.deleteIfExists(zipFile2);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   291
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   292
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   293
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   294
     * Validate that an entry that is moved from a Zip file to an OS file contains
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   295
     * the correct bytes and is removed from the Zip file
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   296
     *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   297
     * @param createMap           Zip FS properties to use when creating the Zip File
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   298
     * @param compression         The compression used when writing the initial entries
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   299
     * @param expectedCompression The compression to be used when moving the entry
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   300
     * @throws Exception If an error occurs
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   301
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   302
    @Test(dataProvider = "copyMoveMap", enabled = true)
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   303
    public void moveFromZipTest(Map<String, String> createMap, int 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:
diff changeset
   304
                            int expectedCompression) throws Exception {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   305
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   306
        Entry e0 = Entry.of("Entry-0", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   307
        Entry e1 = Entry.of("Entry-1", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   308
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   309
        Path zipFile = generatePath(HERE, "test", ".zip");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   310
        Path osFile = generatePath(HERE, "test", ".txt");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   311
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   312
        Files.deleteIfExists(osFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   313
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   314
        createZipFile(zipFile, createMap, e0, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   315
        try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, Map.of())) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   316
            Files.move(zipfs.getPath(e0.name), osFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   317
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   318
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   319
        // Only Entry e1 should exist
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   320
        verify(zipFile, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   321
        // Check to see if the file exists and the bytes match
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   322
        assertTrue(Files.isRegularFile(osFile));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   323
        assertEquals(Files.readAllBytes(osFile), e0.bytes);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   324
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   325
        Files.deleteIfExists(osFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   326
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   327
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   328
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   329
     * Validate that a FileAlreadyExistsException is thrown when copying a
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   330
     * file and not specifying the REPLACE_EXISTING option.
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   331
     *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   332
     * @param createMap Properties used for creating the ZIP Filesystem
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   333
     * @throws Exception if an error occurs
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   334
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   335
    @Test(dataProvider = "zipfsMap", enabled = true)
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   336
    public void testFAEWithCopy(Map<String, String> createMap,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   337
                                int compression) throws Exception {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   338
        if (DEBUG) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   339
            System.out.printf("ZIP FS Map = %s%n ", formatMap(createMap));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   340
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   341
        Entry e0 = Entry.of("Entry-0", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   342
        Entry e1 = Entry.of("Entry-1", compression, ZIP_FILE_VALUE);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   343
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   344
        Path zipFile = generatePath(HERE, "test", ".zip");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   345
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   346
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   347
        createZipFile(zipFile, createMap, e0, e1);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   348
        try (FileSystem zipfs =
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   349
                     FileSystems.newFileSystem(zipFile, createMap)) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   350
            assertThrows(FileAlreadyExistsException.class, () ->
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   351
                    Files.copy(zipfs.getPath(e0.name),
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   352
                            zipfs.getPath(e1.name)));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   353
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   354
        Files.deleteIfExists(zipFile);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   355
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   356
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   357
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   358
     * Generate a temporary file Path
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   359
     *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   360
     * @param dir    Directory used to create the path
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   361
     * @param prefix The prefix string used to create the path
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   362
     * @param suffix The suffix string used to create the path
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   363
     * @return Path that was generated
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   364
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   365
    private static Path generatePath(Path dir, String prefix, String suffix) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   366
        long n = random.nextLong();
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   367
        String s = prefix + Long.toUnsignedString(n) + suffix;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   368
        Path name = dir.getFileSystem().getPath(s);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   369
        // the generated name should be a simple file name
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   370
        if (name.getParent() != null)
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   371
            throw new IllegalArgumentException("Invalid prefix or suffix");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   372
        return dir.resolve(name);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   373
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   374
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   375
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   376
     * Utility method to return a formatted String of the key:value entries for
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   377
     * a Map
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   378
     *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   379
     * @param env Map to format
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   380
     * @return Formatted string of the Map entries
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   381
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   382
    private static String formatMap(Map<String, String> env) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   383
        return env.entrySet().stream()
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   384
                .map(e -> format("(%s:%s)", e.getKey(), e.getValue()))
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   385
                .collect(joining(", "));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   386
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   387
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   388
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   389
     * Create a Zip file using the Zip File System with the specified
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   390
     * Zip File System properties
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   391
     *
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   392
     * @param zipFile Path to the Zip File to create
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   393
     * @param env     Properties used for creating the Zip Filesystem
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   394
     * @param entries The entries to add to the Zip File
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   395
     * @throws IOException If an error occurs while creating the Zip file
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   396
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   397
    private void createZipFile(Path zipFile, Map<String, String> env,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   398
                               Entry... entries) throws IOException {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   399
        if (DEBUG) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   400
            System.out.printf("Creating Zip file: %s with the Properties: %s%n",
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   401
                    zipFile, formatMap(env));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   402
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   403
        try (FileSystem zipfs =
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   404
                     FileSystems.newFileSystem(zipFile, env)) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   405
            for (Entry e : entries) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   406
                Files.writeString(zipfs.getPath(e.name), new String(e.bytes));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   407
            }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   408
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   409
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   410
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   411
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   412
     * Represents an entry in a Zip file. An entry encapsulates a name, a
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   413
     * compression method, and its contents/data.
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   414
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   415
    static class Entry {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   416
        private final String name;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   417
        private final int 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:
diff changeset
   418
        private final byte[] bytes;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   419
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   420
        Entry(String name, int method, String contents) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   421
            this.name = name;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   422
            this.method = 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:
diff changeset
   423
            this.bytes = contents.getBytes(StandardCharsets.UTF_8);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   424
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   425
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   426
        static Entry of(String name, int method, String contents) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   427
            return new Entry(name, method, contents);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   428
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   429
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   430
        /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   431
         * Returns a new Entry with the same name and compression method as this
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   432
         * Entry but with the given content.
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   433
         */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   434
        Entry content(String contents) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   435
            return new Entry(name, method, contents);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   436
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   437
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   438
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   439
    /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   440
     * Verify that the given path is a Zip file containing exactly the
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   441
     * given entries.
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   442
     */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   443
    private static void verify(Path zipfile, Entry... entries) throws IOException {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   444
        // check entries with zip API
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   445
        try (ZipFile zf = new ZipFile(zipfile.toFile())) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   446
            // check entry count
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   447
            assertEquals(entries.length, zf.size());
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   448
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   449
            // check compression method and content of each entry
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   450
            for (Entry e : entries) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   451
                ZipEntry ze = zf.getEntry(e.name);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   452
                //System.out.printf("Name: %s, method: %s, Expected Method: %s%n", e.name, ze.getMethod(), e.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:
diff changeset
   453
                assertNotNull(ze);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   454
                assertEquals(e.method, ze.getMethod());
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   455
                try (InputStream in = zf.getInputStream(ze)) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   456
                    byte[] bytes = in.readAllBytes();
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   457
                    assertTrue(Arrays.equals(bytes, e.bytes));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   458
                }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   459
            }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   460
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   461
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   462
        // check entries with FileSystem API
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   463
        try (FileSystem fs = FileSystems.newFileSystem(zipfile)) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   464
            // check entry count
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   465
            Path top = fs.getPath("/");
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   466
            long count = Files.find(top, Integer.MAX_VALUE,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   467
                    (path, attrs) -> attrs.isRegularFile()).count();
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   468
            assertEquals(entries.length, count);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   469
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   470
            // check content of each entry
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   471
            for (Entry e : entries) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   472
                Path file = fs.getPath(e.name);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   473
                byte[] bytes = Files.readAllBytes(file);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   474
                assertTrue(Arrays.equals(bytes, e.bytes));
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   475
            }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   476
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   477
    }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents:
diff changeset
   478
}