基于内存以ZIP进行压缩和解压工具类


import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.poi.util.IOUtils;

/**
 *基于内存以ZIP算法进行压缩和解压工具类.
 */
public class ZipRamUtil {

    /**
     * 使用ZIP算法进行压缩.
     * @param sourceFileBytesMap 待压缩文件的Map集合.
     * @return 压缩后的ZIP文件字节数组.
     * @throws Exception 压缩过程中可能发生的异常,若发生异常,则返回的字节数组长度为0.
     */
    public static byte[] compressByZip(Map<String, byte[]> sourceFileBytesMap) throws IOException {
        // 变量定义.
        ZipOutputStream zipZos = null;
        ByteArrayOutputStream zipBaos = null;

        try {
            // 压缩文件变量初始化.
            zipBaos = new ByteArrayOutputStream();
            zipZos = new ZipOutputStream(zipBaos);
            // 将文件添加到ZIP条目中.
            if (null != sourceFileBytesMap && sourceFileBytesMap.size() > 0) {
                for (Map.Entry<String, byte[]> singleFile : sourceFileBytesMap.entrySet()) {
                    // 设置文件名
                    ZipEntry zipEntry = new ZipEntry(singleFile.getKey());
                    zipZos.putNextEntry(zipEntry);
                    // 写入文件
                    zipZos.write(singleFile.getValue());
                    // 停止当前文件写入
                    zipZos.closeEntry();
                }
                // 结束压缩
                zipZos.finish();
            } else {
                zipBaos = new ByteArrayOutputStream();
            }
        } finally {
            if (null != zipBaos){
                zipBaos.close();
            }
            if (null != zipZos){
                zipZos.close();
            }
        }
        return zipBaos.toByteArray();
    }


    /**
     * 使用ZIP算法进行解压.
     * @param sourceZipFileBytes ZIP文件字节数组.
     * @return 解压后的文件Map集合.
     * @throws Exception 解压过程中可能发生的异常,若发生异常,返回Map集合长度为0.
     */
    public static Map<String, byte[]> decompressByZip(byte[] sourceZipFileBytes) throws Exception {
        // 变量定义.
        String zipEntryName = null;
        ZipEntry singleZipEntry = null;
        ZipInputStream sourceZipZis = null;
        BufferedInputStream sourceZipBis = null;
        ByteArrayInputStream sourceZipBais = null;
        Map<String, byte[]> targetFilesFolderMap = null;

        try {
            // 解压变量初始化.
            targetFilesFolderMap = new HashMap<String, byte[]>();
            sourceZipBais = new ByteArrayInputStream(sourceZipFileBytes);
            sourceZipBis = new BufferedInputStream(sourceZipBais);
            sourceZipZis = new ZipInputStream(sourceZipBis);
            // 条目解压缩至Map中.
            while ((singleZipEntry = sourceZipZis.getNextEntry()) != null) {
                zipEntryName = singleZipEntry.getName();
                targetFilesFolderMap.put(zipEntryName, IOUtils.toByteArray(sourceZipZis));
            }
        } finally {
            if (null != sourceZipZis){
                sourceZipZis.close();
            }
            if (null != sourceZipBis){
                sourceZipBis.close();
            }
            if (null != sourceZipBais){
                sourceZipBais.close();
            }

        }
        return targetFilesFolderMap;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值