In the last week of 2020, we are preparing to touch the fish and go home for the Chinese New Year. On the vertical day, the group leader came to me with a strange smile:

Group leader: “The Spring Festival is coming soon. Your journey home is far away. Do you want to ask for two days’ leave?”

I: “group leader, you are really my bosom friend, think what I think, think what I think, you say so I don’t mention it, that I please two days”

Group leader: “line, leave affirmation no problem, I always very take care of brothers!!” (At that moment, a warm current in my heart and had already forgotten the “squeeze” he put on me this year)

“But I have one more thing to tell you. I have a need you need to complete before you go home.”

Me: “what??? ,,,,,, TMD……”

Group leader: “The demand is like this: recently, the customer responded that the HTML loading is a little slow. It needs to be optimized. It is best to open ,,,, in seconds.

I: “is not this H5, load slowly that your front end reason ah, you look for me… I…” (The group leader has gone)

With a heavy heart began to study optimization, began to work in the webView layer, open caching, preloading, a small operation effect.

And then you start to look at the front-end file. Notice that local Html files load much faster than urls. So I went to the front end and asked for a local file to put in the project for local loading. Sure enough, the speed went fast, and then the awkward thing happened. The front-end features were constantly updated, and if I put them in the project, I would have to upgrade the version. Not to mention that I’m tired to death, I think the project will be referred to the head of the team, he will bring a knife to meet. So a new way, will be through the interface to download HTMl files, stored in the phone local, so that the webView to load the phone local files. An eerie smile spread across his face.

1. Used FileDownloader to download compressed Html files

Implementation 'com. Liulishuo. Filedownloader: library: 1.7.7'Copy the code

Package download utility class:

