首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用android DocumentFile阅读ZipFile

用android DocumentFile阅读ZipFile
EN

Stack Overflow用户
提问于 2017-03-26 07:04:33
回答 2查看 957关注 0票数 6

我想用java.util.zip.ZipFile和android DocumentFile来读取一个zip文件。

插图:

使用存储访问框架,我在android系统中获取文件的uri,并使用它创建一个Documentfile。文档文件是一个具有zip文件"data.zip“的文件夹。

Data.zip有80多个条目,包括文本文件和媒体文件( >= 8mb <= 20 8mb)。因此,zip文件超过932 So。

使用filePAth,我可以使用下面的代码片段直接读取条目:

代码语言:javascript
复制
zipFile = new ZipFile(zipPath);
                ZipEntry zipEntry = zipFile.getEntry(name);
                if (zipEntry != null) {
                    return writeByteArraysToFile(ByteStreams.toByteArray(zipFile.getInputStream(zipEntry)), ext);
                }

这在不需要存储访问框架的设备上工作得很好。

对于使用SAF的设备,我使用的是DocumentFile,我阅读的内容如下:

代码语言:javascript
复制
InputStream inputStream = context.getContentResolver().openInputStream(documentFile.getUri());

                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                ZipInputStream zipInputStream = new ZipInputStream(bufferedInputStream);
                ZipEntry entry;
                while ((entry = zipInputStream.getNextEntry()) != null) {

                    Log.e("field", entry.getName());
                    if (entry.getName().contains(name)) {
                        File file = PBUtils.writeByteArraysToFile(ByteStreams.toByteArray(zipInputStream), ext);
                        zipInputStream.closeEntry();
                        inputStream.close();
                        zipInputStream.close();
                        bufferedInputStream.close();
                        return file;
                    }
                    zipInputStream.closeEntry();
                }
                inputStream.close();

这不管用,但问题是:

  1. 在迭代条目以找到特定条目时,它很慢。我调试并找出读取媒体条目所需的时间导致延迟。

我需要解决方案,说明如何使其更快,并需要知道是否有任何方法可以将ZipFile与DocumentFile结合使用,以便至少可以这样做:

代码语言:javascript
复制
zipFile = new ZipFile(documentFileUri);
                ZipEntry zipEntry = zipFile.getEntry(name);
                if (zipEntry != null) {
                    return writeByteArraysToFile(ByteStreams.toByteArray(zipFile.getInputStream(zipEntry)), ext);
                }
EN

回答 2

Stack Overflow用户

发布于 2019-11-11 16:56:48

这真是令人沮丧--谷歌( Google )正在向我们提供不必要的范围存储( Scope Storage ),但没有提供在需要时使用它的方法。这样做的一个方法是:

代码语言:javascript
复制
    // mDocFile is a DocumentFile..., you also need a context (activity, service, app...
    String getDocReadableFilePath(DocumentFile mDocFile, Context context) {
        if (mDocFile != null && mDocFile.isFile()) {
            try {
                ParcelFileDescriptor parcelFileDescriptor =
                        context.getContentResolver().openFileDescriptor(mUri, "r"); // gets FileNotFoundException here, if file we used to have was deleted
                if (parcelFileDescriptor != null) {
                    int fd = parcelFileDescriptor.detachFd(); // if we want to close in native code
                    return "/proc/self/fd/" + fd;
                }
            }
            catch (FileNotFoundException fne) {
                return "";
            }
        }
    }

    // Now you may use:
    String documentFilePath = getDocReadableFilePath(docFile, context);
    ZipFile zf = new ZipFile(documentFilePath);
票数 2
EN

Stack Overflow用户

发布于 2020-05-11 15:51:22

对于android DocumentFile,您必须使用java.util.zip.ZipInputStreamjava.util.zip.ZipOutputStream,而不是与android context.getContentResolver().openOutputStream()context.getContentResolver().openInputStream()一起使用的java.util.zip.ZipFile

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43026027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档