问题描述
您可能知道,我正在努力使用邪恶的存储访问框架来提取 zip 文件内容;我不能使用任何 File 对象,所以我必须使用 ZipInputStream、ZipOutputStream 和 DocumentFile,这是 zip 文件结构:
%homepath%
这是我的代码:
Folder 1/ABC 001.jpg
Folder 1/ABC 002.jpg
Folder 2/ABC 003.jpg
Folder 2/ABC 004.jpg
Folder 2/Folder 3/ABC 005.jpg
Folder 2/Folder 3/ABC 006.jpg
Folder 2/Folder 3/Folder 4/ABC 007.jpg
Folder 2/Folder 3/Folder 4/ABC 008.jpg
ABC 009.jpg
这是输出的样子:
谢谢
解决方法
谢谢大家,我已经准备好了,我必须构建一个像下面这样的方法,它返回 DocumentFile 并创建必要的目录,将它张贴在这里以防有人需要......
private DocumentFile CreateFileWithDirectories(String path,DocumentFile destDir)
{
// Like ---> Folder 2/ or Folder 1/Folder 2/Folder 3/
if (path.endsWith("/"))
{
String[] tempStr = path.split("/");
DocumentFile parentDir = destDir;
DocumentFile childDir = null;
for (String st : tempStr)
{
childDir = parentDir.findFile(st);
if (childDir == null)
{
childDir = parentDir.createDirectory(st);
}
parentDir = childDir;
}
// returns null
}
// Like ---> 1 Test/Folder 2/Folder 3/ABC 005.jpg
else if (path.contains("/"))
{
String[] tempStr = path.split("/");
DocumentFile parentDir = destDir;
DocumentFile childDir = null;
for (int i = 0; i < tempStr.length - 1; i++)
{
childDir = parentDir.findFile(tempStr[i]);
if (childDir == null) // No file exists
{
childDir = parentDir.createDirectory(tempStr[i]);
}
//else --> Yes file exists
parentDir = childDir;
}
String finalFileName = path.substring(path.lastIndexOf("/") + 1);
return parentDir.createFile("*/*",finalFileName);
}
// Like ---> /
else if (path.equals("/"))
{
return null;
}
// Like ---> ABC 005.jpg or ABC 005
else
{
return destDir.createFile("*/*",path);
}
return null;
}
,
使用 Unzip();
方法形成 FileUtilsPlus 库
你能给出最终的代码吗