基于java的zip 压缩和解压和修改压缩包

本文介绍了如何使用Java进行ZIP压缩、解压操作,并详细讲解了如何将文件写入压缩包以及修改已有的ZIP文件内容,涵盖了多种实用方法。

解压缩包 返回每个文件流

// 解压缩包
public static Map<String, OutputStream> unzip(InputStream inputStream) throws Exception {
        Map<String, OutputStream> map = new HashMap<>();
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        ZipEntry nextEntry = zipInputStream.getNextEntry();
        while (nextEntry != null) {
            //新建输出字节流
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            //写入数据
            write(zipInputStream, byteArrayOutputStream);
            //放入返回数据
            map.put(nextEntry.getName(), byteArrayOutputStream);
            //下一个压缩包
            nextEntry = zipInputStream.getNextEntry();
        }
        zipInputStream.close();
        return map;
    }

解压缩包 到本地

// 解压缩包
public static void unzip(String zipPath, String unzipPath) throws Exception {
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipPath));
        ZipEntry nextEntry = zipInputStream.getNextEntry();
        while (nextEntry != null) {
            String name = nextEntry.getName();
            String filePath = unzipPath + name;
            String dir = getDir(filePath);
            if (nextEntry.isDirectory()) {
                mkdir(dir + "/");
            } else {
                mkdir(dir);
                FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                write(zipInputStream, fileOutputStream);
                fileOutputStream.close();
            }
            nextEntry = zipInputStream.getNextEntry();
        }
        zipInputStream.close();
    }

压缩包传入流

/**
     * 压缩包传入的流
     * @param map 可以是每个压缩包中的文件名
     * @return
     * @throws Exception
     */
    public static OutputStream zip(Map<String, InputStream> map) throws Exception {
    	//字节数组流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //压缩流
        ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
        try {
            map.forEach((k, v) -> {
                try {
                	//放入元素
                    zipOutputStream.putNextEntry(new ZipEntry(k));
                    write(v, zipOutputStream);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        } finally {
            zipOutputStream.close();
        }
        return byteArrayOutputStream;
    }

压缩文件

 /**
     * 压缩文件
     *
     * @param zipPath
     * @param filepath
     */
    public static void zip(String zipPath, String... filepath) throws Exception {
        FileOutputStream fileOutputStream = new FileOutputStream(zipPath);
        ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
        for (String s : filepath) {
            String zipName = getFileName(s);
            zipOutputStream.putNextEntry(new ZipEntry(zipName));
            FileInputStream fileInputStream = new FileInputStream(s);
            write(fileInputStream, zipOutputStream);
            fileInputStream.close();
        }
        zipOutputStream.close();
    }

通用write 方法

 public static void write(InputStream inputStream, OutputStream outputStream) throws IOException {
        byte[] bytes = new byte[1024];
        int length = 0;
        while ((length = inputStream.read(bytes)) > 0) {
            outputStream.write(bytes, 0, length);
        }
//        outputStream.close();
//        inputStream.close();
    }

修改zip包中的文件

/**
     * 修改文件内容
     *
     * @param inputStream  输入zip 流
     * @param outputStream 输出zip 流
     * @param isUpdate     修改条件
     * @param handle       修改的具体逻辑处理
     * @throws IOException
     */
    public static void updateZipContent(InputStream inputStream, OutputStream outputStream, Predicate<ZipEntry> isUpdate, Function<byte[], byte[]> handle) throws IOException {
    	//输入流
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        //输出zip 流
        ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
        ZipEntry nextEntry = zipInputStream.getNextEntry();
        while (nextEntry != null) {
        //是需要处理的文件处理返回处理完成的字节数组放入新的文件流中
            if (isUpdate.test(nextEntry)) {
                byte[] bytes = zipInputStream.readAllBytes();
                byte[] apply = handle.apply(bytes);
                zipOutputStream.putNextEntry(new ZipEntry(nextEntry.getName()));
                zipOutputStream.write(apply);

            } else {
                //不是的也需要解压
                zipOutputStream.putNextEntry(new ZipEntry(nextEntry.getName()));
                byte[] buf = new byte[1024];
                int len;
                while ((len = zipInputStream.read(buf)) > 0) {
                    zipOutputStream.write(buf, 0, len);
                }
            }
            nextEntry = zipInputStream.getNextEntry();
        }
        zipInputStream.close();
        inputStream.close();
        zipOutputStream.close();
    }

夫英雄者,胸怀大志,腹有良谋,有包藏宇宙之机,吞吐天地之志者也。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值