diff --git a/common/src/main/java/com/xypower/common/FileDownloader.java b/common/src/main/java/com/xypower/common/FileDownloader.java index d781fe14..cda9054e 100644 --- a/common/src/main/java/com/xypower/common/FileDownloader.java +++ b/common/src/main/java/com/xypower/common/FileDownloader.java @@ -22,17 +22,19 @@ public class FileDownloader { return false; HttpURLConnection connection = null; GZIPInputStream gZIPInputStream = null; + InputStream is = null; + OutputStream os = null; boolean res = false; + try { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); URL url = new URL(urlString); connection = (HttpURLConnection) url.openConnection(); - // connection.setRequestProperty("Accept-Encoding", "gzip"); + connection.setRequestProperty("Accept-Encoding", "gzip"); connection.setConnectTimeout(5000); connection.setReadTimeout(60000); connection.setDoInput(true); - String encoding = connection.getContentEncoding(); - InputStream is = connection.getInputStream(); + connection.connect(); final File temp = new File(filePath); if (temp.exists()) temp.delete(); @@ -41,30 +43,37 @@ public class FileDownloader { temp.setWritable(true, false); downloadFile = 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]; + + if(encoding.equals("gzip")) { + gZIPInputStream = new GZIPInputStream(is); + is = gZIPInputStream; + } int len; try { while ((len = is.read(buf)) != -1) { os.write(buf, 0, len); } os.flush(); - if (os instanceof FileOutputStream) { - ((FileOutputStream) os).getFD().sync(); - } res = true; + }catch(Exception ex) { + ex.printStackTrace(); } finally { - closeSilently(os); - closeSilently(is); } - Log.d("downloadAPK", "download complete url=" + urlString + ", fileSize= " + temp.length()); -// installPkg(this, temp, pkg); + // Log.d("Download", "download complete url=" + urlString + ", fileSize= " + temp.length()); } catch (Exception e) { - Log.w("downloadAPK", e); + // Log.w("downloadAPK", e); if (downloadFile != null) downloadFile.delete(); - + e.printStackTrace(); } finally { + closeSilently(os); + closeSilently(is); if (connection != null) connection.disconnect(); } @@ -72,8 +81,6 @@ public class FileDownloader { return res; } - - public static final void closeSilently(Object closeable) { try { if (closeable != null) { diff --git a/common/src/main/java/com/xypower/common/FileUtils.java b/common/src/main/java/com/xypower/common/FileUtils.java new file mode 100644 index 00000000..6664ce0a --- /dev/null +++ b/common/src/main/java/com/xypower/common/FileUtils.java @@ -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 files = new ArrayList(); + + 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; + } + +}