如何在Java中递归解压文件?
我有 zip 文件,其中包含一些其他 zip 文件。
例如,邮件文件是 abc.zip
,它包含 xyz.zip
、class1.java
、class2.java.
xyz.zip
包含文件 class3.java
和 class4.java
。
因此,我需要使用 Java 将 zip 文件提取到一个文件夹,该文件夹应包含 class1.java
、class2.java
、class3.java
和 <代码>class4.java。
I have zip file which contains some other zip files.
For example, the mail file is abc.zip
and it contains xyz.zip
, class1.java
, class2.java
. And xyz.zip
contains the file class3.java
and class4.java
.
So I need to extract the zip file using Java to a folder that should contain class1.java
, class2.java
, class3.java
and class4.java
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在测试中我注意到 File.mkDirs() 在 Windows 下不起作用...
/**
* 对于给定的完整路径名重新创建所有父目录
**/
In testing I noticed File.mkDirs() does not work under Windows...
/**
* for a given full path name recreate all parent directories
**/
根据我的需要进行修改,然后混合一些最佳答案。 此版本将:
递归地将 zip 解压缩到给定位置
创建空目录
正确关闭 zip
Modified as i needed then mixed in a bit of the best answers. This version will:
Recursively Extract a zip to given location
Create empty directories
Close zip properly
解压后应关闭 zip 文件。
One should CLOSE zip file after unzip.
没有第三方依赖项,防止 zip slip,完全注释,递归地重新创建目录结构,忽略空目录,健全的源代码嵌套,提取到 zip 文件的目录,并使用 UTF-8。 用法:
代码如下:
现在您已经有了核心算法,您需要检查“.zip”的文件扩展名,如果存在,则递归调用
Zip.extract( ... )
在那个文件上。No third-party dependencies, guards against zip slip, fully commented, recreates directory structure recursively, ignores empty directories, sane source code nesting, extracts to zip file's directory, and uses UTF-8. Usage:
Here's the code:
Now that you have the core algorithm in place, you need to check the file extension for ".zip" and, if present, recursively call
Zip.extract( ... )
on that file.与 NeilMonday 的答案相同,但提取空目录:
Same as NeilMonday's answer, but extracts empty directories:
这是一些代码,我测试过它工作得很好:
我在这里尝试了投票最高的答案,并且它不会递归地解压缩文件,它只是解压缩第一级的文件。
来源: 将文件提取到给定目录的解决方案
另外,请检查此同一个人的解决方案: 在内存中提取文件的解决方案
Here is some code, which I tested to be working quite well :
I tried with the top voted answer here, and that does not recursively unzip the files, it just unzips the files of the first level.
Source : Solution which extracts files into a given directory
Also, check this solution by the same person : Solution which extracts file in memory
警告,这里的代码对于受信任的 zip 文件来说是可以的,写入之前没有路径验证,这可能会导致安全漏洞,如 zip-slip-漏洞,如果您使用它来压缩来自未知客户端的上传的 zip 文件。
该解决方案与之前发布的解决方案非常相似,但该解决方案在解压缩时重新创建了正确的文件夹结构。
}
Warning, the code here is ok for trusted zip files, there's no path validation before write which may lead to security vulnerability as described in zip-slip-vulnerability if you use it to deflate an uploaded zip file from unknown client.
This solution is very similar to the previous solutions already posted, but this one recreates the proper folder structure on unzip.
}
这是一些未经测试的代码库,基于我解压缩文件的一些旧代码。
Here's some untested code base on some old code I had that unzipped files.
我采用 ca.anderson4 并删除列表 zipFiles 并重写一点,这就是我得到的:
我测试过并且它有效
I take ca.anderson4 and remove the List zipFiles and rewrite a little bit, this is what i got:
I tested and it works