简体   繁体   中英

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. I want to update file1.txt file and add one file file3.txt iin zip file using core java concepts without extracting zip file. 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,

    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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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