I have a website in a ZIP file. I would like to unzip it in memory (not to local disk) and then display it in WebView. This ZIP file has the image directory with images and these files should also be accessible to WebVie开发者_如何学JAVAw. I'm thinking of creating new ContentProvider but I'm not sure I know how to in this case :).
Please help code examples. Thx!
You can look into 2 things:
- using pipe stream as in FileProvider
- Using the new APK expansion as base for your logic here
I have the following in my content provider:
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());
}
And the pipe stream:
@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();
}
}
精彩评论