![](/img/trans.png)
[英]How to unzip all the password protected zip files in a directory using Java
[英]java - How to unzip all the files in a specific directory of a zip file?
假设我有一个名为Bundles.zip的.zip文件,并且直接在Bundles.zip中 ,有几个文件和几个文件夹。 这就是.zip的样子:
现在,我想提取从捆绑文件夹中的一切 。 我的程序已经知道从中提取文件所需的文件夹的名称,在本例中为Bundles 。
zip中的Bundles文件夹可以包含子文件夹中的文件,子文件夹,文件,基本上都是这样的:
我只需要从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;
}
});
}
瞧。 比使用ZipFile
和ZipEntry
要ZipFile
得多,它还具有可重复使用从任何类型的文件系统复制文件夹结构的额外好处,不仅适用于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.