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

最近在做的功能模块有遇到下载ZIP文件里的文件内容,一开始的想法是先通过代码执行解压,然后读取文件内容,但是感觉好麻烦,于是度了一下,发现可以无需解压直接读取,而且还是JDK提供给我们的工具。(((o(*゚▽゚*)o)))

解决方案就是通过ZipInputStream来读取

ZipInputStream在JDK中的util包中,而我们平时用的FileInputStream等都是在io包中的。

使用方法如代码所示:

public static void main(String[] args) throws IOException {

   //获取文件输入流
   FileInputStream input = new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test.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 = null;

   //循环遍历
   while ((ze = zipInputStream.getNextEntry()) != null) {

       System.out.println("文件名:" + ze.getName() + " 文件大小:" + ze.getSize() + " bytes");
       System.out.println("文件内容:");

       //读取
       BufferedReader br = new BufferedReader(new InputStreamReader(zipInputStream,Charset.forName("GBK")));

       String line;

       //内容不为空,输出
       while ((line = br.readLine()) != null) {
           System.out.println(line);
       }
   }

   //一定记得关闭流
   zipInputStream.closeEntry();
   input.close();
}


控制台输出:

文件名:文件1.txt 文件大小:25 bytes
文件内容:
213123
edqfdqf
er2523
文件名:文件2.txt 文件大小:14 bytes
文件内容:
rfq
e
fqefer

我真实使用场景是在读取华为云对象存储服务中Bucket中指定.ZIP文件中包含的全部文件内容,代码如下:

华为云官方文档中给的示例并不能满足我们下载.ZIP文件,所以有需要的朋友可以参考。

public static void main(String[] args) throws IOException {

        //华为云对象存储认证对象
        final ObsClient obsClient = new ObsClient(ak, sk, endPoint);
        //读取对象存储中指定Bucket中的指定文件(test123为Bucket名称、File.zip为文件名称)
        ObsObject obsObject = obsClient.getObject("test23", "File.zip");
        InputStream input = obsObject.getObjectContent();

        System.out.println("Object content:");
        ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(input));

        //定义ZipEntry,避免由于重复调用zipInputStream.getNextEntry造成的不必要的问题
        ZipEntry ze = null;
        while ((ze = zipInputStream.getNextEntry()) != null) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(zipInputStream));
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
        }
        //一定记得关闭流
        zipInputStream.closeEntry();
        input.close();
    }

读取数据成功

  • 16
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
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文件中指定文件的替换,实际应用中还需要考虑异常处理、路径处理等问题。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值