public File zipAttachFile(String filePathDir, List<String> urlFileList, String memberId) throws Exception {
filePathDir = "/home/file"; File zipFileDir = new File(filePathDir); if (!zipFileDir.exists()) { zipFileDir.mkdir(); } List<File> fileList = new ArrayList<>(); for (String fileUrl : urlFileList) { int lastIndexOf = fileUrl.lastIndexOf("/"); String fileName = fileUrl.substring(lastIndexOf + 1); File file = new File(filePathDir + fileName); URL url = new URL(fileUrl); FileUtils.copyURLToFile(url, file); fileList.add(file); } String filePath = filePathDir + transToTrilateralMemberId(olsConfigProperites.getOrderPre(), memberId) + ".zip"; File zipFile = new File(filePath); byte[] buf = new byte[1024]; try { // ZipOutputStream类:完成文件或文件夹的压缩 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); for (File file : fileList) { FileInputStream in = new FileInputStream(file); out.putNextEntry(new ZipEntry(file.getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); } catch (Exception e) { log.error("文件压缩失败" + e); } return zipFile; }
标签:java,File,zip,url,filePathDir,String,file,new,out From: https://www.cnblogs.com/stromgao/p/17333237.html