我有一个网站的ZIP文件。我想在内存中解压缩它(不是到本地磁盘),然后在WebView中显示它。这个ZIP文件包含包含图像的图像目录,这些文件也应该可以被WebView访问。我正在考虑创建新的ContentProvider,但我不确定我知道如何在这种情况下:)。
请帮助代码示例。谢谢!
你可以看两件事:
- 在FileProvider 中使用管道流
- 使用新的APK扩展作为您在这里的逻辑基础
在我的内容提供程序中有以下内容:
private ContentProvider.PipeDataWriter<ZipFile> pipe = new EcoPipeDataWriter();
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode)
throws FileNotFoundException {
ZipFile zip = getZip(uri);
Bundle data = new Bundle();
// put file name in bundle
return new AssetFileDescriptor(openPipeHelper(uri, null, data, zip,
pipe), 0, zip.getUncompressSize());
}
和管道流:
@Override
public void writeDataToPipe(ParcelFileDescriptor output, Uri uri, String s, Bundle bundle, ZipFile zipFile) {
final String filePath = bundle.getString(FILE_PATH_KEY);
byte[] buffer = new byte[8192];
FileOutputStream fout = new FileOutputStream(output.getFileDescriptor());
try {
// iterate throught zipfile until filenamea has been reach
InputStream in = zipFile.getInputStream(fileHeader);
int n;
while ((n = in.read(buffer)) >= 0) {
fout.write(buffer, 0, n);
}
} catch (ZipException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}