JAVA打包和解压文件接口

  • package com.supercloud.common.utils;
    
    import java.io.BufferedInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipOutputStream;
    
    public class ZipUnzipUtils {
        /**
         * 一个压缩包中包含多个文件,
         * @return 文件的byte[]
         */
        public static List<byte[]> decompressFiles(InputStream in) {//解压缩
            List<byte[]> results = new ArrayList<>();
            ZipInputStream Zin= null;
            BufferedInputStream Bin= null;
            ByteArrayOutputStream bos = null;
            try {
                Zin=new ZipInputStream(in);
                Bin=new BufferedInputStream(Zin);
                ZipEntry entry;
                while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){
                    bos = new ByteArrayOutputStream(0xffff);
                    int b;
                    while((b=Bin.read())!=-1){
                        bos.write(b);
                    }
                    if(bos != null) {
                        results.add(bos.toByteArray());
                    }
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if(null != Bin){
                    try {
                        Bin.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(null != Zin){
                    try {
                        Zin.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return results;
        }
    
    //压缩打包
    /** 一个压缩包中包含多个文件,
     * @return 文件的byte[]
     * params is(文件流);fileName(文件名,非压缩包名)
     */
        public static void addInputstreamToZip(ZipOutputStream zipOutputStream, InputStream is, String fileName){
            try {
                zipOutputStream.putNextEntry(new ZipEntry(fileName));
                byte[] bytes = new byte[1024];
                int n = 0;
                while ((n = is.read(bytes)) != -1) {
                    zipOutputStream.write(bytes, 0, n);
                    zipOutputStream.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值