java 解压zip文件,嵌套解压zip文件

Java中可以使用ZipInputStream和GZIPInputStream来解压缩压缩文件,其中ZipInputStream可以处理嵌套的压缩文件。

以下是一个用Java解压缩嵌套的压缩文件的示例代码:

package com.demo.util;

/**
 * description:zip解压例子
 *
 * @author: lgq
 * @create: 2023-05-17 18:49
 */

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

public class UnzipExample {
    public static void unzip(File zipFile, File destination) throws IOException {
        byte[] buffer = new byte[1024];
        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
            ZipEntry zipEntry = zis.getNextEntry();
            while (zipEntry != null) {
                File newFile = newFile(destination, zipEntry);
                if (zipEntry.isDirectory()) {
                    if (!newFile.isDirectory() && !newFile.mkdirs()) {
                        throw new IOException("Failed to create directory " + newFile);
                    }
                } else {
                    File parent = newFile.getParentFile();
                    if (!parent.isDirectory() && !parent.mkdirs()) {
                        throw new IOException("Failed to create directory " + parent);
                    }
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        int len;
                        while ((len = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, len);
                        }
                    }
                }
                try {
                    zipEntry = zis.getNextEntry(); //有些文件会报错,例如.json文件,不需要处理的话可以直接跳过
                } catch (ZipException e) {
                    zipEntry = zis.getNextEntry();
                }
            }
        }
    }

    private static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
        File destFile = new File(destinationDir, zipEntry.getName());
        String destDirPath = destinationDir.getCanonicalPath();
        String destFilePath = destFile.getCanonicalPath();
        if (!destFilePath.startsWith(destDirPath + File.separator)) {
            throw new IOException("Entry is outside of the target directory: " + zipEntry.getName());
        }
        return destFile;
    }

    public static void main(String[] args) throws IOException {
        File zipFile = new File("D:\\帆软\\treasures.zip");
        File destination = new File("D:\\帆软\\unzipped");
        unzip(zipFile, destination);
    }
}

该示例代码使用了try-with-resources语句来确保关闭流,它还包含了一个newFile()方法,该方法用于检查解压缩文件的目录是否在指定的目标目录中。

如果嵌套的zip文件也需要被解压缩,可以使用递归方法来遍历所有的zip文件并解压缩。以下是一个简单的Java代码示例,可以解压缩所有嵌套的zip文件。

package com.demo.util;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * description:unzipAll例子
 *
 * @author: lgq
 * @create: 2023-05-17 18:58
 */

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

public class UnzipAllExample {
    public static void main(String[] args) {
        String zipFilePath = "D:\\帆软\\treasures.zip";
        String destDirPath = "D:\\帆软";
        unzipAll(zipFilePath, destDirPath);
    }

    public static void unzipAll(String zipFilePath, String destDirPath) {
        try {
            File destDir = new File(destDirPath);
            if (!destDir.exists()) {
                destDir.mkdir();
            }
            ZipFile zipFile = new ZipFile(zipFilePath);
            ZipEntry entry;
            String entryName;
            File entryFile, entryDir;
            InputStream inStream;
            OutputStream outStream;
            byte[] buffer = new byte[1024];
            int bytesRead;
            for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) {
                entry = (ZipEntry) e.nextElement();
                entryName = entry.getName();
                entryFile = new File(destDir, entryName);
                if (entry.isDirectory()) {
                    entryFile.mkdirs();
                } else {
                    entryDir = new File(entryFile.getParent());
                    if (!entryDir.exists()) {
                        entryDir.mkdirs();
                    }
                    inStream = zipFile.getInputStream(entry);
                    outStream = new FileOutputStream(entryFile);
                    while ((bytesRead = inStream.read(buffer)) != -1) {
                        outStream.write(buffer, 0, bytesRead);
                    }
                    outStream.close();
                    inStream.close();
                    if (entryName.endsWith(".zip")) {
                        unzipAll(entryFile.getAbsolutePath(), entryDir.getAbsolutePath());
                    }
                    //这里也可以做一些需要的文件读取操作
                }
            }
            zipFile.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

在这个示例中,unzipAll方法接收一个zip文件的路径和一个目标目录的路径,并递归地解压缩所有嵌套的zip文件。如果一个zip文件被解压缩,它将被保存到与其同级的目录中。

你可以将 zipFilePathdestDirPath 替换为你自己的实际值,然后运行这个示例来解压缩所有嵌套的zip文件。

参考:ChitGPT

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
程序设计题二:ZIP格式压缩/解压系统设计 【问题描述】 由于网络带宽的限制,开发以互联网为传输媒介的软件系统时,在运行过程中的数据传输效率会成为评价一套软件系统性能的重要指标。由于网络的数据传输速度是软件运行的客观因素,因此,在这种情况下,程序设计人员首先考虑的减少软件系统运行过程中需要传输的数据量,如果有些数据必须要传输,则软件工程师通常将这些数据在发送端进行压缩,而在数据接收端将数据解压缩,从而主动减少应用系统数据传输量。 JDK环境中提供了多种类型的数据压缩方式,总结起来,利用Java语言可以创建的数据文件压缩格式包括如下类型:   ●ZIP格式   ●GZIP格式   ●JAR格式 通过设计,允许创建ZIP压缩文件,并对ZIP压缩文件中包含的文件进行显示、添加、解压、删除等操作。GUI界面与下图类似: 【实验目的】 要求学生能熟练使用基于Swing的GUI设计,熟练使用常用组件和容器,理解java事件处理机制,会查看API documentation完成设计任务,熟练文件流的操作。 【基本功能】 (1) 通过菜单组件、按钮组件、文本框组件等完成创建ZIP压缩文件。 (2) ZIP压缩文件中包含的文件进行显示、添加、解压、删除等操作 (3) 功能的其他扩展 【指导建议】 完成实验指导书P97“Zip文件的读取与制作” 将程序进行修改,增加GUI设计,并完成基本功能, 其他功能扩展和完善。 【程序设计的开发环境】 JDK1.5 及JCreator350/400。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值