Apache compress单文件、多文件zip压缩/解压

1.添加maven依赖

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.14</version>
</dependency>

 

2.开始测试

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

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

public class ZipConpress {

    static int bufferSize = 8192;//单位bytes

    /**
     * 用于单文件压缩
     */
    public static void doCompress(File srcFile, File destFile) throws IOException {
        ZipArchiveOutputStream out = null;
        InputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(srcFile), bufferSize);
            out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferSize));
            ZipArchiveEntry entry = new ZipArchiveEntry(srcFile.getName());
            entry.setSize(srcFile.length());
            out.putArchiveEntry(entry);
            IOUtils.copy(is, out);
            out.closeArchiveEntry();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(out);
        }
    }

    /** 
     * 用于多文件压缩 
     */
    public static void doCompressFiles(List<File> srcFiles, File destFile) throws IOException {
        ZipArchiveOutputStream out = null;
        InputStream is = null;
        try {

            out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferSize));

            for(File srcFile:srcFiles){
                is = new BufferedInputStream(new FileInputStream(srcFile), bufferSize);
                ZipArchiveEntry entry = new ZipArchiveEntry(srcFile.getName());
                entry.setSize(srcFile.length());
                out.putArchiveEntry(entry);
                IOUtils.copy(is, out);
            }
            out.closeArchiveEntry();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(out);
        }
    }

    public static void doDecompress(File srcFile, File destDir) throws IOException {
        ZipArchiveInputStream is = null;
        try {
            is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferSize));
            ZipArchiveEntry entry = null;
            while ((entry = is.getNextZipEntry()) != null) {
                if (entry.isDirectory()) {
                    File directory = new File(destDir, entry.getName());
                    directory.mkdirs();
                } else {
                    OutputStream os = null;
                    try {
                        os = new BufferedOutputStream(
                                new FileOutputStream(new File(destDir, entry.getName())), bufferSize);
                        IOUtils.copy(is, os);
                    } finally {
                        IOUtils.closeQuietly(os);
                    }
                }
            }
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    public static void main(String[] args) {
        try {
         //ZipConpress.doCompress(new File("/pathto/abc.txt"),new File("/pathto/abc-test.zip"));

            List<File> files=new ArrayList<File>();
            files.add(new File("/pathto/abc.txt"));
            files.add(new File("/pathto/xyz.txt"));


            ZipConpress.doCompressFiles(files,new File("/pathto/abc-test.zip"));


//            File doDecompressDir = new File("/pathto/abc-test1723");
//            if (!doDecompressDir.exists()) {
//                doDecompressDir.mkdirs();
//            }
//            ZipConpress.doDecompress(new File("/pathto/abc-test.zip"), doDecompressDir
//            );

        } catch (IOException e) {

            e.printStackTrace();
        }


    }
}

转载于:https://my.oschina.net/guoshuyun/blog/1377607

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值