添加文件到Zip @param file 文件 @param out 输出 @param entryPath 路径
(File file, ZipArchiveOutputStream out, String entryPath)
| 303 | * @param entryPath 路径 |
| 304 | */ |
| 305 | private static void addFileToZip(File file, ZipArchiveOutputStream out, String entryPath) { |
| 306 | |
| 307 | String path=entryPath + file.getName(); |
| 308 | if(file.isDirectory()){ |
| 309 | path=formatDirPath(path); //为了在压缩文件中包含空文件夹 |
| 310 | } |
| 311 | ZipArchiveEntry entry = new ZipArchiveEntry(path); |
| 312 | entry.setTime(file.lastModified()); |
| 313 | // entry.setSize(files[i].length()); |
| 314 | |
| 315 | try { |
| 316 | |
| 317 | |
| 318 | out.putArchiveEntry(entry); |
| 319 | if(!file.isDirectory()){ |
| 320 | try (InputStream ins = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));) { |
| 321 | |
| 322 | IOUtils.copy(ins, out); |
| 323 | } |
| 324 | } |
| 325 | out.closeArchiveEntry(); |
| 326 | } catch (IOException e) { |
| 327 | // e.printStackTrace(); |
| 328 | if (logger.isErrorEnabled()) { |
| 329 | logger.error("添加文件到Zip--"+path,e); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | private static String formatDirPath(String dir){ |
| 335 | if(!dir.endsWith(File.separator)){ |
no test coverage detected