811a5a4a |
1 | package org.openintents.filemanager.util;\r |
2 | \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 |
9 | \r |
10 | import java.io.*;\r |
11 | import java.util.Enumeration;\r |
12 | import java.util.zip.ZipEntry;\r |
13 | import java.util.zip.ZipFile;\r |
14 | \r |
15 | public class ExtractManager {\r |
16 | /**\r |
17 | * TAG for log messages.\r |
18 | */\r |
19 | static final String TAG = "ExtractManager";\r |
20 | \r |
21 | private static final int BUFFER_SIZE = 1024;\r |
22 | private FileManagerActivity activity;\r |
23 | private ProgressDialog progressDialog;\r |
24 | \r |
25 | public ExtractManager(FileManagerActivity activity) {\r |
26 | this.activity = activity;\r |
27 | }\r |
28 | \r |
29 | public void extract(File f, String destinationPath) {\r |
30 | new ExtractTask().execute(f, destinationPath);\r |
31 | }\r |
32 | \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 |
36 | \r |
37 | /**\r |
38 | * count of extracted files to update the progress bar\r |
39 | */\r |
40 | private int isExtracted = 0;\r |
41 | \r |
42 | /**\r |
43 | * Recursively extract file or directory\r |
44 | */\r |
45 | public boolean extract(File archive, String destinationPath) {\r |
46 | try {\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 |
52 | isExtracted++;\r |
53 | progressDialog.setProgress((isExtracted * 100)/ fileCount);\r |
54 | }\r |
55 | return true;\r |
56 | } catch (Exception e) {\r |
57 | Log.e(TAG, "Error while extracting file " + archive, e);\r |
58 | return false;\r |
59 | }\r |
60 | } \r |
61 | \r |
62 | private void createDir(File dir) {\r |
63 | if (dir.exists()) {\r |
64 | return;\r |
65 | }\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 |
69 | }\r |
70 | } \r |
71 | \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 |
76 | return;\r |
77 | }\r |
78 | File outputFile = new File(outputDir, entry.getName());\r |
79 | if (!outputFile.getParentFile().exists()) {\r |
80 | createDir(outputFile.getParentFile());\r |
81 | }\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 |
85 | try {\r |
86 | int len;\r |
87 | byte buf[] = new byte[BUFFER_SIZE];\r |
88 | while ((len = inputStream.read(buf)) > 0) {\r |
89 | outputStream.write(buf, 0, len);\r |
90 | }\r |
91 | } finally {\r |
92 | outputStream.close();\r |
93 | inputStream.close();\r |
94 | }\r |
95 | }\r |
96 | \r |
97 | @Override\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 |
104 | isExtracted = 0;\r |
105 | }\r |
106 | \r |
107 | @Override\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 |
113 | }\r |
114 | \r |
115 | @Override\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 |
122 | }\r |
123 | activity.refreshList();\r |
124 | }\r |
125 | }\r |
126 | }\r |