2009-11-10 82 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

相关问题