微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 SAF / DocumentFile 提取带有子目录的 zip 存档?

如何解决如何使用 SAF / DocumentFile 提取带有子目录的 zip 存档?

您可能知道,我正在努力使用邪恶的存储访问框架来提取 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

这是输出的样子:

output

谢谢

解决方法

谢谢大家,我已经准备好了,我必须构建一个像下面这样的方法,它返回 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

,

你能给出最终的代码吗

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。