1 package org.openintents.filemanager.util;
\r
3 import android.app.ProgressDialog;
\r
4 import android.content.Intent;
\r
5 import android.os.AsyncTask;
\r
6 import android.util.Log;
\r
7 import android.widget.Toast;
\r
8 import org.openintents.filemanager.FileManagerActivity;
\r
9 import org.openintents.filemanager.R;
\r
10 import org.openintents.intents.FileManagerIntents;
\r
13 import java.util.ArrayList;
\r
14 import java.util.List;
\r
15 import java.util.zip.ZipEntry;
\r
16 import java.util.zip.ZipOutputStream;
\r
18 public class CompressManager {
\r
20 * TAG for log messages.
\r
22 static final String TAG = "CompressManager";
\r
24 private static final int BUFFER_SIZE = 1024;
\r
25 private FileManagerActivity activity;
\r
26 private ProgressDialog progressDialog;
\r
27 private int fileCount;
\r
28 private String fileOut;
\r
30 public CompressManager(FileManagerActivity activity) {
\r
31 this.activity = activity;
\r
34 public void compress(File f, String out) {
\r
35 List <File>list = new ArrayList<File>();
\r
37 compress(list, out);
\r
40 public void compress(List<File> list, String out) {
\r
41 if (list.isEmpty()){
\r
42 Log.v(TAG, "couldn't compress empty file list");
\r
45 this.fileOut = list.get(0).getParent()+File.separator+out;
\r
48 fileCount += FileUtils.getFileCount(f);
\r
50 new CompressTask().execute(list);
\r
53 private class CompressTask extends AsyncTask<Object, Void, Integer> {
\r
54 private static final int success = 0;
\r
55 private static final int error = 1;
\r
56 private ZipOutputStream zos;
\r
59 * count of compressed file to update the progress bar
\r
61 private int isCompressed = 0;
\r
64 * Recursively compress file or directory
\r
65 * @returns 0 if successful, error value otherwise.
\r
67 private void compressFile(File file, String path) throws IOException {
\r
68 if (!file.isDirectory()){
\r
69 byte[] buf = new byte[BUFFER_SIZE];
\r
71 FileInputStream in = new FileInputStream(file);
\r
72 zos.putNextEntry(new ZipEntry(path + "/" + file.getName()));
\r
73 while ((len = in.read(buf)) > 0) {
\r
74 zos.write(buf, 0, len);
\r
79 if (file.list() == null){
\r
82 for (String fileName: file.list()){
\r
83 File f = new File(file.getAbsolutePath()+File.separator+fileName);
\r
84 compressFile(f, path + File.separator + file.getName());
\r
86 progressDialog.setProgress((isCompressed * 100)/ fileCount);
\r
91 protected void onPreExecute() {
\r
92 FileOutputStream out = null;
\r
93 progressDialog = new ProgressDialog(activity);
\r
94 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
\r
95 progressDialog.setMessage(activity.getResources().getString(R.string.compressing));
\r
96 progressDialog.show();
\r
97 progressDialog.setProgress(0);
\r
99 out = new FileOutputStream(new File(fileOut));
\r
100 zos = new ZipOutputStream(new BufferedOutputStream(out));
\r
101 } catch (FileNotFoundException e) {
\r
102 Log.e(TAG, "error while creating ZipOutputStream");
\r
107 protected Integer doInBackground(Object... params) {
\r
111 List<File> list = (List<File>) params[0];
\r
112 for (File file:list){
\r
114 compressFile(file, "");
\r
115 } catch (IOException e) {
\r
116 Log.e(TAG, "Error while compressing", e);
\r
124 protected void onPostExecute(Integer result) {
\r
128 } catch (IOException e) {
\r
129 Log.e(TAG, "error while closing zos", e);
\r
131 progressDialog.cancel();
\r
132 if (result == error){
\r
133 Toast.makeText(activity, R.string.compressing_error, Toast.LENGTH_SHORT).show();
\r
134 } else if (result == success){
\r
135 Toast.makeText(activity, R.string.compressing_success, Toast.LENGTH_SHORT).show();
\r
138 if (activity.getIntent().getAction().equals(FileManagerIntents.ACTION_MULTI_SELECT)){
\r
139 Intent intent = activity.getIntent();
\r
140 activity.setResult(activity.RESULT_OK, intent);
\r
143 activity.refreshList();
\r