Java解压缩文件

在Java中,我们经常需要处理压缩文件,如ZIP和RAR等。通过解压缩文件,我们可以提取其中的内容并进行进一步的处理。本文将介绍如何使用Java解压缩ZIP和RAR文件,并提供相应的代码示例。

ZIP文件解压缩

ZIP是一种常见的压缩文件格式,它可以包含多个文件和目录。Java提供了java.util.zip包来处理ZIP文件。下面是一个简单的示例演示如何解压缩ZIP文件:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipExtractor {

    public static void extract(String zipFile, String outputFolder) throws IOException {
        byte[] buffer = new byte[1024];
        try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(Paths.get(zipFile)))) {
            ZipEntry entry = zis.getNextEntry();
            while (entry != null) {
                String fileName = entry.getName();
                File newFile = new File(outputFolder + File.separator + fileName);
                if (entry.isDirectory()) {
                    newFile.mkdirs();
                } else {
                    new File(newFile.getParent()).mkdirs();
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        int len;
                        while ((len = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, len);
                        }
                    }
                }
                zis.closeEntry();
                entry = zis.getNextEntry();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        String zipFile = "path/to/your/file.zip";
        String outputFolder = "path/to/your/output/folder";
        extract(zipFile, outputFolder);
    }
}

上面的代码创建了一个ZipExtractor类,其中的extract方法接受两个参数:ZIP文件的路径和解压缩后的输出目录。该方法使用ZipInputStream逐个读取ZIP文件中的条目,并将其解压缩到指定目录中。

RAR文件解压缩

相比于ZIP文件,RAR文件的解压缩稍微复杂一些。Java本身不提供RAR文件的解压缩支持,但我们可以使用第三方库来完成这个任务。在本文中,我们将使用junrar库来解压缩RAR文件。下面是一个例子:

import com.github.junrar.Archive;
import com.github.junrar.exception.RarException;
import com.github.junrar.extract.ExtractArchive;

import java.io.File;
import java.io.IOException;

public class RarExtractor {

    public static void extract(String rarFile, String outputFolder) throws IOException, RarException {
        File file = new File(rarFile);
        Archive archive = new Archive(file);
        archive.getMainHeader().print();
        ExtractArchive extractArchive = new ExtractArchive();
        extractArchive.extractArchive(archive, new File(outputFolder));
    }

    public static void main(String[] args) throws IOException, RarException {
        String rarFile = "path/to/your/file.rar";
        String outputFolder = "path/to/your/output/folder";
        extract(rarFile, outputFolder);
    }
}

上面的代码使用了junrar库来解压缩RAR文件。首先,我们创建一个Archive对象,将RAR文件作为参数传递给它。然后,我们可以使用ExtractArchive类来解压缩文件,并将其保存到指定的输出目录中。

总结

通过本文,我们学习了如何使用Java解压缩ZIP和RAR文件。对于ZIP文件,我们可以使用java.util.zip包中的ZipInputStream类来逐个读取条目并将其解压缩。对于RAR文件,我们可以使用第三方库junrar来实现解压缩功能。希望本文能对你有所帮助!

gantt
    title Java解压缩文件
    dateFormat  YYYY-MM-DD
    section ZIP文件解压缩
    解压缩代码示例     :active, 2022-10-01, 1d
    section RAR文件解压缩
    解压缩代码示例     :active, 2022-10-02, 1d