Java 压缩与解压(zip)

准备

示例是基于 Maven Project,需要引入依赖:

<dependency>
    <groupId>net.lingala.zip4j</groupId>
    <artifactId>zip4j</artifactId>
    <version>2.2.8</version>
</dependency>

压缩

待压缩的文件:
在这里插入图片描述

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;

public class Application {
    
    public static void main(String[] args) {
        File file = new File("G:" + File.separator + "20191212" + File.separator + "beach.jpg");
        InputStream is = null;
        
        try {
            is = new FileInputStream(file);
            ZipFile zipFile = new ZipFile("picture.zip");
            ZipParameters zipParameters = new ZipParameters();
            zipParameters.setFileNameInZip(file.getName());
            zipFile.addStream(is, zipParameters);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ZipException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

运行,由于使用了相对路径,所以可以在项目目录下找到生成的压缩文件:
在这里插入图片描述

解压

解压到当前项目的 picture 目录下

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;

public class ExtractingAll {
    
    public static void main(String[] args) {
        ZipFile zipFile = new ZipFile("picture.zip");
        try {
            zipFile.extractAll("./picture"); // 解压到当前项目的 picture 目录下
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

以输出流的形式写出

import net.lingala.zip4j.io.inputstream.ZipInputStream;
import net.lingala.zip4j.model.LocalFileHeader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class ZipInputStreamExample {

    public static void main(String[] args) {
        File zipFile = new File("picture.zip"); // 获取压缩文件的实例
        
        FileInputStream fis = null;
        ZipInputStream zis = null;
        
        LocalFileHeader localFileHeader = null;
        int readLength;
        byte[] readBuffer = new byte[1024 * 4];
        
        try {
            fis = new FileInputStream(zipFile); // 文件输入流
            zis = new ZipInputStream(fis); // ZIP 输入流
            
            // 逐个写出压缩文件中的文件
            while ((localFileHeader = zis.getNextEntry()) != null) {
                File extractedFile = new File(localFileHeader.getFileName());
                OutputStream os = null;
                try {
                    os = new FileOutputStream(extractedFile);
                    while ((readLength = zis.read(readBuffer)) != -1) {
                        os.write(readBuffer, 0, readLength); // 写出
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (os != null) {
                            os.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (zis != null) {
                    zis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

在这里插入图片描述
参考:zip4j

发布了36 篇原创文章 · 获赞 0 · 访问量 1873

猜你喜欢

转载自blog.csdn.net/qq_29761395/article/details/103733807
今日推荐