Java zip解压及读取
解压读取
import com.alibaba.fastjson.JSONObject; import java.io.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Decompressing { public static void zipUncompress(String inputFile) throws Exception { File srcFile = new File(inputFile); // 判断源文件是否存在 if (!srcFile.exists()) { throw new Exception(srcFile.getPath() + "所指文件不存在"); } String destDirPath = inputFile.replace(".zip", ""); //创建压缩文件对象 ZipFile zipFile = new ZipFile(srcFile); //开始解压 Enumeration<?> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); // 如果是文件夹,就创建个文件夹 if (entry.isDirectory()) { srcFile.mkdirs(); } else { // 如果是文件,就先创建一个文件,然后用io流把内容copy过去 File targetFile = new File(destDirPath + "/" + entry.getName()); // 保证这个文件的父文件夹必须要存在 if (!targetFile.getParentFile().exists()) { targetFile.getParentFile().mkdirs(); } targetFile.createNewFile(); // 将压缩文件内容写入到这个文件中 InputStream is = zipFile.getInputStream(entry); FileOutputStream fos = new FileOutputStream(targetFile); int len; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } // 关流顺序,先打开的后关闭 fos.close(); is.close(); } } } public static void readFiles(String inputFile) throws Exception { File srcFile = new File(inputFile); if (srcFile.isDirectory()) { File next[] = srcFile.listFiles(); for (int i = 0; i < next.length; i++) { System.out.println(next[i].getName()); if(!next[i].isDirectory()){ BufferedReader br = new BufferedReader(new FileReader(next[i])); List<String> arr1 = new ArrayList<>(); String contentLine ; while ((contentLine = br.readLine()) != null) { JSONObject js = JSONObject.parseObject(contentLine); arr1.add(contentLine); } System.out.println(arr1); } } } } public static void main(String[] args) { try { String path = "E:\\data\\test\\aaa.zip"; zipUncompress(path); readFiles(path.replace(".zip", "")); } catch (Exception e) { e.printStackTrace(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2016-10-12 ob_start()
2015-10-12 RBAC(基于角色的访问控制权限)表结构