【问题标题】:Java how to turn zip file into regular java.io.File with folders and filesJava如何将zip文件转换为带有文件夹和文件的常规java.io.File
【发布时间】:2020-12-15 13:46:54
【问题描述】:

我正在研究将压缩文件转换为常规 java.io.File 的方法,其中所有文件和文件夹的顺序与 zip 中的相同。基本上我只想解压缩压缩文件而不将其内容复制到新目的地。我已经想出了这个功能,到目前为止效果很好,但前提是 zip 中没有文件夹。 zip 中的文件不能是目录,否则我的功能将不起作用,这就是问题所在。
这是我的功能:

File unzip(File zip) throws IOException
    {
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zip));
        
        //helper directory to store files into while program is running!
        File helperDir = Files.createDirectories(Paths.get("zipFile")).toFile();  
        //helperDir.deleteOnExit();
        
        byte[] buffer = new byte[1024];
        for (ZipEntry entry; (entry = zis.getNextEntry()) != null; )
        {
            if (!entry.isDirectory()) //true if file in zip is not a folder
            {
                File newFile = new File(helperDir, entry.getName());
                //newFile.deleteOnExit();
                
                FileOutputStream fos = new FileOutputStream(newFile);
                for (int len = zis.read(buffer); len > 0; len = zis.read(buffer))
                    fos.write(buffer, 0, len);
                fos.close();
            }
            else
            {
                //What to do if there are folders in zip...
            }
        }
        
        zis.close();
        return helperDir;
    }

如何处理 zip 中的文件夹?或者,是否有更好的方法将 zip 转换为 java.io.File?
请帮忙!

【问题讨论】:

    标签: java directory zip unzip ziparchive


    【解决方案1】:

    找到目录后,您只需要多做一点设置即可。在文件复制之前使用一行newFile.mkdirs()创建所需的目录,或者在entry.isDirectory()时在else中创建目录结构:

    //What to do if there are folders in zip...
    System.out.println("Reading dir  "+entry.getName());
    File newDir = new File(helperDir, entry.getName());
    newDir.mkdirs();
    

    以上 3 行更好,因为它再现了空目录,而仅添加 newFile.mkdirs() 只会创建其中包含文件的目录。

    Java NIO 提供了一种很好的方式来执行相同的解压缩操作,这里是一个示例:

    public static void unzip(Path zip, Path dir) throws IOException {
        try (FileSystem fs = FileSystems.newFileSystem(zip)) {
            for (Path root : fs.getRootDirectories()) {
                BiPredicate<Path, BasicFileAttributes> foreach = (p,a) -> {
                    copy(p,a, Path.of(dir.toString(), p.toString()));
                    return false;
                };
                Files.find(root, Integer.MAX_VALUE, foreach).count();
            }
        }
        System.out.println("UNZIPPED "+zip +" to "+dir);
    }
    private static void copy(Path from, BasicFileAttributes a, Path target)  {
        System.out.println("Copy "+(a.isDirectory() ? "DIR " : "FILE")+" => "+target);
        try {
            if (a.isDirectory())
                Files.createDirectories(target);
            else if (a.isRegularFile())
                Files.copy(from, target, StandardCopyOption.REPLACE_EXISTING);
        }
        catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 2012-06-16
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      相关资源
      最近更新 更多