2015-04-06 200 views
2

說我有一個.zip文件叫Bundles.zip並直接在Bundles.zip,有幾個文件和一些文件夾。這是該.zip樣子:java - 如何解壓zip文件的特定目錄中的所有文件?

Bundles.zip top level screenshot

現在,我想提取一切捆綁文件夾。我的程序已經知道它需要提取文件的文件夾的名稱,在這種情況下,軟件包

的zip內的捆綁文件夾可以有文件,子文件夾,文件中的子文件夾,任何事情,就像這樣:

Bundles.zip inner folder screenshot

而我只是需要提取一切從捆綁文件夾複製到輸出目錄。

我該如何在Java中完成這項工作?我找到了解壓縮所有文件和文件夾的答案,但我只需要提取壓縮文件中的特定文件夾,而不是一切。到目前爲止

工作代碼:

  ZipFile zipFile = new ZipFile(mapsDirectory + "mapUpload.tmp"); 
      Enumeration zipEntries = zipFile.entries(); 
      String fname; 
      String folderToExtract = ""; 
      String originalFileNameNoExtension = originalFileName.replace(".zip", ""); 

      while (zipEntries.hasMoreElements()) { 
       ZipEntry ze = ((ZipEntry)zipEntries.nextElement()); 

       fname = ze.getName(); 

       if (ze.isDirectory()) //if it is a folder 
       { 

        if(originalFileNameNoExtension.contains(fname)) //if this is the folder that I am searching for 
        { 
         folderToExtract = fname; //the name of the folder to extract all the files from is now fname 
         break; 
        } 
       } 
      } 

      if(folderToExtract == "") 
      { 
       printError(out, "Badly packaged Unturned map error: " + e.getMessage()); 
       return; 
      } 


      //now, how do i extract everything from the folder named folderToExtract? 

的代碼到目前爲止,originalFileName是一樣的東西 「的Island.zip」。在拉鍊內有一個名爲The Island的文件夾。我需要在zip文件中找到與zip文件名稱相匹配的文件夾,並提取其中的所有內容。

+0

你的工作代碼在哪裏?java zip庫允許你迭代一個zip文件的內容就好了 – kolossus

+0

@kolossus - 添加我的工作代碼到目前爲止找到要提取的文件夾的名稱。現在的問題是,我如何提取名爲folderToExtract的文件夾? – user3149729

回答

0

該文件的路徑(文件夾)是「zipEntry.getName()」返回的內容的一部分,只要知道文件是否位於您所尋找的文件夾中,就會得到所需的信息。

我會做這樣的事情:

while (zipEntries.hasMoreElements()) { 
    //fname should have the full path 
    if (ze.getName().startsWith(fname) && !ze.isDirectory()) 
    //it is a file within the dir, and it isn't a dir itself 
    ...extract files... 
    } 
} 

的ZipFile有getInputStream方法來獲取給定的ZipEntry輸入流,所以,像這樣:

InputStream instream = zipFile.getInputStream(ze); 

然後讀取字節流並將它們寫入文件。

如果你需要你的代碼在子目錄中遍歷1 +級以上,你可以這樣做。顯然這不會編譯,但你明白了。該方法自行調用,並返回到自身,使其可以根據需要走到子文件夾中。

private void extractFiles(String folder) { 
    //get the files for a given folder 
    files = codeThatGetsFilesAndDirs(folder); 

    for(file in files) { 
    if(file.isDirectory()) { 
     extractFiles(file.getName()); 
    } else { 
     //code to extract the file and writes it to disk. 
    } 
    } 
} 
+0

這工作獲取壓縮文件夾中的文件,但我如何處理該文件夾內的子目錄?我想從中提取文件的zip文件夾中也有子目錄,並且這些子目錄中也有文件。 – user3149729

+0

你可以編寫一個往復運動的方法來遍歷它自己,以便它可以遍歷目錄樹......這有點令人滿意。我會發佈一個簡單的例子。 – slambeth

2

更簡單的方法來做到這一點是使用Java 7中的NIO API我剛剛做了,對我的項目之一:

private void extractSubDir(URI zipFileUri, Path targetDir) 
     throws IOException { 

    FileSystem zipFs = FileSystems.newFileSystem(zipFileUri, new HashMap<>()); 
    Path pathInZip = zipFs.getPath("path", "inside", "zip"); 
    Files.walkFileTree(pathInZip, new SimpleFileVisitor<Path>() { 
     @Override 
     public FileVisitResult visitFile(Path filePath, BasicFileAttributes attrs) throws IOException { 
      // Make sure that we conserve the hierachy of files and folders inside the zip 
      Path relativePathInZip = pathInZip.relativize(filePath); 
      Path targetPath = targetDir.resolve(relativePathInZip.toString()); 
      Files.createDirectories(targetPath.getParent()); 

      // And extract the file 
      Files.copy(filePath, targetPath); 

      return FileVisitResult.CONTINUE; 
     } 
    }); 
} 

瞧。比使用ZipFileZipEntry要乾淨得多,而且它具有可用於從任何類型的文件系統複製文件夾結構的額外好處,不僅適用於zip文件。

相關問題