Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7009069

ZipFile.getEntry(String name) returns null because it does NOT respect the "language encoding flag"

XMLWordPrintable

      JDK version: 7-pro-b123

      The following code shows ZipEntry.getEntry(String name) does NOT respect the "language encoding flag".

      =====Begin of Code=====
      import java.io.*;
      import java.nio.charset.Charset;
      import java.nio.file.FileSystems;
      import java.nio.file.Path;
      import java.nio.file.Paths;
      import java.util.HashMap;
      import java.util.Map;
      import java.util.zip.*;

      import com.sun.nio.zipfs.ZipFileAttributeView;
      import com.sun.nio.zipfs.ZipFileAttributes;
      import com.sun.nio.zipfs.ZipFileSystem;

      import static java.lang.System.out;;

      public class EFSZipFS {

      private static final String name = "zipEntryName\u7b80";
      private static final String comment = "zipEntryComment\u7b80";

      public static void main(String[] args) throws Exception {
      // TODO Auto-generated method stub
      byte[] bb = "This is the conent of the zipfile".getBytes("ISO-8859-1");
      Charset cs = Charset.forName("utf8");
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ZipOutputStream zos = new ZipOutputStream(baos, cs);

      ZipEntry e = new ZipEntry(name);
      e.setComment(comment);
      zos.putNextEntry(e);
      zos.write(bb, 0, bb.length);
      zos.closeEntry();
      zos.close();

      Paths.get("test.zip").deleteIfExists();
      File f = new File("test.zip");
      FileOutputStream fos = new FileOutputStream(f);
      baos.writeTo(fos);
      fos.close();

      // Read back with ZipFile-utf8
      out.println("Read back with ZipFile-utf8");
      ZipFile zf = new ZipFile("test.zip", Charset.forName("utf8"));
      System.out.println(zf.getEntry(name).getComment());
      zf.close();

      // Read back with ZipFile-gb2312
      out.println("Read back with ZipFile-gb2312");
      zf = new ZipFile("test.zip", Charset.forName("gb2312"));
      System.out.println(zf.getEntry(name).getComment());
      zf.close();
      }
      }
      =====End of Code=====

      =====Begin of Output=====
      Read back with ZipFile-utf8
      zipEntryComment¿
      Read back with ZipFile-gb2312
      Exception in thread "main" java.lang.NullPointerException
      at EFSZipFS.main(EFSZipFS.java:50)
      =====End of Output=====

      The source code of ZipEntry shows the root cause:
         public ZipEntry getEntry(String name) {
              if (name == null) {
                  throw new NullPointerException("name");
              }
              long jzentry = 0;
              synchronized (this) {
                  ensureOpen();
                  jzentry = getEntry(jzfile, zc.getBytes(name), true); // !!!Here we should also check "language encoding flag"!!!
                  if (jzentry != 0) {
                      ZipEntry ze = getZipEntry(name, jzentry);
                      freeEntry(jzfile, jzentry);
                      return ze;
                  }
              }
              return null;
          }

          private static native long getEntry(long jzfile, byte[] name,
                                              boolean addSlash);

            eirbjo Eirik Bjørsnøs
            stephenh Stephen Hu (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: