jdk/test/java/util/zip/ZipCoding.java
author shurailine
Mon, 08 Feb 2016 18:14:48 -0800
changeset 35768 7066da300a08
parent 9035 1255eb81cc2f
permissions -rw-r--r--
8149391: Fix module dependences in java/util tests Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     1
/*
35768
7066da300a08 8149391: Fix module dependences in java/util tests
shurailine
parents: 9035
diff changeset
     2
 * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     4
 *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     8
 *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    13
 * accompanied this code).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    14
 *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2592
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2592
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2592
diff changeset
    21
 * questions.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    22
 */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    23
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    24
/**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    25
 * @test
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    26
 * @bug 4244499 4532049 4700978 4820807 4980042
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    27
 * @summary Test ZipInputStream, ZipOutputStream and ZipFile with non-UTF8 encoding
35768
7066da300a08 8149391: Fix module dependences in java/util tests
shurailine
parents: 9035
diff changeset
    28
 * @modules jdk.charsets
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    29
 */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    30
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    31
import java.io.*;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    32
import java.nio.charset.*;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    33
import java.util.*;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    34
import java.util.zip.*;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    35
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    36
public class ZipCoding {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    37
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    38
    public static void main(String[] args) throws Exception {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    39
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    40
        test("MS932",
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    41
             "\u4e00\u4e01", "\uff67\uff68\uff69\uff6a\uff6b\uff6c");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    42
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    43
        test("ibm437",
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    44
             "\u00e4\u00fc", "German Umlaut \u00fc in comment");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    45
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    46
        test("utf-8",
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    47
             "\u4e00\u4e01", "\uff67\uff68\uff69\uff6a\uff6b\uff6c");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    48
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    49
        test("utf-8",
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    50
             "\u00e4\u00fc", "German Umlaut \u00fc in comment");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    51
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    52
        test("utf-8",
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    53
             "Surrogate\ud801\udc01", "Surrogates \ud800\udc00 in comment");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    54
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    55
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    56
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    57
    static void testZipInputStream(InputStream is, Charset cs,
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    58
                                   String name, String comment, byte[] bb)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    59
        throws Exception
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    60
    {
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    61
        try (ZipInputStream zis = new ZipInputStream(is, cs)) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    62
            ZipEntry e = zis.getNextEntry();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    63
            if (e == null || ! name.equals(e.getName()))
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    64
                throw new RuntimeException("ZipIS name doesn't match!");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    65
            byte[] bBuf = new byte[bb.length << 1];
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    66
            int n = zis.read(bBuf, 0, bBuf.length);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    67
            if (n != bb.length ||
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    68
                !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    69
                throw new RuntimeException("ZipIS content doesn't match!");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    70
            }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    71
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    72
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    73
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    74
    static void testZipFile(File f, Charset cs,
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    75
                            String name, String comment, byte[] bb)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    76
        throws Exception
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    77
    {
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    78
        try (ZipFile zf = new ZipFile(f, cs)) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    79
            Enumeration<? extends ZipEntry> zes = zf.entries();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    80
            ZipEntry e = (ZipEntry)zes.nextElement();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    81
            if (! name.equals(e.getName()) ||
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    82
                ! comment.equals(e.getComment()))
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    83
                throw new RuntimeException("ZipFile: name/comment doesn't match!");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    84
            InputStream is = zf.getInputStream(e);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    85
            if (is == null)
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    86
                throw new RuntimeException("ZipFile: getIS failed!");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    87
            byte[] bBuf = new byte[bb.length << 1];
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    88
            int n = 0;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    89
            int nn =0;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    90
            while ((nn = is.read(bBuf, n, bBuf.length-n)) != -1) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    91
                n += nn;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    92
            }
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    93
            if (n != bb.length ||
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    94
                !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    95
                throw new RuntimeException("ZipFile content doesn't match!");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    96
            }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    97
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    98
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    99
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   100
    static void test(String csn, String name, String comment)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   101
        throws Exception
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   102
    {
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   103
        byte[] bb = "This is the content of the zipfile".getBytes("ISO-8859-1");
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   104
        Charset cs = Charset.forName(csn);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   105
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   106
        try (ZipOutputStream zos = new ZipOutputStream(baos, cs)) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   107
            ZipEntry e = new ZipEntry(name);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   108
            e.setComment(comment);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   109
            zos.putNextEntry(e);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   110
            zos.write(bb, 0, bb.length);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   111
            zos.closeEntry();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   112
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   113
        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   114
        testZipInputStream(bis, cs, name, comment, bb);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   115
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   116
        if ("utf-8".equals(csn)) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   117
            // EFS should be set
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   118
            bis.reset();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   119
            testZipInputStream(bis, Charset.forName("MS932"), name, comment, bb);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   120
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   121
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   122
        File f = new File(new File(System.getProperty("test.dir", ".")),
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   123
                          "zfcoding.zip");
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   124
        try (FileOutputStream fos = new FileOutputStream(f)) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   125
            baos.writeTo(fos);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   126
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   127
        testZipFile(f, cs, name, comment, bb);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   128
        if ("utf-8".equals(csn)) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   129
            testZipFile(f, Charset.forName("MS932"), name, comment, bb);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   130
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   131
        f.delete();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   132
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   133
}