Java:提取具有多个子目录的 zip 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23920684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:03:57  来源:igfitidea点击:

Java: Extracting zip file with multiple subdirectories

javaunzip

提问by user3685130

I have a .zip(Meow.zip) and it has multiple files and folders, like so

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

  1. Meow.zip
    • File.txt
    • Program.exe
    • Folder
      • Resource.xml
      • AnotherFolder
        • OtherStuff
          • MoreResource.xml
  1. 喵喵.zip
    • 文件.txt
    • 程序.exe
    • 文件夹
      • 资源文件
      • 另一个文件夹
        • 其他的东西
          • 更多资源.xml

I have looked everywhere but I could not find anything that works. Thanks in advance!

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

采纳答案by slaadvak

Here is a class unzipping files from a zip file and recreating the directory tree as well.

这是一个从 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();
        }
    }

}

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

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

回答by Aron Lorincz

Your friend is ZipFileor ZipInputStremclass.

你的朋友是ZipFileZipInputStrem类。