Android Open Source - MaterialManager Zipper






From Project

Back to project page MaterialManager.

License

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.

Java Source Code

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();
    }
}




Java Source Code List

com.afollestad.cabinet.App.java
com.afollestad.cabinet.ApplicationTest.java
com.afollestad.cabinet.adapters.FileAdapter.java
com.afollestad.cabinet.adapters.NavigationDrawerAdapter.java
com.afollestad.cabinet.cab.CopyCab.java
com.afollestad.cabinet.cab.CutCab.java
com.afollestad.cabinet.cab.MainCab.java
com.afollestad.cabinet.cab.PickerCab.java
com.afollestad.cabinet.cab.base.BaseCab.java
com.afollestad.cabinet.cab.base.BaseFileCab.java
com.afollestad.cabinet.comparators.AlphabeticalComparator.java
com.afollestad.cabinet.comparators.ExtensionComparator.java
com.afollestad.cabinet.comparators.FoldersFirstComparator.java
com.afollestad.cabinet.comparators.HighLowSizeComparator.java
com.afollestad.cabinet.comparators.LastModifiedComparator.java
com.afollestad.cabinet.comparators.LowHighSizeComparator.java
com.afollestad.cabinet.file.CloudFile.java
com.afollestad.cabinet.file.LocalFile.java
com.afollestad.cabinet.file.Remote.java
com.afollestad.cabinet.file.base.FileFilter.java
com.afollestad.cabinet.file.base.File.java
com.afollestad.cabinet.file.root.LsParser.java
com.afollestad.cabinet.file.root.LsTokenizer.java
com.afollestad.cabinet.file.root.RootFile.java
com.afollestad.cabinet.fragments.AboutDialog.java
com.afollestad.cabinet.fragments.DetailsDialog.java
com.afollestad.cabinet.fragments.DirectoryFragment.java
com.afollestad.cabinet.fragments.NavigationDrawerFragment.java
com.afollestad.cabinet.fragments.RemoteConnectionDialog.java
com.afollestad.cabinet.fragments.WelcomeFragment.java
com.afollestad.cabinet.services.NetworkService.java
com.afollestad.cabinet.sftp.FileNotExistsException.java
com.afollestad.cabinet.sftp.SftpClient.java
com.afollestad.cabinet.ui.DrawerActivity.java
com.afollestad.cabinet.ui.SettingsActivity.java
com.afollestad.cabinet.ui.TextEditor.java
com.afollestad.cabinet.ui.base.NetworkedActivity.java
com.afollestad.cabinet.ui.base.ThemableActivity.java
com.afollestad.cabinet.utils.APKIconDownloader.java
com.afollestad.cabinet.utils.PauseOnScrollListener.java
com.afollestad.cabinet.utils.Perm.java
com.afollestad.cabinet.utils.Pins.java
com.afollestad.cabinet.utils.StorageHelper.java
com.afollestad.cabinet.utils.ThemeUtils.java
com.afollestad.cabinet.utils.TimeUtils.java
com.afollestad.cabinet.utils.Utils.java
com.afollestad.cabinet.zip.Unzipper.java
com.afollestad.cabinet.zip.Zipper.java
eu.chainfire.libsuperuser.ApplicationTest.java
eu.chainfire.libsuperuser.Application.java
eu.chainfire.libsuperuser.Debug.java
eu.chainfire.libsuperuser.HideOverlaysReceiver.java
eu.chainfire.libsuperuser.ShellNotClosedException.java
eu.chainfire.libsuperuser.ShellOnMainThreadException.java
eu.chainfire.libsuperuser.Shell.java
eu.chainfire.libsuperuser.StreamGobbler.java