安卓:将ZIP文件解压到内存并在WebView中显示其内容

4
我有一个存放在ZIP文件中的网站。我想将其解压到内存中(而不是本地磁盘),然后在WebView中显示它。这个ZIP文件有一个图像目录,其中包含图片,这些文件也应该能够被WebView访问。我考虑创建新的ContentProvider,但我不确定如何在这种情况下实现 :)

请提供代码示例。

谢谢!

1个回答

1

你可以看两个东西:

  1. 使用管道流作为 FileProvider
  2. 将新的 APK 扩展用作你的逻辑 here

我在我的内容提供者中有以下内容:

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();
    }
}

很好的回答,但是EcoPipeDataWriter类是什么?你能提供更多细节吗? - fvisticot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接