Nos*_*lvi 5 javascript android
我想java.util.zip.ZipFile用android 阅读一个zip文件DocumentFile
插图:
使用Storage Access Framework,我可以在android系统中获取文件的uri并使用它创建一个Documentfile。该文档文件是一个包含zip文件“ data.zip”的文件夹
Data.zip有80多个条目,包括文本文件和媒体文件(> = 8mb <= 20mb)。因此,zip文件超过932mb。
使用filePAth,我可以直接使用下面的代码段读取条目:
zipFile = new ZipFile(zipPath);
ZipEntry zipEntry = zipFile.getEntry(name);
if (zipEntry != null) {
return writeByteArraysToFile(ByteStreams.toByteArray(zipFile.getInputStream(zipEntry)), ext);
}
Run Code Online (Sandbox Code Playgroud)
这在不需要存储访问框架的设备上效果很好。
对于使用SAF的设备,我正在使用DocumentFile,并且我已经阅读了以下内容:
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();
Run Code Online (Sandbox Code Playgroud)
这不起作用,但这是问题所在:
我需要有关如何使其更快地实现的解决方案,并且需要知道是否可以将ZipFile与DocumentFile一起使用,以便至少可以执行以下操作:
zipFile = new ZipFile(documentFileUri);
ZipEntry zipEntry = zipFile.getEntry(name);
if (zipEntry != null) {
return writeByteArraysToFile(ByteStreams.toByteArray(zipFile.getInputStream(zipEntry)), ext);
}
Run Code Online (Sandbox Code Playgroud)
这真的很糟糕——谷歌强制向我们提供不需要的范围存储,但没有提供在需要时使用它的方法。一种方法是:
// 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
170 次 |
| 最近记录: |