jdk/test/java/util/zip/ZipCoding.java
author michaelm
Fri, 02 Oct 2009 13:57:41 +0100
changeset 3952 dc329398de30
parent 2592 ef26f663a2ba
child 5506 202f599c92aa
permissions -rw-r--r--
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash Reviewed-by: chegar
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
/*
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    21
 * have any questions.
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
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    60
        ZipInputStream zis = new ZipInputStream(is, cs);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    61
        ZipEntry e = zis.getNextEntry();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    62
        if (e == null || ! name.equals(e.getName()))
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    63
            throw new RuntimeException("ZipIS name doesn't match!");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    64
        byte[] bBuf = new byte[bb.length << 1];
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    65
        int n = zis.read(bBuf, 0, bBuf.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    66
        if (n != bb.length ||
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    67
            !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    68
            throw new RuntimeException("ZipIS content doesn't match!");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    69
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    70
        zis.close();
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
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    77
        ZipFile zf = new ZipFile(f, cs);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    78
        Enumeration<? extends ZipEntry> zes = zf.entries();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    79
        ZipEntry e = (ZipEntry)zes.nextElement();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    80
        if (! name.equals(e.getName()) ||
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    81
            ! comment.equals(e.getComment()))
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    82
            throw new RuntimeException("ZipFile: name/comment doesn't match!");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    83
        InputStream is = zf.getInputStream(e);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    84
        if (is == null)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    85
            throw new RuntimeException("ZipFile: getIS failed!");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    86
        byte[] bBuf = new byte[bb.length << 1];
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    87
        int n = 0;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    88
        int nn =0;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    89
        while ((nn = is.read(bBuf, n, bBuf.length-n)) != -1) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    90
            n += nn;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    91
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    92
        if (n != bb.length ||
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    93
            !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    94
            throw new RuntimeException("ZipFile content doesn't match!");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    95
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    96
        zf.close();
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
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   102
        byte[] bb = "This is the conent of the zipfile".getBytes("ISO-8859-1");
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();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   105
        ZipOutputStream zos = new ZipOutputStream(baos, cs);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   106
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   107
        ZipEntry e = new ZipEntry(name);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   108
        e.setComment(comment);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   109
        zos.putNextEntry(e);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   110
        zos.write(bb, 0, bb.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   111
        zos.closeEntry();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   112
        zos.close();
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");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   124
        FileOutputStream fos = new FileOutputStream(f);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   125
        baos.writeTo(fos);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   126
        fos.close();
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
}