1

如您所知,我正在努力使用邪恶的存储访问框架提取 zip 文件内容;我不能使用任何 File 对象,所以我必须使用 ZipInputStream、ZipOutputStream 和 DocumentFile,这是 zip 文件结构:

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

这是我的代码:

    public void extractZipFile(DocumentFile srcZipFile, DocumentFile destDir) throws IOException
    {
        ZipEntry entry;

        InputStream inputStream = resolver.openInputStream(srcZipFile.getUri());

        try (java.util.zip.ZipInputStream zipInputStream = new java.util.zip.ZipInputStream(inputStream))
        {
            while ((entry = zipInputStream.getNextEntry()) != null)
            {
                DocumentFile currentDestDir = destDir;

                if (!entry.isDirectory())
                {
                    unzipFile(entry, zipInputStream, currentDestDir);
                }
                else
                {
                    String finalFolderName = entry.getName().replace("/", "");
                    currentDestDir = destDir.createDirectory(finalFolderName);
                }
            }
        }

        inputStream.close();
    }

    private void unzipFile(ZipEntry fileEntry, java.util.zip.ZipInputStream zipInputStream, DocumentFile destDir) throws IOException
    {
        int readLen;
        byte[] readBuffer = new byte[BUFFER_SIZE];

        DocumentFile destFile = destDir.createFile("*/*", fileEntry.getName());

        try (OutputStream outputStream = resolver.openOutputStream(destFile.getUri()))
        {
            while ((readLen = zipInputStream.read(readBuffer)) != -1)
            {
                outputStream.write(readBuffer, 0, readLen);
            }
        }
    }

下面是输出的样子:

输出

谢谢

4

3 回答 3

1

谢谢大家,我已经准备好了,我必须构建一个类似下面的方法,它返回 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;
}
于 2021-02-22T01:39:36.440 回答
0

您可以使用SimpleStorage压缩/解压缩 ZIP 文件:

val zipFile: DocumentFile = ...
zipFile?.decompressZip(applicationContext, targetFolder, object : ZipDecompressionCallback<DocumentFile>(uiScope) {
    override fun onCompleted(
        zipFile: DocumentFile,
        targetFolder: DocumentFile,
        bytesDecompressed: Long,
        totalFilesDecompressed: Int,
        decompressionRate: Float
    ) {
        Toast.makeText(applicationContext, "Decompressed $totalFilesDecompressed files from ${zipFile.name}", Toast.LENGTH_SHORT).show()
    }
    override fun onFailed(errorCode: ErrorCode) {
        Toast.makeText(applicationContext, "$errorCode", Toast.LENGTH_SHORT).show()
    }
})

对于压缩文件,您可以使用扩展功能List<DocumentFile>.compressToZip()

于 2022-01-24T19:27:47.100 回答
0

使用Unzip();方法表单FileUtilsPlus

于 2021-02-15T03:15:29.797 回答