Java Zip压缩解压

简介

在日常的软件开发中,我们经常需要处理文件的压缩与解压缩操作。而Java提供了强大的Zip压缩解压功能,可以轻松地实现文件的打包和解压。本文将介绍Java中如何使用Zip进行文件压缩和解压缩操作,并提供代码示例。

Zip压缩

Zip压缩是将多个文件或目录打包成一个单独的压缩文件的过程。Java中提供了java.util.zip包,其中的ZipOutputStream类可以用于创建Zip压缩文件,并将文件添加到压缩文件中。

下面是一个简单的示例,演示了如何使用Java进行文件压缩:

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

public class ZipExample {

    public static void main(String[] args) {
        String sourceFile = "source.txt";
        String zipFile = "archive.zip";

        try {
            FileOutputStream fos = new FileOutputStream(zipFile);
            ZipOutputStream zos = new ZipOutputStream(fos);

            File file = new File(sourceFile);
            FileInputStream fis = new FileInputStream(file);

            ZipEntry ze = new ZipEntry(file.getName());
            zos.putNextEntry(ze);

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }

            fis.close();
            zos.closeEntry();
            zos.close();

            System.out.println("File Compressed Successfully");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

上述代码通过创建FileOutputStreamZipOutputStream,将源文件source.txt添加到压缩文件archive.zip中,最后关闭流。

Zip解压

Zip解压是将压缩文件中的内容解压到指定目录或文件的过程。Java中提供了ZipInputStream类,可以用于解压缩Zip文件。

下面是一个简单的示例,演示了如何使用Java进行文件解压缩:

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

public class UnzipExample {

    public static void main(String[] args) {
        String zipFile = "archive.zip";
        String destDir = "unzipped";

        byte[] buffer = new byte[1024];

        try {
            File folder = new File(destDir);
            if (!folder.exists()) {
                folder.mkdir();
            }

            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                String fileName = ze.getName();
                File newFile = new File(destDir + File.separator + fileName);

                new File(newFile.getParent()).mkdirs();

                FileOutputStream fos = new FileOutputStream(newFile);

                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
                ze = zis.getNextEntry();
            }

            zis.closeEntry();
            zis.close();

            System.out.println("File Unzipped Successfully");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

上述代码通过创建ZipInputStream,读取压缩文件archive.zip中的内容并解压到unzipped文件夹中。

流程图

以下是Zip压缩和解压的流程图:

st=>start: 开始
op1=>operation: 创建 ZipOutputStream
op2=>operation: 添加文件到压缩文件
op3=>operation: 关闭 ZipOutputStream
cond1=>condition: 压缩成功?
e=>end: 结束

st->op1->op2->op3->cond1
cond1(yes)->e
cond1(no)->e

st=>start: 开始
op1=>operation: 创建 ZipInputStream
op2=>operation: 读取压缩文件中的内容
op3=>operation: 解压缩到指定目录或文件
op4=>operation: 关闭 ZipInputStream
cond1=>condition: 解压成功?
e=>end: 结束

st->op1->op2->op3->op4->cond1
cond1(yes)->e
cond1(no)->e

结论

本文介绍了Java中使用Zip进行文件压缩和解压缩的方法,并提供了相应的代码示例。通过使用java.util.zip包中的ZipOutputStreamZipInputStream类,我们可以方便地