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