-
Bug
-
Resolution: Won't Fix
-
P3
-
None
-
7
-
generic
-
generic
JDK version: 7-pro-b123
The following code shows Zip File System Provider 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 Zip FS - utf8
out.println("Read back with Zip FS - utf8");
Map<String, Object> env = new HashMap<String, Object>();
Path file = Paths.get("test.zip");
ZipFileSystem fs = (ZipFileSystem) FileSystems.newFileSystem(file, env,
null);
System.out.println(fs.getPath(name).exists());
fs.close();
// Read back with Zip FS - gb2312
out.println("Read back with Zip FS - gb2312");
env = new HashMap<String, Object>();
env.put("encoding", "gb2312");
file = Paths.get("test.zip");
fs = (ZipFileSystem) FileSystems.newFileSystem(file, env,
null);
System.out.println(fs.getPath(name).exists());
fs.close();
}
}
=====End of Code=====
=====Begin of Output=====
Read back with Zip FS - utf8
true
Read back with Zip FS - gb2312
false
=====End of Output=====
The source code of ZipPath shows the root cause:
@Override
public boolean exists() {
if (path.length == 1 && path[0] == '/')
return true;
try {
return zfs.exists(getResolvedPath());
} catch (IOException x) {}
return false;
}
// resolved path for locating zip entry inside the zip file,
// the result path does not contain ./ and .. components
private volatile byte[] resolved = null;
byte[] getResolvedPath() {
byte[] r = resolved;
if (r == null) {
if (isAbsolute())
r = getResolved();
else
r = toAbsolutePath().getResolvedPath();
if (r[0] == '/')
r = Arrays.copyOfRange(r, 1, r.length);
resolved = r;
}
return resolved;
}
==========================
From the above code, we can not see "language encoding flag" check. We can not even see a Coder or something alike.
And I think the issue may not only exist in method exists(). It should exist in other methods.
The following code shows Zip File System Provider 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 Zip FS - utf8
out.println("Read back with Zip FS - utf8");
Map<String, Object> env = new HashMap<String, Object>();
Path file = Paths.get("test.zip");
ZipFileSystem fs = (ZipFileSystem) FileSystems.newFileSystem(file, env,
null);
System.out.println(fs.getPath(name).exists());
fs.close();
// Read back with Zip FS - gb2312
out.println("Read back with Zip FS - gb2312");
env = new HashMap<String, Object>();
env.put("encoding", "gb2312");
file = Paths.get("test.zip");
fs = (ZipFileSystem) FileSystems.newFileSystem(file, env,
null);
System.out.println(fs.getPath(name).exists());
fs.close();
}
}
=====End of Code=====
=====Begin of Output=====
Read back with Zip FS - utf8
true
Read back with Zip FS - gb2312
false
=====End of Output=====
The source code of ZipPath shows the root cause:
@Override
public boolean exists() {
if (path.length == 1 && path[0] == '/')
return true;
try {
return zfs.exists(getResolvedPath());
} catch (IOException x) {}
return false;
}
// resolved path for locating zip entry inside the zip file,
// the result path does not contain ./ and .. components
private volatile byte[] resolved = null;
byte[] getResolvedPath() {
byte[] r = resolved;
if (r == null) {
if (isAbsolute())
r = getResolved();
else
r = toAbsolutePath().getResolvedPath();
if (r[0] == '/')
r = Arrays.copyOfRange(r, 1, r.length);
resolved = r;
}
return resolved;
}
==========================
From the above code, we can not see "language encoding flag" check. We can not even see a Coder or something alike.
And I think the issue may not only exist in method exists(). It should exist in other methods.