1 package org.openintents.filemanager.util;
\r
3 import android.app.ProgressDialog;
\r
4 import android.os.AsyncTask;
\r
5 import android.util.Log;
\r
6 import android.widget.Toast;
\r
7 import org.openintents.filemanager.FileManagerActivity;
\r
8 import org.openintents.filemanager.R;
\r
11 import java.util.Enumeration;
\r
12 import java.util.zip.ZipEntry;
\r
13 import java.util.zip.ZipFile;
\r
15 public class ExtractManager {
\r
17 * TAG for log messages.
\r
19 static final String TAG = "ExtractManager";
\r
21 private static final int BUFFER_SIZE = 1024;
\r
22 private FileManagerActivity activity;
\r
23 private ProgressDialog progressDialog;
\r
25 public ExtractManager(FileManagerActivity activity) {
\r
26 this.activity = activity;
\r
29 public void extract(File f, String destinationPath) {
\r
30 new ExtractTask().execute(f, destinationPath);
\r
33 private class ExtractTask extends AsyncTask<Object, Void, Integer> {
\r
34 private static final int success = 0;
\r
35 private static final int error = 1;
\r
38 * count of extracted files to update the progress bar
\r
40 private int isExtracted = 0;
\r
43 * Recursively extract file or directory
\r
45 public boolean extract(File archive, String destinationPath) {
\r
47 ZipFile zipfile = new ZipFile(archive);
\r
48 int fileCount = zipfile.size();
\r
49 for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
\r
50 ZipEntry entry = (ZipEntry) e.nextElement();
\r
51 unzipEntry(zipfile, entry, destinationPath);
\r
53 progressDialog.setProgress((isExtracted * 100)/ fileCount);
\r
56 } catch (Exception e) {
\r
57 Log.e(TAG, "Error while extracting file " + archive, e);
\r
62 private void createDir(File dir) {
\r
66 Log.i(TAG, "Creating dir " + dir.getName());
\r
67 if (!dir.mkdirs()) {
\r
68 throw new RuntimeException("Can not create dir " + dir);
\r
72 private void unzipEntry(ZipFile zipfile, ZipEntry entry,
\r
73 String outputDir) throws IOException {
\r
74 if (entry.isDirectory()) {
\r
75 createDir(new File(outputDir, entry.getName()));
\r
78 File outputFile = new File(outputDir, entry.getName());
\r
79 if (!outputFile.getParentFile().exists()) {
\r
80 createDir(outputFile.getParentFile());
\r
82 Log.i(TAG, "Extracting: " + entry);
\r
83 BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
\r
84 BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
\r
87 byte buf[] = new byte[BUFFER_SIZE];
\r
88 while ((len = inputStream.read(buf)) > 0) {
\r
89 outputStream.write(buf, 0, len);
\r
92 outputStream.close();
\r
93 inputStream.close();
\r
98 protected void onPreExecute() {
\r
99 progressDialog = new ProgressDialog(activity);
\r
100 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
\r
101 progressDialog.setMessage(activity.getResources().getString(R.string.extracting));
\r
102 progressDialog.show();
\r
103 progressDialog.setProgress(0);
\r
108 protected Integer doInBackground(Object... params) {
\r
109 File f= (File) params[0];
\r
110 String destination = (String) params[1];
\r
111 boolean result = extract(f, destination);
\r
112 return result ? success : error;
\r
116 protected void onPostExecute(Integer result) {
\r
117 progressDialog.cancel();
\r
118 if (result == error){
\r
119 Toast.makeText(activity, R.string.extracting_error, Toast.LENGTH_SHORT).show();
\r
120 } else if (result == success){
\r
121 Toast.makeText(activity, R.string.extracting_success, Toast.LENGTH_SHORT).show();
\r
123 activity.refreshList();
\r