JAVA实现文件压缩、解压zip包及相关处理

说明

在jdk1.8版本中,已经提供了针对文件压缩zip包的工具类。

1、压缩文件集合到指定目录

 

   /**
     * @Description 压缩文件集合到指定的目录
     **/
    public static void zipFiles(String zipFileName, String zipFilePath, List<FileDto> fileList) {
        logger.info("fileList -- > " + JSON.toJSONString(fileList));
        if(CollectionUtils.isEmpty(fileList)){
            logger.info("需要压缩的文件为空");
            return;
        }
        // 临时文件
        File tempfile = new File(zipFilePath + File.separator + zipFileName);
        if(tempfile.exists()){
            tempfile.delete();
        }
        final InputStream input = null;
        ZipOutputStream zipOut = null;
        try {
            FileOutputStream fos = new FileOutputStream(tempfile);
            zipOut = new ZipOutputStream(fos);
            // zip的名称为
            zipOut.setComment(zipFileName);
//            zipOut.setEncoding("GBK");
            fileList.forEach(fileDto -> {
                byte [] fileBytes = getFileData(fileDto.getFilePath(), fileDto.getFileName());
                input = new ByteArrayInputStream(fileBytes);
                zipOut.putNextEntry(new ZipEntry(fileDto.getFileName()));
                int temp = 0;
                while ((temp = input.read()) != -1) {
                    zipOut.write(temp);
                }
            });
        }catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            try {
                if (zipOut != null) {zipOut.close();}
                if (input != null) {input.close();}
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }

2、解压(暂未提供)

3、zip转byte[]

注:若在压缩zip包的方法体内将zip压缩包转byte[],需要先将ZipOutputStream流关闭。

 

/**
 *  文件转byte[]
 */
public static byte[] fileToBytes(String path) {
    File file = new File(path);
    FileInputStream fis;
    try {
        fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[(int)file.length()];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        return bos.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

4、byte[]转zip 

/**
 * 字节流写入文件
 */
public static void bytesToFile(byte[] data, String filePath) {
    File file = new File(filePath);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
    }

    FileOutputStream o = null;
    try {
        o = new FileOutputStream(filePath);
        o.write(data, 0, data.length);
        o.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != o) {
            try {
                o.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值