use*_*729 3 java zip extract unzip subdirectory
假设我有一个名为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?
Run Code Online (Sandbox Code Playgroud)
对于到目前为止的代码,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;
}
});
}
Run Code Online (Sandbox Code Playgroud)
瞧.比使用ZipFile和更清洁ZipEntry,并且它具有可重复使用从任何类型的文件系统复制文件夹结构的额外好处,不仅适用于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...
}
}
Run Code Online (Sandbox Code Playgroud)
ZipFile 有一个 getInputStream 方法来获取给定 ZipEntry 的输入流,因此,如下所示:
InputStream instream = zipFile.getInputStream(ze);
Run Code Online (Sandbox Code Playgroud)
然后从流中读取字节并将其写入文件。
如果您需要代码深入子目录 1+ 层,您可以执行类似的操作。显然这不会编译,但你明白了。该方法调用自身,并返回自身,从而可以根据需要深入到子文件夹。
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.
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7472 次 |
| 最近记录: |