zip工具

压缩多个oss文件

/**
     * 打包 多个oss文件 为 zip 包
     * @param filenames 所要压缩的源 oss文件
     * @param show_files 所要展示的文件名称列表(压缩完之后,压缩包里面的文件名称)
     * @throws IOException
     */
    private   File zipFileListFromObj(List<OSSObject> filenames ,List<String> show_files) throws IOException {

        String filename = "临时名称"+".zip";//生成临时压缩包名字

        byte[] buffer = new byte[8192];
        String ziptemp = ziptempPath;//临时zip压缩包存放路径
        String strZipPath = ziptemp+filename;//生成的压缩包完整路径

        File path = new File(ziptemp);
        if(!path.exists()) {
            path.mkdir();
        }
        File f = new File(strZipPath);//生成的临时压缩包文件
		//读写文件
        ZipOutputStream out =null;
        try {
            out = new ZipOutputStream(new FileOutputStream(strZipPath)) ;
            out.setEncoding("gbk");//设置压缩文件内的字符编码,不然会变成乱码
            for (int i = 0; i < filenames.size(); i++) {
                OSSObject ossObject = filenames.get(i);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(ossObject.getObjectContent());
                out.putNextEntry(new ZipEntry((String)show_files.get(i)));   //包内文件名称
                int len;
                // 读入需要下载的文件的内容,打包到zip文件
                while ((len = bufferedInputStream.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                out.closeEntry();
                bufferedInputStream.close();
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
            log.error("文件不存在,打包失败!");
            throw new IOException("文件不存在,打包失败!");
        }
        finally
        {
            if(out!=null)
            {
                out.flush();
                out.close();
            }
        }
        return f;
    }

压缩多个文件 File

/**
     * 打包压缩文件
     * @param filenames 所要压缩的源文件名称
     * @param show_files 所要展示的文件名称列表(压缩完之后,压缩包里面的文件名称)
     * @throws IOException
     */
    private   File zipFileList(List<File> filenames ,List<String> show_files) throws IOException {

        String filename = buildTempName()+".zip";//生成临时压缩包名字

        byte[] buffer = new byte[8192];
        String ziptemp = ziptempPath;//临时zip压缩包存放路径
        String strZipPath = ziptemp+filename;//生成的压缩包完整路径

        File path = new File(ziptemp);
        if(!path.exists()) {
            path.mkdir();
        }

        File f = new File(strZipPath);//生成的临时压缩包文件
        ZipOutputStream out =null;
        try {
            out = new ZipOutputStream(new FileOutputStream(strZipPath)) ;
            out.setEncoding("gbk");//设置压缩文件内的字符编码,不然会变成乱码
            for (int i = 0; i < filenames.size(); i++) {

                File file = filenames.get(i);
                if(!file.exists()) {
                    log.error("文件"+file.getName()+"不存在,打包失败!");
                    throw new IOException("文件"+file.getName()+"不存在,打包失败!");
                }

                FileInputStream fis = new FileInputStream(file);
                out.putNextEntry(new ZipEntry((String)show_files.get(i)));   //包内文件名称
                int len;
                // 读入需要下载的文件的内容,打包到zip文件
                while ((len = fis.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                out.closeEntry();
                fis.close();
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
            log.error("文件不存在,打包失败!");
            throw new IOException("文件不存在,打包失败!");
        }
        finally
        {
            if(out!=null)
            {
                out.flush();
                out.close();
            }
        }
        return f;
    }

解压zip

/***
     * 解压GZip
     *
     * @param data
     * @return
     */
    public static byte[] unGZip(byte[] data) {
        byte[] b = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            GZIPInputStream gzip = new GZIPInputStream(bis);
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = gzip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
            gzip.close();
            bis.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return b;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值