java实现zip压缩

这个Java程序实现了文件和目录的压缩与解压缩功能。`ZipUtils`类提供了`toZip`和`unZip`两个静态方法,分别用于将指定目录压缩成ZIP文件和将ZIP文件解压缩到目标目录。程序使用了`ZipOutputStream`和`ZipFile`进行压缩和解压缩操作。
摘要由CSDN通过智能技术生成

java实现zip压缩

import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;


public class ZipUtils {

    static final int BUFFER = 8192;

    /**
     * 压缩文件
     *
     * @param srcPath 被压缩的目录
     * @param dstPath 压缩后的zip包
     * @throws IOException
     */
    public static void toZip(String srcPath, String dstPath) throws IOException {
        File srcFile = new File(srcPath);
        File dstFile = new File(dstPath);
        if (!srcFile.exists()) {
            throw new FileNotFoundException(srcPath + "不存在!");
        }

        FileOutputStream out = null;
        ZipOutputStream zipOut = null;
        try {
            out = new FileOutputStream(dstFile);
            CheckedOutputStream cos = new CheckedOutputStream(out, new CRC32());
            zipOut = new ZipOutputStream(cos);
            String baseDir = "";
            toZip(srcFile, zipOut, baseDir);
        } finally {
            if (null != zipOut) {
                zipOut.close();
                out = null;
            }

            if (null != out) {
                out.close();
            }
        }
    }

    private static void toZip(File file, ZipOutputStream zipOut, String baseDir) throws IOException {
        if (file.isDirectory()) {
            toZipDir(file, zipOut, baseDir);
        } else {
            toZipFile(file, zipOut, baseDir);
        }
    }

    /**
     * 压缩一个目录
     */
    private static void toZipDir(File dir, ZipOutputStream zipOut, String baseDir) throws IOException {
        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            toZip(files[i], zipOut, baseDir + dir.getName() + "/");
        }
    }

    /**
     * 压缩一个文件
     */
    private static void toZipFile(File file, ZipOutputStream zipOut, String baseDir) throws IOException {
        if (!file.exists()) {
            return;
        }

        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(baseDir + file.getName());
            zipOut.putNextEntry(entry);
            int count;
            byte data[] = new byte[BUFFER];
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                zipOut.write(data, 0, count);
            }

        } finally {
            if (null != bis) {
                bis.close();
            }
        }
    }

    /**
     * @param zipFile 被解压的压缩包文件名
     * @param dstPath 解压缩路径
     * @throws IOException
     */
    public static void unZip(String zipFile, String dstPath) throws IOException {
        File pathFile = new File(dstPath);
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }
        ZipFile zip = new ZipFile(zipFile);
        for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            String zipEntryName = entry.getName();
            InputStream in = null;
            OutputStream out = null;
            try {
                in = zip.getInputStream(entry);
                String outPath = (dstPath + "/" + zipEntryName).replaceAll("\\*", "/");
                ;
                //判断路径是否存在,不存在则创建文件路径
                File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
                if (!file.exists()) {
                    file.mkdirs();
                }
                //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
                if (new File(outPath).isDirectory()) {
                    continue;
                }

                out = new FileOutputStream(outPath);
                byte[] buf1 = new byte[1024];
                int len;
                while ((len = in.read(buf1)) > 0) {
                    out.write(buf1, 0, len);
                }
            } finally {
                if (null != in) {
                    in.close();
                }

                if (null != out) {
                    out.close();
                }
            }
        }
        zip.close();
    }

    public static void main(String[] args) throws Exception {
        String sourceFile = "D:\\IdeaProjects\\DEPLOY\\dist";
        String unZipFile = "D:\\IdeaProjects\\DEPLOY\\unZipFile.zip";
        String toZipFile = "D:\\IdeaProjects\\DEPLOY\\zipFile.zip";

        //将Zip文件解压缩到目标目录
        ZipUtils.unZip(unZipFile, sourceFile);

        //将目标目录的文件压缩成Zip文件
        ZipUtils.toZip(sourceFile, toZipFile);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值