服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用java解压zip


  关于解压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./n");
   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测试通过.

扫描关注微信公众号