Zip4J使用

使用Java的ZipOutputStream压缩和解压缩文件,代码写起来太繁琐了,从网上找到Zip4J可以很方便的操作zip压缩包,同时可以给压缩包设置解压密码,使用起来很方便。

我使用gradle管理工程,在build.gradle文件添加如下依赖:

// https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j
compile group: 'net.lingala.zip4j', name: 'zip4j', version: '1.3.2'

示例代码:

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import java.io.File;

/**
 * Created by wzj on 2017/11/27.
 */
public class ZipUtil
{
    public static void main(String[] args)
    {
        try
        {
            //zip("D:\\资料", "D:\\资料.zip", "123");
            unZip("D:\\资料.zip","D:\\资料","123");
        }
        catch (ZipException e)
        {
            e.printStackTrace();
        }
     }

    /**
     * 压缩
     *
     * @param srcFile 源目录
     * @param dest    要压缩的目录
     * @param passwd  密码 不是必填
     * @throws ZipException 异常
     */
    public static void zip(String srcFile, String dest, String passwd) throws ZipException
    {
        File srcfile = new File(srcFile);

        //创建目标文件
        String destname = buildDestFileName(srcfile, dest);
        ZipParameters par = new ZipParameters();
        par.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        par.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

        if (passwd != null)
        {
            par.setEncryptFiles(true);
            par.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
            par.setPassword(passwd.toCharArray());
        }

        ZipFile zipfile = new ZipFile(destname);
        if (srcfile.isDirectory())
        {
            zipfile.addFolder(srcfile, par);
        }
        else
        {
            zipfile.addFile(srcfile, par);
        }
    }

    /**
     * 解压
     * @param zipfile 压缩包文件
     * @param dest 目标文件
     * @param passwd 密码
     * @throws ZipException 抛出异常
     */
    public static void unZip(String zipfile, String dest, String passwd) throws ZipException
    {
        ZipFile zfile = new ZipFile(zipfile);
        zfile.setFileNameCharset("UTF-8");//在GBK系统中需要设置
        if (!zfile.isValidZipFile())
        {
            throw new ZipException("压缩文件不合法,可能已经损坏!");
        }

        File file = new File(dest);
        if (file.isDirectory() && !file.exists())
        {
            file.mkdirs();
        }

        if (zfile.isEncrypted())
        {
            zfile.setPassword(passwd.toCharArray());
        }
        zfile.extractAll(dest);
    }

    public static String buildDestFileName(File srcfile, String dest)
    {
        if (dest == null)
        {
            if (srcfile.isDirectory())
            {
                dest = srcfile.getParent() + File.separator + srcfile.getName() + ".zip";
            }
            else
            {
                String filename = srcfile.getName().substring(0, srcfile.getName().lastIndexOf("."));
                dest = srcfile.getParent() + File.separator + filename + ".zip";
            }
        }
        else
        {
            createPath(dest);//路径的创建
            if (dest.endsWith(File.separator))
            {
                String filename = "";
                if (srcfile.isDirectory())
                {
                    filename = srcfile.getName();
                }
                else
                {
                    filename = srcfile.getName().substring(0, srcfile.getName().lastIndexOf("."));
                }
                dest += filename + ".zip";
            }
        }
        return dest;
    }

    private static void createPath(String dest)
    {
        File destDir = null;
        if (dest.endsWith(File.separator))
        {
            destDir = new File(dest);//给出的是路径时
        }
        else
        {
            destDir = new File(dest.substring(0, dest.lastIndexOf(File.separator)));
        }

        if (!destDir.exists())
        {
            destDir.mkdirs();
        }
    }
}