package com.zbooksoft.gdmis.common; import com.ruili.wcp.configsettting.SystemConfig; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.nio.charset.Charset; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; /** * @Description: * @Author: zhai * @Date: 2024/9/22 **/ public class ZipUtil { private static final Logger logger = LoggerFactory.getLogger(ZipUtil.class); public static String zipFile(File path, String format) { String generatePath = ""; if (!path.exists()) { path.mkdirs(); } try { if (path.isDirectory()) { generatePath = path.getParent().endsWith("/") == false ? path.getParent() + File.separator + path.getName() + "." + format : path.getParent() + path.getName() + "." + format; } else { generatePath = path.getParent().endsWith("/") == false ? path.getParent() + File.separator : path.getParent(); generatePath += path.getName().substring(0, path.getName().lastIndexOf(".")) + "." + format; } // 输出流 FileOutputStream outputStream = new FileOutputStream(generatePath); // 压缩输出流 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream)); zip(out, path, ""); out.flush(); out.close(); return generatePath; } catch (Exception e) { return null; } finally { try { FileUtils.deleteDirectory(path); } catch (IOException e) { e.printStackTrace(); } } } private static void zip(ZipOutputStream output, File file, String childPath) throws Exception { FileInputStream input = null; try { // 文件为目录 if (file.isDirectory()) { // 得到当前目录里面的文件列表 File list[] = file.listFiles(); childPath = childPath + (childPath.length() == 0 ? "" : "/") + file.getName(); // 循环递归压缩每个文件 for (File f : list) { zip(output, f, childPath); } } else { // 压缩文件 childPath = (childPath.length() == 0 ? "" : childPath + "/") + file.getName(); output.putNextEntry(new ZipEntry(childPath)); input = new FileInputStream(file); int readLen = 0; byte[] buffer = new byte[1024 * 8]; while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) { output.write(buffer, 0, readLen); } } } catch (Exception ex) { ex.printStackTrace(); logger.info(ex.getMessage()); } finally { // 关闭流 if (input != null) { try { input.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } /** * 复制文件 * * @param srcPath 源文件绝对路径 * @param * @return boolean */ public static String copyFile(String srcPath, String destPath) { File srcFile = new File(srcPath); if (!srcFile.exists()) { // 源文件不存在 return "文件不存在!"; } // 获取待复制文件的文件名 File file = new File(srcPath); if (destPath.equals(srcPath)) { // 源文件路径和目标文件路径重复 return "文件路径重复!"; } File destFile = new File(destPath); if (destFile.exists() && destFile.isFile()) { // 该路径下已经有一个同名文件 return destPath + ",已经有一个同名文件!"; } String destDir = destFile.getParentFile().getPath(); File destFileDir = new File(destDir); destFileDir.mkdirs(); try { FileUtils.copyFile(file, destFile); return ""; } catch (IOException e) { logger.info(e.getMessage()); return "文件归档出错!"; } } public static void main(String[] args) { String paths = "D:\\RecordFile\\dataPack\\2024090006_20240927101254\\"; File path = new File(paths); if (!path.exists()) { path.mkdirs(); } boolean directory = path.isDirectory(); System.out.println(directory); } /** * 解压zip文件 * * @param multipartFile */ private String unzip(MultipartFile multipartFile, String dataSource) throws Exception { try { //metaBusActivity String fileName = multipartFile.getOriginalFilename(); SystemConfig config = SystemConfig.getInstance(); Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM"); SimpleDateFormat formatterYear = new SimpleDateFormat("yyyy"); String strDate = formatter.format(date); String formatYear = formatterYear.format(date); // 待接收 /部门编号/年度/年度月份/ String destDir = config.getAttachUploadPath() + "待接收" + File.separator + formatYear + File.separator + strDate + File.separator + dataSource; String filePath = destDir + File.separator + fileName.substring(0, fileName.indexOf('.')); File fileFolder = new File(destDir); // 存入文件夹 if (!fileFolder.exists() && !fileFolder.isDirectory()) { fileFolder.mkdirs(); } File targetFile = new File(fileFolder, fileName); multipartFile.transferTo(targetFile); ZipFile zp = null; //指定编码,否则压缩包里面不能有中文目录 zp = new ZipFile(targetFile, Charset.forName("gbk")); //遍历里面的文件及文件夹 Enumeration entries = zp.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); String zipEntryName = entry.getName(); InputStream in = zp.getInputStream(entry); String outpath = (filePath + File.separator + zipEntryName).replace("/", File.separator); //判断路径是否存在,不存在则创建文件路径 File file = new File(outpath.substring(0, outpath.lastIndexOf(File.separator))); if (!file.exists()) { file.mkdirs(); } //判断文件全路径是否为文件夹,如果是,不需要解压 if (new File(outpath).isDirectory()) { continue; } OutputStream out = new FileOutputStream(outpath); byte[] bf = new byte[2048]; int len; while ((len = in.read(bf)) > 0) { out.write(bf, 0, len); } in.close(); out.close(); } zp.close(); return filePath; } catch (Exception e) { throw new Exception("解压失败"); } } }