问题是这样的:从服务器拿到.zip压缩文件,然后解压到手机指定的路径下面,起到覆盖更新的作用,话不多说进入正题:
FileDownloader.getImpl().create(url) .setPath(savePath) .setForceReDownload(true) .setListener(new FileDownloadListener() { @Override protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) { }
@Override
protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
}
@Override
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
tvload.setText(soFarBytes+"/"+totalBytes);
}
@Override
protected void blockComplete(BaseDownloadTask task) {
}
@Override
protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) {
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void completed(BaseDownloadTask task) {
tvload.setText("下载完成");
unPackage(savePath,Environment.getExternalStorageDirectory().getAbsolutePath() + "/com.game.rpg");
}
@Override
protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
@Override
protected void error(BaseDownloadTask task, Throwable e) {
}
@Override
protected void warn(BaseDownloadTask task) {
}
}).start();
private void unPackage(String zipPath, String outputDir){
try {
ZipUtils.UnZipFolder(zipPath,outputDir);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解压zip到指定的路径
*
* @param zipFileString ZIP的名称
* @param outPathString 要解压缩路径
*/
@RequiresApi(api = Build.VERSION_CODES.N)
public static void UnZipFolder(String zipFileString, String outPathString) throws Exception {
BufferedInputStream bi;
ZipFile zf = new ZipFile(zipFileString, Charset.forName("GBK"));
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
String entryName = ze2.getName();
String path = outPathString + "/" + entryName;
if (ze2.isDirectory()) {
System.out.println("正在创建解压目录 - " + entryName);
File decompressDirFile = new File(path);
if (!decompressDirFile.exists()) {
decompressDirFile.mkdirs();
}
} else {
System.out.println("正在创建解压文件 - " + entryName);
String fileDir = path.substring(0, path.lastIndexOf("/"));
File fileDirFile = new File(fileDir);
if (!fileDirFile.exists()) {
fileDirFile.mkdirs();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPathString + "/" + entryName));
bi = new BufferedInputStream(zf.getInputStream(ze2));
byte[] readContent = new byte[1024];
int readCount = bi.read(readContent);
while (readCount != -1) {
bos.write(readContent, 0, readCount);
readCount = bi.read(readContent);
}
bos.close();
}
}
zf.close();