熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Javascript >> 正文

用JAVA解壓ZIP

2022-06-13   來源: Javascript 

  關於解壓ZIP文件的類解決了中文問題及提高了解壓的速度.希望對大家有所幫助.
  import javaio*;
  import javautil*;
  import javautilzip*;
  import sunio*;
  
  public class linzip
  {
  public static String maketoGB(String str)
  {
   try{
   String str = new String(strgetBytes(_)GB);
   return str;
   }catch(UnsupportedEncodingException ioe){
   return str;
   }
  }
  
  public static void main(String[] args)
  {
   if (argslength < 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測試通過.

From:http://tw.wingwit.com/Article/program/Java/Javascript/201311/25289.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.