0

I want somehow to add files into inner zip entry. Following structure of zip file looks like this:

input-zip-file

|-- directory/
|   |-- zipfile.zip - 
|   |   |-- files.txt - *How can i get these entries to my zipfile.zip*
|   |   |-- files.txt

It's only small piece of zip file. I walking through zip file and check if entry has more files inside. What I've done so far

snippet

private void handleZipFile(ZipFile input) throws IOException {
    input.stream()
            .forEach(entry -> {
                        try {

                            InputStream in = input.getInputStream(entry);

                            ZipInputStream zis = new ZipInputStream(in);
                            zos.putNextEntry(new ZipEntry(entry.getName()));

                            handleInnerZipEntry(entryName, zis, zos);

                            zos.closeEntry();

                            in.close();
                            zis.close();
                            zos.flush();
                        } catch (IOException e) {
                            e.printStackTrace());
                        }
                    }
            );
    zos.close();
} 

handleInnerZip

private void handleInnerZipEntry(String entryPath, ZipInputStream in, ZipOutputStream out) throws IOException {

    try {
        ZipEntry entry = in.getNextEntry();
        while (entry != null) {
            out.putNextEntry(new ZipEntry(entry.getName())); // Need to put files.txt files

            if (entry.getSize() > 0 && entry.getName().endsWith("zip")) {

                ZipInputStream recursiveIn = new ZipInputStream(in);
                ZipOutputStream recursiveOut = new ZipOutputStream(out);

                handleInnerZipEntry(entryPath, recursiveIn, recursiveOut);

                recursiveOut.flush();

            } else if (entry.getSize() > 0 && !(entry.isDirectory())) {

            }
            in.closeEntry();
            entry = in.getNextEntry();
        }

    } catch (IOException e) {
        CustomLogger.error("IOException: " + e.getLocalizedMessage());
    }
}
mjagic
  • 45
  • 1
  • 9

1 Answers1

0

The problem is (if I understand correctly) that the Zip format does not allow, what you want to achieve (cf. https://en.wikipedia.org/wiki/Zip_(file_format)#Structure ).

You will have to create a new zip file with the augmented content. Also, possible duplicate of this SO.

D. Kovács
  • 1,232
  • 13
  • 25
  • That's what I'm trying to do. I'm creating a new zip file, but I can't add the following zip entry correctly inside zip (zipfile.zip -> files.txt). What happening here is that it creates entry inside root directory with the name /files.txt – mjagic Mar 13 '18 at 12:06