Java 无需解压直接读取ZIP压缩包里的文件及内容

最近开发的时候遇到要获取到zip压缩包里面的文件内容,一开始的想法是先通过代码执行解压,然后读取文件内容,但是感觉好麻烦,于是度了一下,发现可以无需解压直接读取,而且还是JDK提供给我们的工具。

解决方案就是通过ZipInputStream来读取。
ZipInputStream在JDK中的util包中,而我们平时用的FileInputStream等都是在io包中的。

示例如下:

@Test
    public void test() throws Exception {
        //获取文件输入流
        FileInputStream input = new FileInputStream("D:\\2022-03-28.zip");

        //获取ZIP输入流(一定要指定字符集Charset.forName("GBK")否则会报java.lang.IllegalArgumentException: MALFORMED)
        ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(input), Charset.forName("GBK"));

        //定义ZipEntry置为null,避免由于重复调用zipInputStream.getNextEntry造成的不必要的问题
        ZipEntry ze;
        List<List<Object>> list;
        //循环遍历
        while ((ze = zipInputStream.getNextEntry()) != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            if (!ze.isDirectory() && ze.toString().endsWith("xls")) {
                //读取
                byte[] buffer = new byte[1024];
                int len;
                while ((len = zipInputStream.read(buffer)) > -1) {
                    baos.write(buffer, 0, len);
                }
                baos.flush();
                InputStream stream = new ByteArrayInputStream(baos.toByteArray()); //excel 流
                //根据excel输入流读取EXCEL中的数据
                ExcelReader excelReader = ExcelUtil.getReader(stream);
                list =  excelReader.read(2, excelReader.getRowCount());
                for(List<Object> objList : list){
                 objList.get(0);
                 objList.get(1); 
                 //获取到数据进行相关处理
                 ......
                }
            }
        }
        //一定记得关闭流
        zipInputStream.closeEntry();
        input.close();
    }
  • 5
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Java提供了ZipInputStream和ZipOutputStream两个类来进行zip文件读取和写入操作,可以不需要解压实现替换zip压缩包文件夹中的指定文件。以下是实现的步骤: 1. 创建ZipInputStream对象,读取zip文件中的内容。 2. 创建ZipOutputStream对象,向zip文件中写入内容。 3. 遍历ZipInputStream中的每一个ZipEntry,如果是要替换的文件,则跳过,否则将其写入ZipOutputStream中。 4. 将要替换的文件写入ZipOutputStream中。 5. 关闭ZipInputStream和ZipOutputStream对象。 以下是一个代码示例: ```java import java.io.*; import java.util.zip.*; public class ReplaceFileInZip { public static void main(String[] args) throws IOException { String zipFilePath = "test.zip"; String fileNameToReplace = "test.txt"; String replacementFilePath = "replacement.txt"; // 创建ZipInputStream对象,读取zip文件中的内容 ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath)); // 创建ZipOutputStream对象,向zip文件中写入内容 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("temp.zip")); // 遍历ZipInputStream中的每一个ZipEntry,如果是要替换的文件,则跳过,否则将其写入ZipOutputStream中 ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (entry.getName().equals(fileNameToReplace)) { continue; // 不写入要替换的文件 } zos.putNextEntry(new ZipEntry(entry.getName())); byte[] buffer = new byte[1024]; int len; while ((len = zis.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); } // 将要替换的文件写入ZipOutputStream中 ZipEntry replacementEntry = new ZipEntry(fileNameToReplace); zos.putNextEntry(replacementEntry); FileInputStream fis = new FileInputStream(replacementFilePath); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } fis.close(); zos.closeEntry(); // 关闭ZipInputStream和ZipOutputStream对象 zis.close(); zos.close(); // 删除原来的zip文件并将新的temp.zip文件重命名为原来的文件名 new File(zipFilePath).delete(); new File("temp.zip").renameTo(new File(zipFilePath)); } } ``` 在上面的示例中,我们将test.zip文件中的test.txt文件替换为replacement.txt文件。注意,这个示例只是演示了如何进行zip文件中指定文件的替换,实际应用中还需要考虑异常处理、路径处理等问题。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值