--- /dev/null
+package org.openintents.filemanager.util;\r
+\r
+import android.app.ProgressDialog;\r
+import android.content.Intent;\r
+import android.os.AsyncTask;\r
+import android.util.Log;\r
+import android.widget.Toast;\r
+import org.openintents.filemanager.FileManagerActivity;\r
+import org.openintents.filemanager.R;\r
+import org.openintents.intents.FileManagerIntents;\r
+\r
+import java.io.*;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.zip.ZipEntry;\r
+import java.util.zip.ZipOutputStream;\r
+\r
+public class CompressManager {\r
+ /**\r
+ * TAG for log messages.\r
+ */\r
+ static final String TAG = "CompressManager";\r
+\r
+ private static final int BUFFER_SIZE = 1024;\r
+ private FileManagerActivity activity;\r
+ private ProgressDialog progressDialog;\r
+ private int fileCount;\r
+ private String fileOut;\r
+\r
+ public CompressManager(FileManagerActivity activity) {\r
+ this.activity = activity;\r
+ }\r
+\r
+ public void compress(File f, String out) {\r
+ List <File>list = new ArrayList<File>();\r
+ list.add(f);\r
+ compress(list, out);\r
+ } \r
+\r
+ public void compress(List<File> list, String out) {\r
+ if (list.isEmpty()){\r
+ Log.v(TAG, "couldn't compress empty file list");\r
+ return;\r
+ }\r
+ this.fileOut = list.get(0).getParent()+File.separator+out;\r
+ fileCount=0;\r
+ for (File f: list){\r
+ fileCount += FileUtils.getFileCount(f);\r
+ }\r
+ new CompressTask().execute(list);\r
+ }\r
+\r
+ private class CompressTask extends AsyncTask<Object, Void, Integer> {\r
+ private static final int success = 0;\r
+ private static final int error = 1;\r
+ private ZipOutputStream zos;\r
+\r
+ /**\r
+ * count of compressed file to update the progress bar\r
+ */\r
+ private int isCompressed = 0;\r
+\r
+ /**\r
+ * Recursively compress file or directory\r
+ * @returns 0 if successful, error value otherwise.\r
+ */\r
+ private void compressFile(File file, String path) throws IOException {\r
+ if (!file.isDirectory()){\r
+ byte[] buf = new byte[BUFFER_SIZE];\r
+ int len;\r
+ FileInputStream in = new FileInputStream(file);\r
+ zos.putNextEntry(new ZipEntry(path + "/" + file.getName()));\r
+ while ((len = in.read(buf)) > 0) {\r
+ zos.write(buf, 0, len);\r
+ }\r
+ in.close();\r
+ return;\r
+ }\r
+ if (file.list() == null){\r
+ return;\r
+ }\r
+ for (String fileName: file.list()){\r
+ File f = new File(file.getAbsolutePath()+File.separator+fileName);\r
+ compressFile(f, path + File.separator + file.getName());\r
+ isCompressed++;\r
+ progressDialog.setProgress((isCompressed * 100)/ fileCount);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ protected void onPreExecute() {\r
+ FileOutputStream out = null;\r
+ progressDialog = new ProgressDialog(activity);\r
+ progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);\r
+ progressDialog.setMessage(activity.getResources().getString(R.string.compressing));\r
+ progressDialog.show();\r
+ progressDialog.setProgress(0);\r
+ try {\r
+ out = new FileOutputStream(new File(fileOut));\r
+ zos = new ZipOutputStream(new BufferedOutputStream(out));\r
+ } catch (FileNotFoundException e) {\r
+ Log.e(TAG, "error while creating ZipOutputStream");\r
+ }\r
+ }\r
+\r
+ @Override\r
+ protected Integer doInBackground(Object... params) {\r
+ if (zos == null){\r
+ return error;\r
+ }\r
+ List<File> list = (List<File>) params[0]; \r
+ for (File file:list){\r
+ try {\r
+ compressFile(file, "");\r
+ } catch (IOException e) {\r
+ Log.e(TAG, "Error while compressing", e);\r
+ return error;\r
+ }\r
+ }\r
+ return success;\r
+ }\r
+\r
+ @Override\r
+ protected void onPostExecute(Integer result) {\r
+ try {\r
+ zos.flush();\r
+ zos.close();\r
+ } catch (IOException e) {\r
+ Log.e(TAG, "error while closing zos", e);\r
+ }\r
+ progressDialog.cancel();\r
+ if (result == error){\r
+ Toast.makeText(activity, R.string.compressing_error, Toast.LENGTH_SHORT).show();\r
+ } else if (result == success){\r
+ Toast.makeText(activity, R.string.compressing_success, Toast.LENGTH_SHORT).show();\r
+ }\r
+\r
+ if (activity.getIntent().getAction().equals(FileManagerIntents.ACTION_MULTI_SELECT)){\r
+ Intent intent = activity.getIntent();\r
+ activity.setResult(activity.RESULT_OK, intent);\r
+ activity.finish();\r
+ } else {\r
+ activity.refreshList();\r
+ }\r
+ }\r
+ }\r
+}\r