public class FileDownloadUtils { public static FileDownloadUtils instance = null; public FileDownloadUtils() { } public static FileDownloadUtils getInstance() { if (null == instance) { instance = new FileDownloadUtils(); } return instance; } /** ** single task download ** @param downLoadUri Network address for downloading files * @param destinationUri Absolute path for storing downloaded files */ public void startDownLoadFileSingle(String downLoadUri, String destinationUri,FileDownLoaderCallBack callBack) { FileDownloader.getImpl().create(downLoadUri).setPath(destinationUri).setListener(fileDownloadListener(callBack)).start() ; } private FileDownloadListener FileDownloadListener (final FileDownLoaderCallBack callBack) {return new FileDownloadListener() {@override protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {// Wait, } @override protected void progress(BaseDownloadTask task, int soFarBytes, Int totalBytes) {// Download progress callBack if (callBack! = null){ callBack.downLoadProgress(task,soFarBytes,totalBytes); }} @override protected void completed(BaseDownloadTask task) {// Complete the download if (callBack! = null){ callBack.downLoadCompleted(task); } } @Override protected void paused(BaseDownloadTask task, int soFarBytes, Int totalBytes) {Override protected void error(BaseDownloadTask task, Throwable e) {if (callBack! = null){ callBack.downLoadError(task,e); }} @override protected void warn(BaseDownloadTask task) {// A task already exists in the download queue (waiting/downloading) with the same download connection and the same storage path}}; } public interface FileDownLoaderCallBack {// Whether the file has been downloaded void downLoadCompleted(BaseDownloadTask task); // Whether the file fails to be downloaded void downLoadError(BaseDownloadTask task, Throwable e); // file downLoadProgress void downLoadProgress(BaseDownloadTask task, int soFarBytes, int totalBytes); }}Copy the code

Decompress the Zip file

public class ZipUtils { public static final String TAG = "ZIP"; Public ZipUtils() {} /** * Decompress the zip to the specified path ** @param zipFileString Zip name * @param outPathString Path to decompress * @throws Exception */ public static void UnZipFolder(String zipFileString, String outPathString) throws Exception { ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString)); ZipEntry zipEntry; String szName = ""; while ((zipEntry = inZip.getNextEntry()) ! = null) { szName = zipEntry.getName(); if (zipEntry.isDirectory()) { szName = szName.substring(0, szName.length() - 1); File folder = new File(outPathString + File.separator + szName); folder.mkdirs(); } else { Log.e(TAG, outPathString + File.separator + szName); File file = new File(outPathString + File.separator + szName); if (! file.exists()) { Log.e(TAG, "Create the file:" + outPathString + File.separator + szName); file.getParentFile().mkdirs(); file.createNewFile(); FileOutputStream out = new FileOutputStream(file); int len; byte[] buffer = new byte[1024]; While ((len = inzip.read (buffer))! Write (buffer, 0, len); // Write (byte) bytes out. Write (buffer, 0, len); out.flush(); } out.close(); } } inZip.close(); } public static void UnZipFolder(String zipFileString, String outPathString, String szName) throws Exception { ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString)); ZipEntry zipEntry; while ((zipEntry = inZip.getNextEntry()) ! = null) { //szName = zipEntry.getName(); If (zipentry.isdirectory ()) {// Get the folder name of the component szName = szname.substring (0, szname.length () -1); File folder = new File(outPathString + File.separator + szName); folder.mkdirs(); } else { Log.e(TAG, outPathString + File.separator + szName); File file = new File(outPathString + File.separator + szName); if (! file.exists()) { Log.e(TAG, "Create the file:" + outPathString + File.separator + szName); file.getParentFile().mkdirs(); file.createNewFile(); FileOutputStream out = new FileOutputStream(file); int len; byte[] buffer = new byte[1024]; While ((len = inzip.read (buffer))! Write (buffer, 0, len); // Write (byte) bytes out. Write (buffer, 0, len); out.flush(); } out.close(); } } inZip.close(); } /** * compressed files and folders ** @param srcFileString File or folder to be compressed * @param zipFileString Zip path to be decompressed * @throws Exception */ public static void ZipFolder(String srcFileString, String zipFileString) throws Exception {// Create ZIP ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString)); File File = new File(srcFileString); / / / / compression LogUtils. LOGE (" -- -- -- -- > "+ file. GetParent () +" = = = "+ file. GetAbsolutePath ()); ZipFiles(file.getParent()+ File.separator, file.getName(), outZip); // Finish and close outzip.finish (); outZip.close(); } /** * Compressed file ** @param folderString * @param fileString * @param zipOutputSteam * @throws Exception */ private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam) throws Exception { // LogUtils.LOGE("folderString:" + folderString + "\n" +"fileString:"  + fileString + "\n=========================="); if (zipOutputSteam == null) return; File file = new File(folderString + fileString); if (file.isFile()) { ZipEntry zipEntry = new ZipEntry(fileString); FileInputStream inputStream = new FileInputStream(file); zipOutputSteam.putNextEntry(zipEntry); int len; byte[] buffer = new byte[4096]; while ((len = inputStream.read(buffer)) ! = -1) { zipOutputSteam.write(buffer, 0, len); } zipOutputSteam.closeEntry(); } else {// folder String fileList[] = file.list(); If (filelist. length <= 0) {ZipEntry ZipEntry = new ZipEntry(fileString + file.separator); zipOutputSteam.putNextEntry(zipEntry); zipOutputSteam.closeEntry(); } // Subfiles and recursion for (int I = 0; i < fileList.length; i++) { ZipFiles(folderString+fileString+"/", fileList[i], zipOutputSteam); }}} /** * returns the zip file InputStream ** @param zipFileString zip name * @param fileString zip file name * @return InputStream * @throws Exception */ public static InputStream UpZip(String zipFileString, String fileString) throws Exception { ZipFile zipFile = new ZipFile(zipFileString); ZipEntry zipEntry = zipFile.getEntry(fileString); return zipFile.getInputStream(zipEntry); } /** * returns a list of files (files and folders) in ZIP ** @param zipFileString ZIP name * @param bContainFolder whether folders are included * @param bContainFile Whether files are included  * @return * @throws Exception */ public static List<File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile) throws Exception { List<File> fileList = new ArrayList<File>(); ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString)); ZipEntry zipEntry; String szName = ""; while ((zipEntry = inZip.getNextEntry()) ! = null) { szName = zipEntry.getName(); If (zipentry.isdirectory ()) {// Get the folder name of the component szName = szname.substring (0, szname.length () -1); File folder = new File(szName); if (bContainFolder) { fileList.add(folder); } } else { File file = new File(szName); if (bContainFile) { fileList.add(file); } } } inZip.close(); return fileList; }}Copy the code

Download:

File file = new File(Constants.saveH5FilePath); if (file.exists()) { file.delete(); } / / download ZIP ZIP FileDownloadUtils. GetInstance () startDownLoadFileSingle (bean. The getUrl (), the saveH5FilePath, new FileDownloadUtils.FileDownLoaderCallBack() { @Override public void downLoadCompleted(BaseDownloadTask task) { try { / / uncompress ZIP ZIP ZipUtils. UnZipFolder (the saveH5FilePath, the unH5ZipPath); PreferencesUtil.getInstance().saveParam("H5VersionName", H5VersionName); } catch (Exception e) { e.printStackTrace(); } } @Override public void downLoadError(BaseDownloadTask task, Throwable e) { } @Override public void downLoadProgress(BaseDownloadTask task, int soFarBytes, int totalBytes) { } });Copy the code

The webView to load:

 mWebSe.loadUrl("file:"+ Constants.unH5ZipPath+"/index.html");
Copy the code

At this time, the heart is like calm water, go home, search ga…