以下是本人编写的关于解压zip文件的类,解决了中文问题及提高了解压的速度.希望对大家有所帮助,如果觉的好,请给我投一票,如果有什么更好方法,请通知我 ,谢谢.
import java.io.*;
import java.util.*;
import java.util.zip.*;
import sun.io.*;
public class linzip
{
public static string make8859togb(string str)
{
try{
string str8859 = new string(str.getbytes("8859_1"),"gb2312");
return str8859;
}catch(unsupportedencodingexception ioe){
return str;
}
}
public static void main(string[] args)
{
if (args.length < 1){
system.err.println("required parameter missing!");
system.exit(-1);
}
file infile = new file(args[0]);
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 = make8859togb(file.getname()).replace('/','/').lastindexof('/');
if ( i != -1 ){
file dirs = new file(dirname+file.separator+make8859togb(file.getname()).replace('/','/').substring(0,i));
dirs.mkdirs();
dirs = null;
}
system.out.print("extract "+make8859togb(file.getname()).replace('/','/')+" ........ ");
if (file.isdirectory()){
file dirs = new file(make8859togb(file.getname()).replace('/','/'));
dirs.mkdir();
dirs = null;
}
else{
fileoutputstream out = new fileoutputstream(dirname+file.separator+make8859togb(file.getname()).replace('/','/'));
while((slen = in.read(c,0,c.length)) != -1)
out.write(c,0,slen);
out.close();
}
system.out.print("o.k.");
file = in.getnextentry();
}
in.close();
}catch(zipexception zipe){
messagebox(0,infile.getname()+"不是一个zip文件!","文件格式错误",16);
}catch(ioexception ioe){
messagebox(0,"读取"+args[0]+"时错误!","文件读取错误",16);
}catch(exception i){
system.out.println("over");
}
}
/**
* @dll.import("user32", auto)
*/
public static native int messagebox(int hwnd, string lptext, string lpcaption, int utype);
}
以上程序在win98&vj6.0测试通过.本人e-mail: lsf82@citiz.net
闽公网安备 35060202000074号