当前位置: 首页 > 图灵资讯 > 技术篇> 比较完善的用JAVA解压ZIP

比较完善的用JAVA解压ZIP

来源:图灵教育
时间:2024-03-07 09:39:00

以下是我编写的解压ZIP文件类别,解决了中文问题,提高了解压速度.我希望它能帮助你。如果你感觉良好,请投票给我。如果有更好的方法,请通知我 ,谢谢.

import java.io.*;import java.util.*;import java.util.zip.*;import sun.io.*;

public class linzip{public static String 8859toGB(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);elsedirname = infile.getAbsolutePath();File newdir = new File(dirname);newdir.mkdir();byte[] c = new byte[1024];int len;int slen;while (file != null){i = make859(file.getName()).replace('/','\').lastIndexOf(\\');if ( i != -1 ){File dirs = new File(dirname+File.separator+make859(file.getName()).replace('/','\').substring(0,i));dirs.mkdirs();dirs = null;}System.out.print("Extract GBB+make8859to(file.getName()).replace('/',"\""" ... ");if (file.isDirectory()){File dirs = new File(make859toGB(file.getName()).replace('/',""""";dirs.mkdir();dirs = null;}else{FileOutputStream out = new FileOutputStream(dirname+File.separator+make859(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){MessageBox(0,infile.getName()+“不是ZIP文件!“文件格式错误”,16);}catch(IOException ioe){MessageBox(0,“读”+args[0]+“时错!“文件格式错误”,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);}

WIN988上述程序&VJ6.0测试通过.