load html(whole website) in webview from a zipfile without extracting it on card/phone store

  android, webview, zip

i have a zipfile which contain whole website js css images. i want to load it in webview. i dont want to extract it in phone memory i have to load it from RAM.

so far what i have done is

this will hold all the files in byte

Map<String , byte[]> allWeb = new HashMap<>(); 

to unzip files i have this function.

 private void unpackZip()
    {
        InputStream is;
        ZipInputStream zis;
        String html = null;
        try
        {
            is = getAssets().open("page.zip");
            zis = new ZipInputStream(new BufferedInputStream(is));
            ZipEntry ze;

            while((ze = zis.getNextEntry()) != null)
            {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int count;

                String filename = ze.getName();
                filename = filename.substring(filename.lastIndexOf('/') + 1);
               // Log.e("error" , filename+" ");

                //FileOutputStream fout = new FileOutputStream(path + filename);

                // reading and writing

                    while ((count = zis.read(buffer)) != -1) {
                        //Log.e("te" , "1");
                        baos.write(buffer, 0, count);


                    }
                    byte[] data = baos.toByteArray();
                    allWeb.put(filename , data);
                    baos.reset();
                    zis.closeEntry();
            }

            zis.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();

        }
    }

then i read first page

 String html = new String(allWeb.get("index.html"));

i have to change local local file src links to external so i can get resource loading call back in webclient "shouldInterceptRequest" function.

 String makeWebViewFriendly(String html)
    {
     
        html = html.replaceAll("src=" , "src=https://");
        html = html.replaceAll("href=" , "href=https://");
        return html;
    }

my webview client function where i inject local resource.

 public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
       
        final String url = request.getUrl().toString();
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        String fileName = url.substring(url.lastIndexOf('/') + 1);
        byte[] data = allWeb.get(fileName);
        if(data == null)
            return null;
        else
        {
          
            return new WebResourceResponse(mime , "utf-8" , new ByteArrayInputStream( data ));
        }

    }

but still website is not loading.
is there any better way to handle is issue. load a website from zip without extracting it. website have css js and images

Source: Android Questions

LEAVE A COMMENT