Java zip/gzip文件压缩和解压缩

Java IO

为了减少传输时的数据量 在Java中提供了专门的压缩流将文件或者文件夹压缩成zip,gzip,jar等文件形式。

压缩流实现

Java支持的三种压缩格式:zip、jar、gzip。

1.zip是一种较为常见的压缩格式,Java提供了java.util.zip包,常用类:

  • ZipInputStream
  • ZipOutputStream
  • ZipEntry
  • ZipFile

依靠上述4个类完成zip类型文件操作。

2.Jar是Java专有的一种压缩格式,Java提供了java.util.jar包,常用类:

  • JarInputStream
  • JarOutputStream
  • JarEntry
  • JarFile


3.GZIP是Unix系统的文件压缩格式,Linux系统中经常用的.gz 文件就是GZIP格式,Java提供了

  • GZipInputStream
  • GZipOutputStream


本文以zip 压缩/解压缩为例来讲解。

压缩

@Test
public void testZip() throws IOException {

    File zipFile = new File("D:\\log.zip"); //压缩后的文件
    ZipOutputStream zipOut = null;
    try{
        zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
        File dir = new File("D:\\logs\\info\\");   //待压缩的文件夹
        File[] files = dir.listFiles();
        for (File file : files){
            InputStream in = null;
            try{
                zipOut.putNextEntry(new ZipEntry(file.getName())) ;
                zipOut.setComment("test zip") ;  // 设置注释
                in = new FileInputStream(file) ; // 定义文件的输入流
                IoUtils.copy(in, zipOut);
            }finally {
                IoUtils.closeQuietly(in);
            }
        }
    }finally {
        IoUtils.closeQuietly(zipOut);
    }

    System.out.println("压缩文件完成");
}

解压缩

@Test
public void testUnZip() throws IOException {

    File dir = new File("D:\\test");
    if(!dir.exists()){
        dir.mkdirs();
    }

    File srcFile = new File("D:\\log.zip");
    ZipFile zipFile = new ZipFile(srcFile);

    ZipInputStream zipIn = null;
    try{
        zipIn = new ZipInputStream(new FileInputStream(srcFile));
        ZipEntry entry = null;
        while((entry = zipIn.getNextEntry())!=null){
            System.out.println("解压缩文件:" + entry.getName());
            OutputStream out = null;
            InputStream in = null;
            try{
                File file = new File(dir, entry.getName());
                in = zipFile.getInputStream(entry);
                out = new FileOutputStream(file);
                IoUtils.copy(in, out);
            }finally {
                IoUtils.closeQuietly(out);
                IoUtils.closeQuietly(in);
            }
        }
    }finally {
        IoUtils.closeQuietly(zipIn);
    }

    System.out.println("解压缩文件完成");
}

IoUtils.java

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 * @create 2016-10-31 10:19
 */
public class IoUtils {

    public static void closeQuietly(InputStream input){
        closeQuietly((Closeable)input);
    }

    public static void closeQuietly(OutputStream output){
        closeQuietly((Closeable)output);
    }

    public static void closeQuietly(Closeable closeable) {
        try {
            if(closeable != null) {
                closeable.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void closeQuietly(Closeable... closeables){
        for (Closeable closeable : closeables){
            closeQuietly(closeable);
        }
    }

    public static long copy(InputStream in, OutputStream out, int bufferSize) throws IOException {
        byte[] buff = new byte[bufferSize];
        return copy(in, out, buff);
    }

    public static long copy(InputStream in, OutputStream out) throws IOException {
        byte[] buff = new byte[1024];
        return copy(in, out, buff);
    }

    public static long copy(InputStream in, OutputStream out, byte[] buff) throws IOException {
        long count = 0;
        int len = -1;
        while((len=in.read(buff, 0, buff.length))!=-1){
            out.write(buff, 0, len);
            count += len;
        }
        return count;
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值