Java學習之使用net.lingala.zip4j.core.ZipFile解壓縮文件,帶解壓縮進度


點擊下載 net.lingala.zip4j.core.ZipFile

import java.io.File;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.progress.ProgressMonitor;

public class Zip4jTest001 {

    /**
     * 進度接口
     *
     * @author lzy
     *
     */
    public interface ProgressListener {
        void onStart();

        void onProgress(long progress);

        void onError(Exception e);

        void onCompleted();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            unZipFileWithProgress("C:\\Users\\lzy\\Desktop\\www.zip", "C:\\Users\\lzy\\Desktop\\www", "123456",
                    new Zip4jTest001.ProgressListener() {

                        @Override
                        public void onStart() {
                            System.out.println("--onStart--");
                        }

                        @Override
                        public void onProgress(long progress) {
                            System.out.println("--onProgress--" + progress);
                        }

                        @Override
                        public void onError(Exception e) {
                            System.out.println("--onCompleted--" + e.getMessage());
                        }

                        @Override
                        public void onCompleted() {
                            System.out.println("--onCompleted--");
                        }
                    }, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 解壓縮zip,帶解壓進度
     *
     * @param zipFilePath
     * @param filePath
     * @param password
     * @param listener
     * @param isDeleteZip
     * @throws ZipException
     */
    public static void unZipFileWithProgress(final String zipFilePath, final String filePath, final String password,
                                             final ProgressListener listener, final boolean isDeleteZip) throws ZipException {

        final File zipFile = new File(zipFilePath);

        ZipFile zFile = new ZipFile(zipFile);
        zFile.setFileNameCharset("utf-8");

        if (!zFile.isValidZipFile()) { //
            throw new ZipException("exception!");
        }
        File destDir = new File(filePath);
        if (destDir.isDirectory() && !destDir.exists()) {
            destDir.mkdir();
        }
        if (zFile.isEncrypted()) {
            zFile.setPassword(password); // 設置解壓密碼
        }

        final ProgressMonitor progressMonitor = zFile.getProgressMonitor();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    int precentDone = 0;
                    if (listener == null) {
                        return;
                    }
                    listener.onStart();
                    while (true && !progressMonitor.isCancelAllTasks()) {
                        // 每隔50ms,發送一個解壓進度出去
                        Thread.sleep(50);
                        precentDone = progressMonitor.getPercentDone();
                        listener.onProgress(precentDone);
                        if (precentDone >= 100) {
                            break;
                        }
                    }
                    listener.onCompleted();
                } catch (InterruptedException e) {
                    listener.onError(e);
                } finally {
                    if (isDeleteZip) {
                        zipFile.delete();// 將原壓縮文件刪除
                    }
                }
            }
        });
        thread.start();

        zFile.setRunInThread(false); // true 在子線程中進行解壓 , false主線程中解壓
        try {
            zFile.extractAll(filePath); // 將壓縮文件解壓到filePath...
        } catch (Exception e) {
            progressMonitor.cancelAllTasks();
            listener.onError(e);
        }
    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章