0N/A/*
2362N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4244499 4532049 4700978 4820807 4980042
0N/A * @summary Test ZipInputStream, ZipOutputStream and ZipFile with non-UTF8 encoding
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.charset.*;
0N/Aimport java.util.*;
0N/Aimport java.util.zip.*;
0N/A
0N/Apublic class ZipCoding {
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A test("MS932",
0N/A "\u4e00\u4e01", "\uff67\uff68\uff69\uff6a\uff6b\uff6c");
0N/A
0N/A test("ibm437",
0N/A "\u00e4\u00fc", "German Umlaut \u00fc in comment");
0N/A
0N/A test("utf-8",
0N/A "\u4e00\u4e01", "\uff67\uff68\uff69\uff6a\uff6b\uff6c");
0N/A
0N/A test("utf-8",
0N/A "\u00e4\u00fc", "German Umlaut \u00fc in comment");
0N/A
0N/A test("utf-8",
0N/A "Surrogate\ud801\udc01", "Surrogates \ud800\udc00 in comment");
0N/A
0N/A }
0N/A
0N/A static void testZipInputStream(InputStream is, Charset cs,
0N/A String name, String comment, byte[] bb)
0N/A throws Exception
0N/A {
0N/A try (ZipInputStream zis = new ZipInputStream(is, cs)) {
0N/A ZipEntry e = zis.getNextEntry();
0N/A if (e == null || ! name.equals(e.getName()))
0N/A throw new RuntimeException("ZipIS name doesn't match!");
0N/A byte[] bBuf = new byte[bb.length << 1];
0N/A int n = zis.read(bBuf, 0, bBuf.length);
0N/A if (n != bb.length ||
0N/A !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) {
0N/A throw new RuntimeException("ZipIS content doesn't match!");
0N/A }
0N/A }
0N/A }
0N/A
0N/A static void testZipFile(File f, Charset cs,
0N/A String name, String comment, byte[] bb)
0N/A throws Exception
0N/A {
0N/A try (ZipFile zf = new ZipFile(f, cs)) {
0N/A Enumeration<? extends ZipEntry> zes = zf.entries();
0N/A ZipEntry e = (ZipEntry)zes.nextElement();
0N/A if (! name.equals(e.getName()) ||
0N/A ! comment.equals(e.getComment()))
0N/A throw new RuntimeException("ZipFile: name/comment doesn't match!");
0N/A InputStream is = zf.getInputStream(e);
0N/A if (is == null)
0N/A throw new RuntimeException("ZipFile: getIS failed!");
0N/A byte[] bBuf = new byte[bb.length << 1];
0N/A int n = 0;
0N/A int nn =0;
0N/A while ((nn = is.read(bBuf, n, bBuf.length-n)) != -1) {
0N/A n += nn;
0N/A }
0N/A if (n != bb.length ||
0N/A !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) {
0N/A throw new RuntimeException("ZipFile content doesn't match!");
0N/A }
0N/A }
0N/A }
0N/A
0N/A static void test(String csn, String name, String comment)
0N/A throws Exception
0N/A {
0N/A byte[] bb = "This is the content of the zipfile".getBytes("ISO-8859-1");
0N/A Charset cs = Charset.forName(csn);
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A try (ZipOutputStream zos = new ZipOutputStream(baos, cs)) {
0N/A ZipEntry e = new ZipEntry(name);
0N/A e.setComment(comment);
0N/A zos.putNextEntry(e);
0N/A zos.write(bb, 0, bb.length);
0N/A zos.closeEntry();
0N/A }
0N/A ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
testZipInputStream(bis, cs, name, comment, bb);
if ("utf-8".equals(csn)) {
// EFS should be set
bis.reset();
testZipInputStream(bis, Charset.forName("MS932"), name, comment, bb);
}
File f = new File(new File(System.getProperty("test.dir", ".")),
"zfcoding.zip");
try (FileOutputStream fos = new FileOutputStream(f)) {
baos.writeTo(fos);
}
testZipFile(f, cs, name, comment, bb);
if ("utf-8".equals(csn)) {
testZipFile(f, Charset.forName("MS932"), name, comment, bb);
}
f.delete();
}
}