运维下载升级包增加了gzip的支持

serial
BlueMatthew 1 year ago
parent d6b9ba9420
commit 0bb988ea34

@ -22,17 +22,19 @@ public class FileDownloader {
return false; return false;
HttpURLConnection connection = null; HttpURLConnection connection = null;
GZIPInputStream gZIPInputStream = null; GZIPInputStream gZIPInputStream = null;
InputStream is = null;
OutputStream os = null;
boolean res = false; boolean res = false;
try { try {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY); Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
URL url = new URL(urlString); URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection(); connection = (HttpURLConnection) url.openConnection();
// connection.setRequestProperty("Accept-Encoding", "gzip"); connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setConnectTimeout(5000); connection.setConnectTimeout(5000);
connection.setReadTimeout(60000); connection.setReadTimeout(60000);
connection.setDoInput(true); connection.setDoInput(true);
String encoding = connection.getContentEncoding(); connection.connect();
InputStream is = connection.getInputStream();
final File temp = new File(filePath); final File temp = new File(filePath);
if (temp.exists()) if (temp.exists())
temp.delete(); temp.delete();
@ -41,30 +43,37 @@ public class FileDownloader {
temp.setWritable(true, false); temp.setWritable(true, false);
downloadFile = temp; downloadFile = temp;
Log.d("download", "url " + urlString + "\n save to " + temp); Log.d("download", "url " + urlString + "\n save to " + temp);
OutputStream os = new FileOutputStream(temp); os = new FileOutputStream(temp);
String encoding = connection.getContentEncoding();
is = connection.getInputStream();
byte[] buf = new byte[8 * 1024]; byte[] buf = new byte[8 * 1024];
if(encoding.equals("gzip")) {
gZIPInputStream = new GZIPInputStream(is);
is = gZIPInputStream;
}
int len; int len;
try { try {
while ((len = is.read(buf)) != -1) { while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len); os.write(buf, 0, len);
} }
os.flush(); os.flush();
if (os instanceof FileOutputStream) {
((FileOutputStream) os).getFD().sync();
}
res = true; res = true;
}catch(Exception ex) {
ex.printStackTrace();
} finally { } finally {
closeSilently(os);
closeSilently(is);
} }
Log.d("downloadAPK", "download complete url=" + urlString + ", fileSize= " + temp.length()); // Log.d("Download", "download complete url=" + urlString + ", fileSize= " + temp.length());
// installPkg(this, temp, pkg);
} catch (Exception e) { } catch (Exception e) {
Log.w("downloadAPK", e); // Log.w("downloadAPK", e);
if (downloadFile != null) if (downloadFile != null)
downloadFile.delete(); downloadFile.delete();
e.printStackTrace();
} finally { } finally {
closeSilently(os);
closeSilently(is);
if (connection != null) if (connection != null)
connection.disconnect(); connection.disconnect();
} }
@ -72,8 +81,6 @@ public class FileDownloader {
return res; return res;
} }
public static final void closeSilently(Object closeable) { public static final void closeSilently(Object closeable) {
try { try {
if (closeable != null) { if (closeable != null) {

@ -0,0 +1,42 @@
package com.xypower.common;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class FileUtils {
public static boolean DeleteFilesInPath(String path, long seconds) {
File pathFile = new File(path);
if (!pathFile.exists() || !pathFile.isDirectory()) {
return false;
}
long ts = System.currentTimeMillis();
long diff = 0;
seconds *= 1000;
List<File> files = new ArrayList<File>();
for (File f : pathFile.listFiles()) {
if (!f.isFile()) {
continue;
}
diff = ts - f.lastModified();
if (diff > seconds) {
files.add(f);
}
}
for (File f : files) {
f.delete();
}
return true;
}
}
Loading…
Cancel
Save