java base64工具类_java工具类 文件zip压缩 base64 加密,base64解密 zip解压

这个Java工具类提供了文件压缩成zip并转换为Base64编码的功能,以及Base64解码后解压zip文件的方法。适用于在网络传输中处理文件。
摘要由CSDN通过智能技术生成

packagecom.cfam.utils;importjava.io.BufferedOutputStream;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.ArrayList;importjava.util.Base64;importjava.util.List;importjava.util.zip.ZipEntry;importjava.util.zip.ZipInputStream;importjava.util.zip.ZipOutputStream;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;/***

* Title: ZipToBase64.java

*

* Description: 压缩 转 base64 网络传输

*

*@authorken chen

* @date 2019年7月26日

*@paramsrcFiles 需要压缩的文件

*@returnBase64 压缩文件后进行base64编码

*@version1.0*/

public classZipToBase64 {private static final int BUFFER_SIZE = 2 * 1024;private static final Logger log = LoggerFactory.getLogger(ZipToBase64.class);public static String toBase64Zip(List srcFiles) throwsRuntimeException {

log.info("开始压缩文件 [{}]", srcFiles);long start =System.currentTimeMillis();

String base64toZip= "";

ZipOutputStream zos= null;

ByteArrayOutputStream baos= newByteArrayOutputStream();try{

zos= newZipOutputStream(baos);for(File srcFile : srcFiles) {byte[] buf = new byte[BUFFER_SIZE];

log.info("压缩的文件名称 [{}] ", srcFile.getName() + "压缩的文件大小 [{}] ", srcFile.length());

zos.putNextEntry(newZipEntry(srcFile.getName()));intlen;

FileInputStream in= newFileInputStream(srcFile);while ((len = in.read(buf)) != -1) {

zos.write(buf,0, len);

}

zos.closeEntry();

in.close();

}long end =System.currentTimeMillis();

log.info("压缩完成,耗时: [{}] ms", (end -start));

}catch(Exception e) {throw new RuntimeException("zip error from ZipToBase64", e);

}finally{if (zos != null) {try{

zos.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}byte[] refereeFileBase64Bytes =Base64.getEncoder().encode(baos.toByteArray());try{

base64toZip= new String(refereeFileBase64Bytes, "UTF-8");

}catch(Exception e) {throw new RuntimeException("压缩流出现异常", e);

}returnbase64toZip;

}/***

* Title: ZipToBase64.java

*

* Description: Base64 解密 zip 解压缩

*

*@authorken chen

* @date 2019年7月26日

*@parampath 解压文件夹路径 文件夹父文件可更改

*@parambase64 base64加密字符

*@returnBase64 压缩文件后进行base64编码

*@version1.0*/

public static void Base64ToFile(String base64, String path) throwsRuntimeException {

log.info("解压文件地址" +path);try{byte[] byteBase64 =Base64.getDecoder().decode(base64);

ByteArrayInputStream byteArray= newByteArrayInputStream(byteBase64);

ZipInputStream zipInput= newZipInputStream(byteArray);

ZipEntry entry=zipInput.getNextEntry();

File fout= null;

File file= newFile(path);

String parent=file.getParent();while (entry != null && !entry.isDirectory()) {

log.info("文件名称: [{}]", entry.getName());

fout= newFile(parent, entry.getName());if (!fout.exists()) {

(newFile(fout.getParent())).mkdirs();

}

BufferedOutputStream bos= new BufferedOutputStream(newFileOutputStream(fout));int offo = -1;byte[] buffer = new byte[BUFFER_SIZE];while ((offo = zipInput.read(buffer)) != -1) {

bos.write(buffer,0, offo);

}

bos.close();//获取 下一个文件

entry =zipInput.getNextEntry();

}

zipInput.close();

byteArray.close();

}catch(Exception e) {throw new RuntimeException("解压流出现异常", e);

}

}public static voidmain(String[] args) {//文件压缩

List fileList = new ArrayList();

fileList.add(new File("C:\\Users\\cjy\\Desktop\\qqq\\担保合同附件.pdf"));

fileList.add(new File("C:\\Users\\cjy\\Desktop\\qqq\\融资申请表.xls"));

fileList.add(new File("C:\\Users\\cjy\\Desktop\\qqq\\其他附件.jpg"));

String base64=ZipToBase64.toBase64Zip(fileList);

log.info("文件base 64 加密:" +base64);

ZipToBase64.Base64ToFile(base64,"C:\\Users\\cjy\\Desktop\\qqq\\新建文件夹\\a");

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值