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");
}
}