简体   繁体   English

更新Zip文件或zip文件中的文件,而无需使用Java提取zip文件

[英]Update Zip file or files in zip file without extracting zip file using java

I have a zip file called test.zip in that zip file I have 2 files file1.txt and file2.txt. 我有一个名为test.zip的压缩文件,该压缩文件中有2个文件file1.txt和file2.txt。 I want to update file1.txt file and add one file file3.txt iin zip file using core java concepts without extracting zip file. 我想使用核心Java概念更新file1.txt文件并添加一个文件file3.txt iin zip文件,而不提取zip文件。 can anyone suggest which approach i should use for this use case. 谁能建议我针对该用例使用哪种方法。 thank you in advance. 先感谢您。

get the file on what you want to perform operation, 获取要执行操作的文件,

     try (FileInputStream fis = new FileInputStream( String fileName = "C:/Users/Administrator/Desktop/log.zip";);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    ZipInputStream zis = new ZipInputStream(bis)) {

                ZipEntry ze;

                while ((ze = zis.getNextEntry()) != null) {

                    System.out.format("File: %s Size: %d Last Modified %s %n",
                            ze.getName(), ze.getSize(),
                            LocalDate.ofEpochDay(ze.getTime() / MILLS_IN_DAY));
                }
            }   

Then get bufferreader object like as below, 然后获取如下的bufferreader对象,

    ZipEntry entry = entries.nextElement();
    InputStream stream = zipFile.getInputStream(entry);
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));

Once you will have it, You can modify content as, 拥有后,您可以将内容修改为

    while (line = br.readLine() != null)
        {
            if (line.contains("java"))
                line = line.replace("java", " ");
            lines.add(line);
        }
    FileWriter fw = new FileWriter(f1);
    BufferedWriter out = new BufferedWriter(fw);
    out.write(lines.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM