当前位置:首页 > Java

java如何解压文件

2026-02-05 09:08:41Java

使用 java.util.zip 包解压文件

Java 提供了 java.util.zip 包来处理 ZIP 格式的压缩文件。以下是一个简单的解压示例:

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

public class UnzipExample {
    public static void unzip(String zipFilePath, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }

        try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) {
            ZipEntry entry = zipIn.getNextEntry();
            while (entry != null) {
                String filePath = destDirectory + File.separator + entry.getName();
                if (!entry.isDirectory()) {
                    extractFile(zipIn, filePath);
                } else {
                    File dir = new File(filePath);
                    dir.mkdir();
                }
                zipIn.closeEntry();
                entry = zipIn.getNextEntry();
            }
        }
    }

    private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
            byte[] bytesIn = new byte[4096];
            int read;
            while ((read = zipIn.read(bytesIn)) != -1) {
                bos.write(bytesIn, 0, read);
            }
        }
    }
}

使用 Apache Commons Compress 库

对于更复杂的压缩格式(如 RAR、7z 等),可以使用 Apache Commons Compress 库:

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;

public class CommonsUnzip {
    public static void unzip(String zipFile, String outputFolder) throws IOException {
        try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new FileInputStream(zipFile))) {
            ZipArchiveEntry entry;
            while ((entry = zis.getNextZipEntry()) != null) {
                File outputFile = new File(outputFolder, entry.getName());
                if (entry.isDirectory()) {
                    outputFile.mkdirs();
                } else {
                    outputFile.getParentFile().mkdirs();
                    try (OutputStream os = new FileOutputStream(outputFile)) {
                        IOUtils.copy(zis, os);
                    }
                }
            }
        }
    }
}

处理 GZIP 压缩文件

对于单个文件的 GZIP 解压缩:

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

public class GzipExample {
    public static void ungzip(String gzipFile, String outputFile) throws IOException {
        try (GZIPInputStream gis = new GZIPInputStream(new FileInputStream(gzipFile));
             FileOutputStream fos = new FileOutputStream(outputFile)) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = gis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
        }
    }
}

注意事项

解压文件时应考虑以下安全因素:

java如何解压文件

  • 检查解压路径是否在目标目录内,防止路径遍历攻击
  • 检查解压后文件大小,防止 ZIP 炸弹
  • 处理大文件时考虑内存使用情况
  • 关闭所有流资源,使用 try-with-resources 语句

这些方法提供了 Java 中处理常见压缩文件格式的基本方式,可根据具体需求进行调整和扩展。

标签: 文件java
分享给朋友:

相关文章

vue实现录音文件播放

vue实现录音文件播放

实现录音文件播放的方法 在Vue中实现录音文件播放需要结合HTML5的Web Audio API或第三方库。以下是两种常见的方法: 使用HTML5 Audio元素 通过Vue动态绑定<aud…

如何选择java培训

如何选择java培训

评估培训机构资质 选择有正规资质的机构,查看其营业执照、办学许可证等。优先考虑具备人力资源和社会保障部或教育部认证的机构,这类机构的教学质量和课程设置通常更规范。 考察课程内容与行业需求匹配度…

如何导入java项目

如何导入java项目

导入Java项目的方法 使用IDE导入(如IntelliJ IDEA或Eclipse) 打开IDE后选择导入现有项目,导航至项目根目录(包含pom.xml或build.gradle的文件位置)。IDE…

java如何返回数组

java如何返回数组

返回数组的方法 在Java中,可以通过多种方式返回数组。以下是几种常见的方法: 直接返回数组 定义一个方法,返回类型为数组类型,直接在方法内部创建并返回数组。 public int[] getI…

java面试官如何面试别人

java面试官如何面试别人

考察基础知识 从Java核心概念入手,包括面向对象特性(封装、继承、多态)、集合框架(ArrayList vs LinkedList、HashMap实现原理)、异常处理机制、多线程(线程池、锁机制)等…

vue实现打印文件流

vue实现打印文件流

Vue 实现打印文件流 在 Vue 中实现打印文件流(如 PDF、图片或其他二进制数据)通常涉及接收后端返回的文件流,将其转换为可打印的格式,并调用浏览器的打印功能。以下是具体实现方法: 接…