java.util.zip.* 不能支持中文文件名(300分)

  • 主题发起人 主题发起人 qdyoung
  • 开始时间 开始时间
Q

qdyoung

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是测试代码:
// These are the files to include in the ZIP file
String[] filenames = new String[]{"文件1.txt", "文件2.txt"};

// Create a buffer for reading the files
byte[] buf = new byte[1024];

try {
// Create the ZIP file
String outFilename = "outfile.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

// Compress the files
for (int i=0;
i<filenames.length;
i++) {
FileInputStream in = new FileInputStream(filenames);

// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filenames));

// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}

// Complete the entry
out.closeEntry();
in.close();
}

// Complete the ZIP file
out.close();
} catch (IOException e) {
}
输出的压缩文件用winzip查看中文文件名都是乱码
再用以下命令测试(应该也是用的java.util.zip.*)
jar cvf test.zip 文件1.txt
也是一样的,压缩提示都是中文的,输出的zip文件文件名乱码
JDK 是1.4.1 RC
 
http://developer.java.sun.com/developer/bugParade/bugs/4244499.html
是个 bug
 
没看明白该怎么解决,改jdk源程序不是办法
 
自己写一个ZipEntryEx类继承ZipEntry,然后覆盖后面说的方法
 
这个方法对中文也不行:
I have tried it bydo
ing this:
byte[] bytes = fileName.getBytes("Cp437");
String str = new String(bytes, 0);
System.err.println(str);
ZipEntry ze1 = new ZipEntry(str);
结果文件名:
C:/??1.txt
C:/??2.txt
测试代码:
import java.io.*;
import java.util.zip.*;
public class TestZip {

public static void main(String args[]) {
try {
String[] filenames = new String[]{"C://文件1.txt", "C://文件2.txt"};
byte[] buf = new byte[1024];

String outFilename = "C://outfile.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
for (int i=0;
i<filenames.length;
i++) {
FileInputStream in = new FileInputStream(filenames);
String fileName = filenames;
fileName = new String(filenames.getBytes("Cp437"), 0);
System.err.println(fileName);
out.putNextEntry(new ZipEntry(fileName));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
 
压缩前对文件名转码就行。
 
最好使用FileReader和FileWriter的后续类,他们对双字节编码的文件兼容性好!
 
转成什么码?
>压缩前对文件名转码就行。
 
支持的但你要先把中文文件名编码由(iso-8859-1)转为(gbk)
 
接受答案了.
 
后退
顶部