zip解压,中文文件名问题
如题。 问题点数:100、回复次数:3Top
1 楼juson()回复于 2002-11-18 14:57:41 得分 0
把相关类反编译,再修改?
自己重写?
要么等着sun自己在下个版本做修改。Top
2 楼zhkn(小鱼儿)回复于 2002-11-27 15:26:50 得分 100
今天也遇到这个问题,好不容易解决了,不过还是不爽,因为修改了相关类,而不能自己另写类。方法如下:
1.将/jdk/jre/lib/rt.jar解包
反编译ZipInputStream.class
修改getUTF8String函数为:
private static String getUTF8String(byte b[], int off, int len)
{
String s = "";
try
{
s = new String(b, off, len, "GBK");
}
catch(Exception e)
{
System.out.println(e.toString());
}
return s;
}
编译,打包。
2.自己写的解包的类:
import java.io.*;
import java.util.*;
import java.util.zip.*;
import sun.io.*;
public class UnZip
{
public static void main(String filename)
{
System.out.println(filename);
File infile = new File(filename);
try{
//检查是否是zip文件
ZipFile zip = new ZipFile(infile);
zip.close();
//建立与目标文件的输入连接
ZipInputStream in = new ZipInputStream(new FileInputStream(infile));
ZipEntry file = in.getNextEntry();
int i =infile.getAbsolutePath().lastIndexOf('.');
String dirname = new String();
if ( i != -1 )
dirname = infile.getAbsolutePath().substring(0,i);
else
dirname = infile.getAbsolutePath();
File newdir = new File(dirname);
newdir.mkdir();
byte[] c = new byte[1024];
int len;
int slen;
while (file != null){
i = file.getName().replace('/','\\').lastIndexOf('\\');
if ( i != -1 ){
File dirs = new File(dirname+File.separator+file.getName().replace('/','\\').substring(0,i));
dirs.mkdirs();
dirs = null;
}
System.out.print("extract "+file.getName().replace('/','\\')+" ........ ");
if (file.isDirectory()){
File dirs = new File(file.getName().replace('/','\\'));
dirs.mkdir();
dirs = null;
}
else{
FileOutputStream out = new FileOutputStream(dirname+File.separator+file.getName().replace('/','\\'));
while((slen = in.read(c,0,c.length)) != -1)
out.write(c,0,slen);
out.close();
}
System.out.print("o.k.\n");
file = in.getNextEntry();
}
in.close();
}catch(ZipException zipe){
System.out.println(infile.getName() + "不是一个zip文件!");
}catch(IOException ioe){
System.out.println("读取"+filename+"时错误!");
}catch(Exception i){
System.out.println("over");
}
}
}
希望能解决你的问题,大家探讨一下更好的解决办法。Top
3 楼wjmmml(笑着悲伤)回复于 2002-11-27 15:49:43 得分 0
其实就是中文问题,你对文件名重新编码就可以了,用如下的函数:
public static String UnicodeToChinese(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
public static String ChineseToUnicode(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}Top




