小编Raj*_*ena的帖子

使用Java 7编写包含非英语字符的文件名时,zip条目不正确

我正在尝试开发可以处理具有非英文字符(变音符号,阿拉伯语等)的压缩文件的代码,但压缩文件包含不正确的名称.我使用的是java版本1.7.0_45因此它不应该是由于这里提到的错误.我正在为ZipOutputStream构造函数将字符集设置为UTF-8,并且按照Javadocs它应该按照我的要求工作.

我确信zip文件正在正确写入,因为尝试从文件中读取条目会提供正确的文件名(如预期的那样).

但是,当我尝试使用Ubuntu默认的ArchiveManager/Unzip工具打开/解压缩时,文件名会混乱.

这是我的代码:

private void convertFilesToZip(List<File> files) {
    FileInputStream inputStream = null;
    try {
        byte[] buffer = new byte[1024];

        FileOutputStream fileOutputStream = new FileOutputStream("zipFile.zip");

        ZipOutputStream outputStream = new ZipOutputStream(fileOutputStream, Charset.forName("UTF-8"));

        for (File file : files) {
            inputStream = new FileInputStream(file);
            String filename = file.getName();
            System.out.println("Adding file : " + filename);
            outputStream.putNextEntry(new ZipEntry(filename));

            int length;

            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
            outputStream.closeEntry();
        }

        if(inputStream != null) inputStream.close();
        outputStream.close();
        System.out.println("Zip …
Run Code Online (Sandbox Code Playgroud)

java zip special-characters java-7

6
推荐指数
1
解决办法
459
查看次数

标签 统计

java ×1

java-7 ×1

special-characters ×1

zip ×1