今天項目提了一個新需求:把html網頁放在本地,如果背景修改了網頁,手機端要去背景下載下傳壓縮包并解壓後把本地的網頁跟新成最新的,請求背景接口,根據傳回的結果中的某個字段判斷是否需要下載下傳zip檔案,如果需要下載下傳,傳回的結果中會提供zip檔案下載下傳的位址,下載下傳後并解壓顯示網頁,既然需求提了就得去實作
先來看下實作的效果圖:
檔案下載下傳

解壓後顯示本地HTML:
1.實作檔案的下載下傳
檔案下載下傳可以參考這篇部落格:https://blog.csdn.net/yushuangping/article/details/84038582
2.檔案解壓縮的工具類:
/**
* Created by YuShuangPing on 2018/11/11.
*/
public class ZipUtils {
public static final String TAG = "ZIP";
public ZipUtils() {
}
/**
* 解壓zip到指定的路徑
*
* @param zipFileString ZIP的名稱
* @param outPathString 要解壓縮路徑
* @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)) != -1) {
// 從緩沖區(0)位置寫入(位元組)位元組
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()) {
//擷取部件的檔案夾名
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)) != -1) {
// 從緩沖區(0)位置寫入(位元組)位元組
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
}
inZip.close();
}
/**
* 壓縮檔案和檔案夾
*
* @param srcFileString 要壓縮的檔案或檔案夾
* @param zipFileString 解壓完成的Zip路徑
* @throws Exception
*/
public static void ZipFolder(String srcFileString, String zipFileString) throws Exception {
//建立ZIP
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));
//建立檔案
File file = new File(srcFileString);
//壓縮
LogUtils.LOGE("---->"+file.getParent()+"==="+file.getAbsolutePath());
ZipFiles(file.getParent()+ File.separator, file.getName(), outZip);
//完成和關閉
outZip.finish();
outZip.close();
}
/**
* 壓縮檔案
*
* @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 {
//檔案夾
String fileList[] = file.list();
//沒有子檔案和壓縮
if (fileList.length <= 0) {
ZipEntry zipEntry = new ZipEntry(fileString + File.separator);
zipOutputSteam.putNextEntry(zipEntry);
zipOutputSteam.closeEntry();
}
//子檔案和遞歸
for (int i = 0; i < fileList.length; i++) {
ZipFiles(folderString+fileString+"/", fileList[i], zipOutputSteam);
}
}
}
/**
* 傳回zip的檔案輸入流
*
* @param zipFileString zip的名稱
* @param fileString ZIP的檔案名
* @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);
}
/**
* 傳回ZIP中的檔案清單(檔案和檔案夾)
*
* @param zipFileString ZIP的名稱
* @param bContainFolder 是否包含檔案夾
* @param bContainFile 是否包含檔案
* @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()) {
// 擷取部件的檔案夾名
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;
}
}
3.activity中實作下載下傳解壓顯示html頁面
public class MainActivity extends BaseActivity implements LoginContract.View {
..........
private String savePath = FileUtils.getFileDir() + "thinkyun" + File.separator + "download.zip";//下載下傳檔案的存儲絕對路徑
private String unZipPath = FileUtils.getFileDir() + "thinkyun" + File.separator + "download";//解壓的zip檔案路徑
..........
//請求網絡成功的傳回
@Override
public void onLoginSuccess(LoginUserBean2 bean) {
int update = bean.getUpdate();
//zip壓縮包的下載下傳位址
downloadUrl = bean.getZipurl();
if (update == 1) {//需要下載下傳新的壓縮包
File file = new File(savePath);
if (file.exists()) {
file.delete();
}
//開始下載下傳ZIP壓縮包
FileDownloadUtils.getInstance().startDownLoadFileSingle(downloadUrl, savePath,
new FileDownloadUtils.FileDownLoaderCallBack() {
@Override
public void downLoadCompleted(BaseDownloadTask task) {
LogUtils.LOGD("下載下傳完成========");
try {
//解壓ZIP壓縮包
ZipUtils.UnZipFolder(savePath, unZipPath);
File file = new File(unZipPath + "/view/homepage/homepage.html");
if (file.exists()) {
tv_progress.setVisibility(View.GONE);
//顯示html網頁
webView.loadUrl("file:" + unZipPath + "/view/homepage/homepage.html");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void downLoadError(BaseDownloadTask task, Throwable e) {
}
@Override
public void downLoadProgress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
tv_progress.setText("下載下傳進度:" + soFarBytes + "/" + totalBytes);
}
});
}
}
..........
}
到此就完成了上述功能
我是存儲在getFileDir()路徑下即:/data/data/包名/files下,在AndroidStudio中可以檢視到: