Back to project page MaterialManager.
The source code is released under:
There is no license, someone decided to pretty much republish Cabinet with no credit so I?m taking away the license altogether.
If you think the Android project MaterialManager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.afollestad.cabinet.zip; /*from w w w . j a v a 2 s.c om*/ import android.app.ProgressDialog; import android.util.Log; import com.afollestad.cabinet.R; import com.afollestad.cabinet.file.LocalFile; import com.afollestad.cabinet.file.base.File; import com.afollestad.cabinet.fragments.DirectoryFragment; import com.afollestad.cabinet.utils.Utils; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Zipper { private static ProgressDialog mDialog; public static void zip(final DirectoryFragment context, final List<File> files, final ZipCallback callback) { LocalFile dest = new LocalFile(context.getActivity(), context.getDirectory(), files.get(0).getNameNoExtension() + ".zip"); Utils.checkDuplicates(context.getActivity(), dest, new Utils.DuplicateCheckResult() { @Override public void onResult(final File dest) { mDialog = new ProgressDialog(context.getActivity()); mDialog.setTitle(context.getString(R.string.zipping)); mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mDialog.setMax(LocalFile.getFileCount(files)); mDialog.setCancelable(true); mDialog.show(); new Thread(new Runnable() { @Override public void run() { try { FileOutputStream fout = new FileOutputStream(dest.toJavaFile()); ZipOutputStream zout = new ZipOutputStream(fout); writeFiles("", zout, files); zout.close(); if (context.getActivity() == null) return; context.getActivity().runOnUiThread(new Runnable() { @Override public void run() { mDialog.dismiss(); context.reload(); if (callback != null) callback.onComplete(); } }); } catch (final Exception e) { e.printStackTrace(); try { if (dest.existsSync()) dest.deleteSync(); } catch (Exception e2) { e2.printStackTrace(); } if (context.getActivity() == null) return; context.getActivity().runOnUiThread(new Runnable() { @Override public void run() { if (mDialog != null) mDialog.dismiss(); Utils.showErrorDialog(context.getActivity(), R.string.failed_zip_files, e); } }); } } }).start(); } }); } private static void log(String message) { Log.v("Zipper", message); } private static void writeFiles(String currentDir, ZipOutputStream zout, List<File> files) throws Exception { if (mDialog == null || !mDialog.isShowing()) { // Cancelled return; } log("Writing " + files.size() + " files to " + currentDir); byte[] buffer = new byte[1024]; for (File fi : files) { if (mDialog == null || !mDialog.isShowing()) { // Cancelled break; } else if (fi.isDirectory()) { writeFiles(currentDir + "/" + fi.getName(), zout, fi.listFilesSync(true)); continue; } mDialog.setProgress(mDialog.getProgress() + 1); log(" >>> Writing: " + currentDir + "/" + fi.getName()); ZipEntry ze = new ZipEntry(currentDir + "/" + fi.getName()); ze.setSize(fi.length()); FileInputStream fin = new FileInputStream(fi.getPath()); zout.putNextEntry(ze); int length; while ((length = fin.read(buffer)) > 0) { zout.write(buffer, 0, length); } zout.closeEntry(); fin.close(); } } public static interface ZipCallback { public abstract void onComplete(); } }