1. 程式人生 > >webView載入本地檔案(zip壓縮檔案)

webView載入本地檔案(zip壓縮檔案)

 /**
     * 解壓
     *
     * @param context         上下文物件
     * @param assetName       壓縮檔名
     * @param outputDirectory 輸出目錄
     * @throws IOException
     */
    public static void unZip(Context context, String assetName,
                             String outputDirectory) throws IOException {
        //建立解壓目標目錄
        File file = new File(outputDirectory);
        //如果目標目錄不存在,則建立
        if (!file.exists()) {
            file.mkdirs();
        }
        InputStream inputStream = null;
        //開啟壓縮檔案
        inputStream = context.getAssets().open(assetName);
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        //讀取一個進入點
        ZipEntry zipEntry = zipInputStream.getNextEntry();

        byte[] buffer = new byte[1024 * 1024];
        //解壓時位元組計數
        int count = 0;
        //如果進入點為空說明已經遍歷完所有壓縮包中檔案和目錄
        while (zipEntry != null) {
            //如果是一個目錄
            if (zipEntry.isDirectory()) {
                //String name = zipEntry.getName();
                //name = name.substring(0, name.length() - 1);
                file = new File(outputDirectory + File.separator + zipEntry.getName());
                //建立資料夾
                file.mkdir();
                Log.e("zipFile資料夾", file.getAbsolutePath() + "--");
            } else {
                //如果是檔案
                file = new File(outputDirectory + File.separator
                        + zipEntry.getName());
                //建立該檔案
                file.createNewFile();
                Log.e("zipFile檔案", file.getAbsolutePath() + "--");
                //用檔案輸出流把讀取到的內容寫入建立的檔案或資料夾中
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                while ((count = zipInputStream.read(buffer)) > 0) {
                    fileOutputStream.write(buffer, 0, count);
                }
                //關閉流
                fileOutputStream.close();
            }
            //定位到下一個檔案入口
            zipEntry = zipInputStream.getNextEntry();
        }
        //關閉流
        zipInputStream.close();
    }
  /**
     * 執行解壓
     */
    AsyncTask<String, String, String> zipTask = new AsyncTask<String, String, String>() {
        @Override
        protected String doInBackground(String... params) {
            File cacheFile = Environment.getExternalStorageDirectory();

            File zipFile = new File(cacheFile.getAbsoluteFile() + "/testHtml");
            if (!zipFile.exists()) {
                zipFile.mkdirs();
            }
            try {
                
                unZip(ZipActivity.this, "dist_v0.0.1.zip", zipFile.getAbsolutePath());
                Log.d("filePath-----", zipFile.getAbsolutePath());
                return zipFile.getAbsolutePath();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return zipFile.getAbsolutePath();
        }

        @Override
        protected void onPostExecute(String s) {
            Log.e("s", s.toString());
            webView.loadUrl("file:///mnt/sdcard/testHtml/dist/index.html");
        }
    };