Java文件压缩工具类:ZipUtil.java
问题记录时间:2023.10.09
整理博客时间:2024.01.13
import java.io.*; import java.net.URL; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtil { public static ByteArrayOutputStream downloadAndZip(List<Map<String, String>> fileInfoList) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(byteArrayOutputStream); for (final Map<String, String> fileInfo : fileInfoList) { String url = fileInfo.get("url"); String fileName = fileInfo.get("fileName"); URL downloadUrl = new URL(url); ZipEntry zipEntry = new ZipEntry(fileName); zos.putNextEntry(zipEntry); try (InputStream is = downloadUrl.openStream()) { byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { zos.write(buffer, 0, len); } } zos.closeEntry(); } zos.close(); return byteArrayOutputStream; } public static void saveToFile(ByteArrayOutputStream byteArrayOutputStream, String filePath) throws IOException { File file = new File(filePath); try (FileOutputStream fos = new FileOutputStream(file)) { byteArrayOutputStream.writeTo(fos); } } public static void main(String[] args) throws Exception { LinkedList<Map<String, String>> s = new LinkedList<>(); HashMap<String, String> file1 = new HashMap<>(); file1.put("url", "https://img1.baidu.com/it/u=3007048469,3759326707&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1"); file1.put("fileName", "图片1.png"); s.add(file1); HashMap<String, String> file2 = new HashMap<>(); file2.put("url", "http://csi.hubu.edu.cn/fujian1-zhongguojisuanjixuehuituijianguojixueshuhuiyiheqikanmulu-2022nian.pdf"); file2.put("fileName", "folder/pdf2.pdf"); s.add(file2); ByteArrayOutputStream outputStream = downloadAndZip(s); saveToFile(outputStream, "D:/test.zip"); } }
添加文件名重复校验(重复文件名在后面加 (n))
import java.io.*; import java.net.URL; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtil { private static String duplicateFilenameDetect(HashMap<String, Integer> map, String filename) { Integer count = map.getOrDefault(filename, 0); map.put(filename, count + 1); if (count > 0) { String suffix = "(" + (count + 1) + ")"; int lastDotIndex = filename.lastIndexOf('.'); if (lastDotIndex > 0) { String prefix = filename.substring(0, lastDotIndex); String postfix = filename.substring(lastDotIndex); return prefix + suffix + postfix; } else { return filename + suffix; } } else { return filename; } } public static ByteArrayOutputStream downloadAndZip(List<Map<String, String>> fileInfoList) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(byteArrayOutputStream); HashMap<String, Integer> duplicateFilenameDetectionMap = new HashMap<>(); for (final Map<String, String> fileInfo : fileInfoList) { String url = fileInfo.get("url"); String rawFileName = fileInfo.get("fileName"); String fileName = duplicateFilenameDetect(duplicateFilenameDetectionMap, rawFileName); URL downloadUrl = new URL(url); ZipEntry zipEntry = new ZipEntry(fileName); zos.putNextEntry(zipEntry); try (InputStream is = downloadUrl.openStream()) { byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { zos.write(buffer, 0, len); } } zos.closeEntry(); } zos.close(); return byteArrayOutputStream; } public static void saveToFile(ByteArrayOutputStream byteArrayOutputStream, String filePath) throws IOException { File file = new File(filePath); try (FileOutputStream fos = new FileOutputStream(file)) { byteArrayOutputStream.writeTo(fos); } } public static void main(String[] args) throws Exception { LinkedList<Map<String, String>> s = new LinkedList<>(); HashMap<String, String> file1 = new HashMap<>(); file1.put("url", "https://img1.baidu.com/it/u=3007048469,3759326707&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1"); file1.put("fileName", "图片1.png"); s.add(file1); HashMap<String, String> file2 = new HashMap<>(); file2.put("url", "http://csi.hubu.edu.cn/fujian1-zhongguojisuanjixuehuituijianguojixueshuhuiyiheqikanmulu-2022nian.pdf"); file2.put("fileName", "folder/pdf2.pdf"); s.add(file2); // 验证重名加 (n) 逻辑 HashMap<String, String> file3 = new HashMap<>(); file3.put("url", "https://img0.baidu.com/it/u=3182669744,3015526205&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1"); file3.put("fileName", "图片1.png"); s.add(file3); ByteArrayOutputStream outputStream = downloadAndZip(s); saveToFile(outputStream, "D:/test.zip"); } }
本站文章除注明转载/出处外,均为原创,若要转载请务必注明出处。转载后请将转载链接通过邮件告知我站,谢谢合作。本站邮箱:admin@only4.work
尊重他人劳动成果,共创和谐网络环境。点击版权声明查看本站相关条款。