package com.vci.ubcs.file.util; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.vci.ubcs.file.constant.VciFileLangCodeConstant; import com.vci.ubcs.file.properties.VciFileTransProperties; import com.vci.ubcs.starter.exception.VciBaseException; import com.vci.ubcs.starter.web.util.LangBaseUtil; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; /** * zip相关的工具类 * @author weidy * @date 2020/3/12 */ @Component public class VciZipUtil { /** * 文件传输配置 */ @Autowired(required = false) private VciFileTransProperties transProperties; /** * 获取zip的字符集 * @return 默认为GBK,否则需要配置 */ private String getZipCharset(){ return (transProperties == null || StringUtils.isBlank(transProperties.getZipCharset()))?"GBK":transProperties.getZipCharset(); } /** * 将文件夹压缩成压缩包 * @param folder 文件夹路径 * @param zipFileName zip文件名 * @throws VciBaseException 参数为空或者添加出错的会抛出异常 */ public void folderToZipFile(String folder, String zipFileName) throws VciBaseException { folderToZipFile(folder,zipFileName,getZipCharset()); } /** * 将文件夹压缩成压缩包 * @param folder 文件夹路径 * @param zipFileName zip文件名 * @param zipEncode zip的字符集,windows应该为GBK * @throws VciBaseException 参数为空或者添加出错的会抛出异常 */ public void folderToZipFile(String folder, String zipFileName,String zipEncode) throws VciBaseException{ File sourceFile = new File(folder); if(!sourceFile.exists()){ throw new VciBaseException(VciFileLangCodeConstant.FILE_NOT_FOUND,new String[]{folder}); } ZipOutputStream zos = null; try { zos = new ZipOutputStream(new FileOutputStream(zipFileName)); zos.setEncoding(zipEncode); //默认是UTF-8格式的 addFileToZip(zos, new File(folder).listFiles(),folder + File.separator); }catch (Throwable e){ throw new VciBaseException(LangBaseUtil.getErrorMsg(e),new String[]{}); }finally { try{ if(zos!=null){ zos.close(); } }catch (Exception e){ } } } /** * 将文件夹压缩成压缩包 * @param zos zip输出流 * @param files 要压缩的文件 * @param rootPath 压缩的文件中的顶层目录 * @throws VciBaseException 文件不存在,或者内容出现错误会抛出异常 */ private void addFileToZip(ZipOutputStream zos,File[] files,String rootPath) throws VciBaseException{ if(files!=null && files.length>0) { for (File file : files) { if(StringUtils.isBlank(rootPath)){ try { rootPath = file.getParent(); if (rootPath == null) { rootPath = file.getPath(); } }catch (Throwable e){ rootPath = file.getPath(); } } if (!file.exists()) { throw new VciBaseException(VciFileLangCodeConstant.FILE_NOT_FOUND,new String[]{file.getAbsolutePath()}); } if (file.isDirectory()) { addFileToZip(zos, file.listFiles(),rootPath); }else{ InputStream is = null; try { is = new FileInputStream(file); String entryName = file.getAbsolutePath().replace(rootPath,""); if(entryName.startsWith("\\")) { entryName = entryName.substring(1); } ZipEntry zipEntry = new ZipEntry(entryName); zos.putNextEntry(zipEntry); byte[] b = new byte[100]; int length = 0; while ((length = is.read(b)) != -1) { zos.write(b, 0, length); } } catch (Exception e) { throw new VciBaseException(VciFileLangCodeConstant.ZIP_FILE_ERROR,new String[]{file.getAbsolutePath()},e); } finally { try { IOUtils.closeQuietly(is); } catch (Exception e) { } } } } } } /** * 将单个文件添加到zip中 * @param file 文件 * @param zipFile zip文件 * @throws VciBaseException 参数为空或者添加出错的会抛出异常 */ public void addFileToZip(File file, String zipFile) throws VciBaseException{ if(file == null || !file.exists() ){ throw new VciBaseException(VciFileLangCodeConstant.FILE_NOT_FOUND ,new String[]{file.getAbsolutePath()}); } if(StringUtils.isBlank(zipFile)){ throw new VciBaseException(VciFileLangCodeConstant.ZIP_FILE_NAME_NULL,new String[0]); } addFileToZip(new File[]{file},zipFile); } /** * 将多个个文件添加到zip中 * @param files 文件 * @param zipFile zip文件 * @throws VciBaseException 参数为空或者添加出错的会抛出异常 */ public void addFileToZip(File[] files, String zipFile) throws VciBaseException{ addFileToZip(files, zipFile,getZipCharset()); } /** * 将多个个文件添加到zip中 * @param files 文件 * @param zipFile zip文件 * @param zipEncode zip的字符集 * @throws VciBaseException 参数为空或者添加出错的会抛出异常 */ public void addFileToZip(File[] files, String zipFile,String zipEncode) throws VciBaseException{ if(files == null || files.length == 0){ throw new VciBaseException(VciFileLangCodeConstant.FILES_NULL,new String[0]); } if(StringUtils.isBlank(zipFile)){ throw new VciBaseException(VciFileLangCodeConstant.ZIP_FILE_NAME_NULL,new String[0]); } ZipOutputStream zos = null; try { zos = new ZipOutputStream(new FileOutputStream(zipFile)); zos.setEncoding(zipEncode); addFileToZip(zos, files,""); }catch (Throwable e){ throw new VciBaseException(LangBaseUtil.getErrorMsg(e),new String[]{},e); }finally { try{ if(zos!=null){ zos.close(); } }catch (Exception e){ } } } }