Java 复制zip文件到指定目录并解压zip文件

xiaoxiao2025-08-03  29

过程中使用apache的ant jar包 apache-ant-1.8.2.jar 

示例代码如下:

import org.apache.log4j.Logger; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class FileHelpUtils { /** The Constant LOG. */ private static final Logger LOG = Logger.getLogger(FileHelpUtils.class); // 拷贝文件到指定目录 public static void backupFile(final File srcFile, String copyPath ) { if (srcFile == null) { LOG.error("backSuccess process, source is null!"); return; } File typeFolder = new File(copyPath); if (typeFolder.exists()) { LOG.info(" check folder exists! (" + copyPath + ")"); }else{ LOG.info(" create folder (" + copyPath + ")..."); typeFolder.mkdir(); LOG.info(" create folder (" + copyPath + ") end, check exists:" + typeFolder.exists()); } //Folder: date String version = new SimpleDateFormat("yyyyMMdd").format(new Date()); final String strDestDirectory = copyPath + File.separator +version; final File destFolder = new File(strDestDirectory); if (destFolder.exists()) { LOG.info(" check folder exists! (" + strDestDirectory + ") "); }else{ LOG.info(" create folder (" + strDestDirectory + ")..."); destFolder.mkdir(); LOG.info(" create folder (" + strDestDirectory + ") end, check exists:" + destFolder.exists()); } //file name final String fileName = srcFile.getName(); //{destDirectory}/{fileName} final String destFilePath = strDestDirectory + File.separator + fileName; // Move file final File destFile = new File(destFilePath); if (destFile.exists()) { LOG.info(" Delete old file : " + destFilePath + "!"); destFile.delete(); } // NEW strategy to avoid move file failure reNameFile(srcFile, destFile); } public static boolean reNameFile(File oldname, File newname){ try{ // copy file org.apache.commons.io.FileUtils.copyFile(oldname, newname); LOG.info(" [OK] Copy file from " + oldname.getPath() + " ----> " + newname + " !"); // delete file if(oldname.delete()){ LOG.info(" [OK] Delete source file:" + oldname.getPath() + " !"); } }catch(IOException ioex){ LOG.error(" [KO] Copy file path: " + oldname.getPath() + ". error:" + ioex.getMessage()); return false; } return true; } }

 

/** * 解压zip */ public static void decompress(String srcPath) throws Exception { final File folderToScan = new File(srcPath); final File[] targetFiles = folderToScan.listFiles(); for (File file : targetFiles) { if(!file.getName().endsWith("zip")){ continue; } ZipFile zf = new ZipFile(file); Enumeration entries = zf.getEntries(); ZipEntry entry = null; while (entries.hasMoreElements()) { entry = (ZipEntry) entries.nextElement(); LOG.info("compress the file: " + entry.getName()); // 表示文件 File f = new File(srcPath+File.separator+entry.getName()); if (!f.exists()) { File parentDir = new File(srcPath); parentDir.mkdirs(); } f.createNewFile(); // 将压缩文件内容写入到这个文件中 InputStream is = zf.getInputStream(entry); FileOutputStream fos = new FileOutputStream(f); int count; //一次写入8192B byte[] buf = new byte[8192]; while ((count = is.read(buf)) != -1) { fos.write(buf, 0, count); } is.close(); fos.close(); } } }
转载请注明原文地址: https://www.6miu.com/read-5034213.html

最新回复(0)