Browse files

fix bug in zip algorithm. replace zipinputstream with zipfile so much…

… faster now. fixed percentages to take into account unzipping.
  • Loading branch information...
1 parent e9ac214 commit a26c9b9ebda682ded12b2697c647f57fed9e2c06 @ahmedre ahmedre committed Feb 27, 2012
View
13 src/com/quran/labs/androidquran/common/InternetActivity.java
@@ -246,6 +246,19 @@ protected void onPause(){
super.onPause();
}
+ @Override
+ protected void onDestroy(){
+ try {
+ if (pDialog != null)
+ pDialog.dismiss();
+ }
+ catch (Exception e){
+ android.util.Log.d("InternetActivity", "exception while removing dialog: " + e);
+ }
+
+ super.onDestroy();
+ }
+
private void showProgressDialog(){
if (hideProgressBar)
return;
View
149 src/com/quran/labs/androidquran/service/QuranDataService.java
@@ -3,16 +3,17 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
+import java.util.Enumeration;
import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
+import java.util.zip.ZipFile;
import android.app.Notification;
import android.app.NotificationManager;
@@ -342,63 +343,84 @@ private boolean resumeDownload() {
try {
for (; downloadIndex < fileNames.length; downloadIndex++) {
int downloaded = 0;
+ boolean skipDownload = false;
+ boolean shouldRename = true;
File f = new File(saveToDirectories[downloadIndex]);
f.mkdirs();
+
+ // try to open the actual file without .part in case
+ // it has already finished downloading.
File file = new File(saveToDirectories[downloadIndex],
- fileNames[downloadIndex] + DOWNLOAD_EXT);
+ fileNames[downloadIndex]);
URL url = new URL(downloadUrls[downloadIndex]);
URLConnection conn = url.openConnection();
fileLength = conn.getContentLength();
- if(!isSpaceAvailable()){
- Log.e("quran_srv", "Not enough space on SD card");
- return false;
+ if (file.exists() && (fileLength == (int)file.length())){
+ skipDownload = true;
+ shouldRename = false;
+ }
+ else {
+ // try to see if the filename with .part exists
+ file = new File(saveToDirectories[downloadIndex],
+ fileNames[downloadIndex] + DOWNLOAD_EXT);
+ if (file.exists()) {
+ downloaded = (int) file.length();
+ Log.d("quran_srv", "Resuming from " + downloaded);
+ if (downloaded == fileLength)
+ skipDownload = true;
+ }
}
- Log.d("quran_srv", "File to download: " + file.getName()
- + " - total length: " + fileLength);
- HttpURLConnection connection = (HttpURLConnection) url
+
+ if (!skipDownload){
+ if(!isSpaceAvailable()){
+ Log.e("quran_srv", "Not enough space on SD card");
+ return false;
+ }
+ Log.d("quran_srv", "File to download: " + file.getName()
+ + " - total length: " + fileLength);
+
+ HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
- if (file.exists()) {
- downloaded = (int) file.length();
- connection.setRequestProperty("Range",
- "bytes=" + (file.length()) + "-");
- Log.d("quran_srv", "Resuming from " + downloaded);
- if (downloaded == fileLength)
- continue;
+ connection.setRequestProperty("Range", "bytes="
+ + downloaded + "-");
+ connection.setDoInput(true);
+ in = new BufferedInputStream(connection.getInputStream(),
+ DOWNLOAD_BUFFER_SIZE);
+ fos = (downloaded == 0) ? new FileOutputStream(
+ file.getAbsolutePath()) : new FileOutputStream(
+ file.getAbsolutePath(), true);
+
+ bout = new BufferedOutputStream(fos, DOWNLOAD_BUFFER_SIZE);
+ byte[] data = new byte[DOWNLOAD_BUFFER_SIZE];
+ int x = 0;
+
+ while (isRunning
+ && (x = in.read(data, 0, DOWNLOAD_BUFFER_SIZE)) >= 0) {
+ bout.write(data, 0, x);
+ downloaded += x;
+ double percent = 100.0 * ((1.0 * downloaded) / (1.0 * fileLength));
+ updateProgress((int) percent, fileNames.length,
+ downloadIndex, false);
+ }
+ bout.flush();
+ bout.close();
+ fos.close();
}
- connection.setRequestProperty("Range", "bytes="
- + downloaded + "-");
- connection.setDoInput(true);
- in = new BufferedInputStream(connection.getInputStream(),
- DOWNLOAD_BUFFER_SIZE);
- fos = (downloaded == 0) ? new FileOutputStream(
- file.getAbsolutePath()) : new FileOutputStream(
- file.getAbsolutePath(), true);
-
- bout = new BufferedOutputStream(fos, DOWNLOAD_BUFFER_SIZE);
- byte[] data = new byte[DOWNLOAD_BUFFER_SIZE];
- int x = 0;
-
- while (isRunning
- && (x = in.read(data, 0, DOWNLOAD_BUFFER_SIZE)) >= 0) {
- bout.write(data, 0, x);
- downloaded += x;
- double percent = 100.0 * ((1.0 * downloaded) / (1.0 * fileLength));
- updateProgress((int) percent, fileNames.length,
- downloadIndex);
- }
- bout.flush();
- bout.close();
- fos.close();
if (isRunning) {
- file.renameTo(new File(
+ updateProgress(100, fileNames.length, downloadIndex, false);
+ if (shouldRename){
+ file.renameTo(new File(
saveToDirectories[downloadIndex],
fileNames[downloadIndex]));
+ }
if (zipped || fileNames[downloadIndex].endsWith(".zip"))
unzipFile(saveToDirectories[downloadIndex],
- fileNames[downloadIndex]);
+ fileNames[downloadIndex],
+ fileNames.length, downloadIndex);
+ updateProgress(100, fileNames.length, downloadIndex, true);
Log.d("quran_srv", "Download Completed ["
+ downloadUrls[downloadIndex] + "]");
} else
@@ -424,9 +446,14 @@ private boolean isSpaceAvailable(){
}
private void updateProgress(int percent, int totalFiles,
- int nDownloadedFiles) {
- percent = (int) (((double) percent / (double) 100 + (double) nDownloadedFiles)
- / (double) totalFiles * 100);
+ int nDownloadedFiles, boolean zipPortion) {
+ //android.util.Log.d("quran_srv", "called updateProgress with " + percent + ", " + totalFiles + ", "
+ // + nDownloadedFiles + ", " + zipPortion);
+ int totalPercent = totalFiles * 100;
+ int donePercent = nDownloadedFiles * 100;
+ int thisFileDone = (int)(1.0 * percent / 2.0) + (zipPortion? 50 : 0);
+ donePercent = donePercent + thisFileDone;
+ percent = (int)((100.0 * donePercent) / (1.0 * totalPercent));
service.updateProgress(percent);
// notification.contentView.setTextViewText(R.id.text,
// "Downloading.. " + percent + "%");
@@ -468,38 +495,48 @@ public void run() {
thread = null;
}
- protected void unzipFile(String saveToDirectory, String fileName) {
+ protected void unzipFile(String saveToDirectory, String fileName,
+ int totalFiles, int downloadedFiles) {
try {
Log.d("quran_srv", "Unziping file: " + saveToDirectory
+ fileName);
// success, unzip the file...
File file = new File(saveToDirectory, fileName);
- FileInputStream is = new FileInputStream(file);
- ZipInputStream zis = new ZipInputStream(is);
-
- ZipEntry entry;
- while ((entry = zis.getNextEntry()) != null) {
+ ZipFile zip = new ZipFile(file, ZipFile.OPEN_READ);
+ int numberOfFiles = zip.size();
+ Enumeration<? extends ZipEntry> entries = zip.entries();
+
+ int processedFiles = 0;
+ while (entries.hasMoreElements()) {
+ processedFiles++;
+ ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
- zis.closeEntry();
+ File f = new File(saveToDirectory, entry.getName());
+ if (!f.exists()){
+ f.mkdirs();
+ }
continue;
}
// ignore files that already exist
File f = new File(saveToDirectory, entry.getName());
if (!f.exists()) {
+ InputStream is = zip.getInputStream(entry);
FileOutputStream ostream = new FileOutputStream(f);
int size;
byte[] buf = new byte[DOWNLOAD_BUFFER_SIZE];
- while ((size = zis.read(buf)) > 0)
+ while ((size = is.read(buf)) > 0)
ostream.write(buf, 0, size);
+ is.close();
ostream.close();
}
- zis.closeEntry();
+
+ updateProgress((int)((100.0 * processedFiles) / (1.0 * numberOfFiles)),
+ totalFiles, downloadedFiles, true);
}
- zis.close();
- is.close();
+ zip.close();
file.delete();
Log.d("quran_srv", "file unzipped successfully");

0 comments on commit a26c9b9

Please sign in to comment.