Java:使用多个子目录提取zip文件

我有一个.zip(Meow.zip),它有多个文件和文件夹,就像这样

  1. Meow.zip
    • FILE.TXT
    • Program.exe文件
      • Resource.xml
      • AnotherFolder
        • 其他的东西
          • MoreResource.xml

我到处都看,但找不到任何有用的东西。 提前致谢!

这是一个从zip文件解压缩文件并重新创建目录树的类。

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class ExtractZipContents { public static void main(String[] args) { try { // Open the zip file ZipFile zipFile = new ZipFile("Meow.zip"); Enumeration enu = zipFile.entries(); while (enu.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) enu.nextElement(); String name = zipEntry.getName(); long size = zipEntry.getSize(); long compressedSize = zipEntry.getCompressedSize(); System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n", name, size, compressedSize); // Do we need to create a directory ? File file = new File(name); if (name.endsWith("/")) { file.mkdirs(); continue; } File parent = file.getParentFile(); if (parent != null) { parent.mkdirs(); } // Extract the file InputStream is = zipFile.getInputStream(zipEntry); FileOutputStream fos = new FileOutputStream(file); byte[] bytes = new byte[1024]; int length; while ((length = is.read(bytes)) >= 0) { fos.write(bytes, 0, length); } is.close(); fos.close(); } zipFile.close(); } catch (IOException e) { e.printStackTrace(); } } } 

资料来源: http : //www.avajava.com/tutorials/lessons/how-do-i-unzip-the-contents-of-a-zip-file.html

你的朋友是ZipFile或ZipInputStrem类。