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




使用zip4j库可以创建、读取和修改ZIP文件,但是它不支持修改压缩后的文件后缀名。 ZIP文件格式是一种归档文件格式,其中包含多个文件和目录,但不会保留文件的扩展名。因此,更改ZIP文件中的文件扩展名不会更改解压缩后的文件的扩展名。 如果要更改ZIP文件中文件的扩展名,您可以将文件解压缩到临时文件夹中,修改文件名,然后使用zip4j库重新压缩文件。 以下是Java使用zip4j库进行ZIP文件解压缩和压缩的示例代码: ```java import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.FileHeader; import net.lingala.zip4j.model.ZipParameters; import java.io.File; import java.util.List; public class Zip4jExample { public static void main(String[] args) throws ZipException { // 解压缩ZIP文件 ZipFile zipFile = new ZipFile("example.zip"); String destFolder = "unzip/"; if (!zipFile.isValidZipFile()) { throw new ZipException("Invalid ZIP file"); } zipFile.extractAll(destFolder); // 修改文件名 File fileToRename = new File(destFolder + "example.txt"); File newFile = new File(destFolder + "example2.csv"); boolean renamed = fileToRename.renameTo(newFile); if (!renamed) { throw new RuntimeException("Failed to rename file"); } // 压缩文件ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); zipFile = new ZipFile("example2.zip"); zipFile.addFolder(destFolder, parameters); } } ``` 请注意,以上代码只是示例,并不会处理错误处理和异常情况。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值