簡體   English   中英

如何解壓縮zip文件的特定目錄中的所有文件?

[英]java - How to unzip all the files in a specific directory of a zip file?

假設我有一個名為Bundles.zip的.zip文件,並且直接在Bundles.zip中 ,有幾個文件和幾個文件夾。 這就是.zip的樣子:

Bundles.zip頂級截圖

現在,我想提取從捆綁文件夾中的一切 我的程序已經知道從中提取文件所需的文件夾的名稱,在本例中為Bundles

zip中的Bundles文件夾可以包含子文件夾中的文件,子文件夾,文件,基本上都是這樣的:

Bundles.zip內部文件夾截圖

我只需要從Bundles文件夾中提取所有內容到輸出目錄。

我怎樣才能在Java中實現這一目標? 我找到了提取zip中所有文件和文件夾的答案,但我只需要提取zip中的特定文件夾,而不是一切。

到目前為止工作代碼:

            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類似於“The Island.zip”。 在拉鏈內有一個名為The Island的文件夾。 我需要在zip文件中找到與zip文件名稱匹配的文件夾,並提取其中的所有內容。

更簡單的方法是使用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;
        }
    });
}

瞧。 比使用ZipFileZipEntryZipFile得多,它還具有可重復使用從任何類型的文件系統復制文件夾結構的額外好處,不僅適用於zip文件。

該文件的路徑(文件夾)是“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);

然后從流中讀取字節並將其寫入文件。

如果您需要將代碼深入到子目錄中,可以執行類似的操作。 顯然這不會編譯,但你明白了。 該方法調用自身,並返回自身,使得可以根據需要走到子文件夾中。

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.
    }
  } 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM