package com.zbooksoft.gdmis.common; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.zip.ZipEntry; 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); } }