jdk/test/java/util/zip/ZipCoding.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8553 46c2babb1e44
child 35768 7066da300a08
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
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
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8553
diff changeset
     2
 * Copyright (c) 2009, 2011, 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
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    28
 */
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
import java.io.*;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    31
import java.nio.charset.*;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    32
import java.util.*;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    33
import java.util.zip.*;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    34
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    35
public class ZipCoding {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    36
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    37
    public static void main(String[] args) throws Exception {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    38
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    39
        test("MS932",
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    40
             "\u4e00\u4e01", "\uff67\uff68\uff69\uff6a\uff6b\uff6c");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    41
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    42
        test("ibm437",
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    43
             "\u00e4\u00fc", "German Umlaut \u00fc in comment");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    44
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    45
        test("utf-8",
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    46
             "\u4e00\u4e01", "\uff67\uff68\uff69\uff6a\uff6b\uff6c");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    47
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    48
        test("utf-8",
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    49
             "\u00e4\u00fc", "German Umlaut \u00fc in comment");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    50
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    51
        test("utf-8",
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    52
             "Surrogate\ud801\udc01", "Surrogates \ud800\udc00 in comment");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    53
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
    static void testZipInputStream(InputStream is, Charset cs,
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    57
                                   String name, String comment, byte[] bb)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    58
        throws Exception
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    59
    {
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    60
        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
    61
            ZipEntry e = zis.getNextEntry();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    62
            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
    63
                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
    64
            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
    65
            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
    66
            if (n != bb.length ||
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    67
                !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
    68
                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
    69
            }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    70
        }
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
    static void testZipFile(File f, Charset cs,
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    74
                            String name, String comment, byte[] bb)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    75
        throws Exception
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    76
    {
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    77
        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
    78
            Enumeration<? extends ZipEntry> zes = zf.entries();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    79
            ZipEntry e = (ZipEntry)zes.nextElement();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    80
            if (! name.equals(e.getName()) ||
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    81
                ! comment.equals(e.getComment()))
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    82
                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
    83
            InputStream is = zf.getInputStream(e);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    84
            if (is == null)
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    85
                throw new RuntimeException("ZipFile: getIS failed!");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    86
            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
    87
            int n = 0;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    88
            int nn =0;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    89
            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
    90
                n += nn;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    91
            }
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    92
            if (n != bb.length ||
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
    93
                !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
    94
                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
    95
            }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    96
        }
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
    static void test(String csn, String name, String comment)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   100
        throws Exception
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   101
    {
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   102
        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
   103
        Charset cs = Charset.forName(csn);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   104
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   105
        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
   106
            ZipEntry e = new ZipEntry(name);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   107
            e.setComment(comment);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   108
            zos.putNextEntry(e);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   109
            zos.write(bb, 0, bb.length);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   110
            zos.closeEntry();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   111
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   112
        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   113
        testZipInputStream(bis, cs, name, comment, bb);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   114
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   115
        if ("utf-8".equals(csn)) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   116
            // EFS should be set
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   117
            bis.reset();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   118
            testZipInputStream(bis, Charset.forName("MS932"), name, comment, bb);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   119
        }
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
        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
   122
                          "zfcoding.zip");
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   123
        try (FileOutputStream fos = new FileOutputStream(f)) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   124
            baos.writeTo(fos);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   125
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   126
        testZipFile(f, cs, name, comment, bb);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   127
        if ("utf-8".equals(csn)) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   128
            testZipFile(f, Charset.forName("MS932"), name, comment, bb);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   129
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   130
        f.delete();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   131
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   132
}