java 直接读取zip文件和文件内容

不解压zip文件,直接读取zip包内的文件夹以及文件内容
zip包内内容:

这里写图片描述

代码如下:

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class ReadFile {
    public static void main(String[] args) throws IOException {
        String path = "F:\*******\201707\78641695079026649.zip";
        ZipFile zf = new ZipFile(path);
        InputStream in = new BufferedInputStream(new FileInputStream(path));
        Charset gbk = Charset.forName("gbk");
        ZipInputStream zin = new ZipInputStream(in,gbk);
        ZipEntry ze;
        while((ze = zin.getNextEntry()) != null){
            if(ze.toString().endsWith("txt")){
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(zf.getInputStream(ze)));
                String line;
                while((line = br.readLine()) != null){
                    System.out.println(line.toString());
                }
                br.close();
            }
            System.out.println();
        }
        zin.closeEntry();
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Java 中替换 zip 文件中的文件,可以使用以下步骤: 1. 使用 `java.util.zip.ZipFile` 类打开 zip 文件,获取其中的所有条目(entry)。 2. 遍历所有条目,找到要替换的文件。 3. 打开要替换的文件,并将其内容读入到一个字节数组中。 4. 使用 `java.util.zip.ZipOutputStream` 类创建一个新的 zip 文件,并将原始 zip 文件中的所有条目复制到新文件中。 5. 在新文件中添加要替换的文件条目,将其内容设置为步骤 3 中读取的字节数组。 6. 关闭原始 zip 文件和新的 zip 文件。 以下是一个示例代码: ```java import java.io.*; import java.util.zip.*; public class ZipReplaceFileExample { public static void main(String[] args) throws IOException { String zipFilePath = "example.zip"; String targetFileName = "file.txt"; String replacementFilePath = "replacement.txt"; // Open the original zip file ZipFile zipFile = new ZipFile(zipFilePath); // Create a temporary file for the new zip file File tempFile = File.createTempFile("temp", ".zip"); tempFile.deleteOnExit(); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(tempFile)); // Iterate over all the entries in the original zip file for (ZipEntry entry : (Iterable<ZipEntry>) zipFile.entries()) { // Copy the entry to the new zip file zipOut.putNextEntry(entry); // If this is the file we want to replace, copy the replacement file's content instead if (entry.getName().equals(targetFileName)) { FileInputStream replacementIn = new FileInputStream(replacementFilePath); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = replacementIn.read(buffer)) > 0) { zipOut.write(buffer, 0, bytesRead); } replacementIn.close(); } else { // Otherwise, just copy the original entry's content InputStream in = zipFile.getInputStream(entry); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) > 0) { zipOut.write(buffer, 0, bytesRead); } in.close(); } // Close the entry zipOut.closeEntry(); } // Close the original zip file and the new zip file zipFile.close(); zipOut.close(); // Replace the original zip file with the new one File originalFile = new File(zipFilePath); tempFile.renameTo(originalFile); } } ``` 请注意,此示例代码假设要替换的文件和替换文件都在当前目录中。也要注意,这种方法只适用于替换单个文件,如果需要替换多个文件,则需要稍作修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值