2009-11-10 77 views
1

我有一個壓縮文件。該文件還包含各種目錄和文件。我想提取所有這些並保存在指定的路徑中。什麼是使用java提取zip文件的最佳方式

那麼如何編寫一個java程序來提取壓縮文件。

感謝 蘇尼爾·庫馬爾Sahoo

+0

可能重複[如何解壓遞歸的Java文件?(http://stackoverflow.com/questions/981578/how-to-unzip-files-recursively-in -java) – Kevin 2014-05-07 02:10:50

回答

1
public static void unzip(){ 
     try{ 
      BufferedOutputStream out = null; 
      ZipInputStream in = new ZipInputStream(ZipFileExtracter.class.getClassLoader().getResourceAsStream("com/artificialmachines/chitme/stamps/ChitMeData.zip")); 
      ZipEntry entry; 
      boolean isDirectory=false; 
      while((entry = in.getNextEntry()) != null){ 
       int count; 
       byte data[] = new byte[BUFFER]; 
       // write the files to the disk 
       String entryName = entry.getName(); 
       File newFile = new File(new StringBuffer().append(System.getProperty("user.dir")).append(File.separator).append(entryName).toString()); 
       if(entryName.endsWith("/")){ 
        isDirectory=true; 
        newFile.mkdir(); 
        //System.out.println("This is directory "+newFile.exists()+" IS DIr "+newFile.isDirectory()+" path "+newFile.getPath()); 
       }else{ 
        newFile.createNewFile(); 
       } 
       if(!isDirectory){ 
        out = new BufferedOutputStream(new FileOutputStream(newFile),BUFFER); 
        while ((count = in.read(data,0,BUFFER)) != -1){ 
         out.write(data,0,count); 
        } 
        cleanUp(out); 
       } 
       isDirectory=false; 
      } 
      cleanUp(in); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
      System.exit(0); 
     } 
    } 
1

java.util.zip。*執行所需的操作。 查看參考API。如果您正在尋找例如LookThis

相關問題