R TheWriter
R TheWriter

Reputation: 163

How to create ZIP file inside a ZIP file in Java

I am trying to create a ZIP file inside a ZIP file to re-construct a previously in memory zip structure I got in Java.

I am failing since I get an error on the inner ZIP created inside the initial ZIP file. The file is corrupted. I get an "unexpected ending of file" when trying to open it.

I got this structure:

-input.zip --innerInput.zip

The Code unzip it all in memory using a java Stack and a Map. Then it creates input2.zip, with innerInput.zip in it.

Summary: I need to create a ZIP with a ZIP in it, all in memory (not saving on disk temporarily).

CODE:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.lang3.StringUtils;

public class ZipHandler1 {

    private static final int BUFFER_SIZE = 2048;

    private static final String ZIP_EXTENSION = ".zip";
    public static final Integer FOLDER = 1;
    public static final Integer ZIP = 2;
    public static final Integer FILE = 3;


    public static Deque<Map<Integer, Object[]>> unzip(ByteArrayOutputStream zippedOutputFile) {

        try {

            ZipInputStream inputStream = new ZipInputStream(
          new BufferedInputStream(new ByteArrayInputStream(
              zippedOutputFile.toByteArray())));

            ZipEntry entry;

            Deque<Map<Integer, Object[]>> result = new ArrayDeque<Map<Integer, Object[]>>();

            while ((entry = inputStream.getNextEntry()) != null) {

                LinkedHashMap<Integer, Object[]> map = new LinkedHashMap<Integer, Object[]>();
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                System.out.println("\tExtracting entry: " + entry);
                int count;
                byte[] data = new byte[BUFFER_SIZE];
                if (!entry.isDirectory()) {
                    BufferedOutputStream out = new BufferedOutputStream(
          outputStream, BUFFER_SIZE);

                while ((count = inputStream.read(data, 0, BUFFER_SIZE)) != -1) {
                    out.write(data, 0, count);
                }

                out.flush();
                out.close();

                //  recursively unzip files
                if (entry.getName().toUpperCase().endsWith(ZIP_EXTENSION.toUpperCase())) {
                map.put(ZIP, new Object[] {entry.getName(), unzip(outputStream)});
                result.add(map);

                } else { 
                    map.put(FILE, new Object[] {entry.getName(), outputStream});
                    result.add(map);
                }
                } else {
                    map.put(FOLDER, new Object[] {entry.getName(), 
                    unzip(outputStream)});
                    result.add(map);
                }
            }

        inputStream.close();

        return result;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

package course.hernan;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;

public class FileReader {

  private static final int  BUFFER_SIZE = 2048;

  public static void main(String[] args) {

    try {
      File f = new File("DIR/inputs.zip");
      FileInputStream fis = new FileInputStream(f);
      BufferedInputStream bis = new BufferedInputStream(fis);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      BufferedOutputStream bos = new BufferedOutputStream(baos);
      byte[] buffer = new byte[BUFFER_SIZE];

      while (bis.read(buffer, 0, BUFFER_SIZE) != -1) {
        bos.write(buffer);
      }

      bos.flush();
      bos.close();
      bis.close();

      Deque<Map<Integer, Object[]>> outputDataStack = ZipHandler1.unzip(baos);

      //Output file
      File fout = new File("DIR/inputs2.zip");

      ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(fout)));

      processZip(outputDataStack, zipOutput);
      zipOutput.close();

    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
}

private static final void processZip(Deque<Map<Integer, Object[]>> outputDataStack, 
  ZipOutputStream zipOutput) throws IOException {

while (!outputDataStack.isEmpty()) {
    Map<Integer, Object[]> map = outputDataStack.pop();

    for (Map.Entry<Integer, Object[]> entry : map.entrySet()) {
        System.out.println("KEY:" + entry.getKey());
        Object[] values = entry.getValue();
        String entryName = (String)values[0];

        if (entry.getKey().equals(ZipHandler1.FILE)) {
            System.out.println("..........................");
            System.out.println("type: FILE");
            System.out.println("Name: " + entryName);

            zipOutput.putNextEntry(new ZipEntry(entryName));
            byte[] outputByteArray = ((ByteArrayOutputStream)values[1]).toByteArray();

            IOUtils.write(outputByteArray, zipOutput);
            zipOutput.closeEntry();
            ((ByteArrayOutputStream)values[1]).close();

        } else if (entry.getKey().equals(ZipHandler1.FOLDER)) {
            System.out.println("..........................");
            System.out.println("type: FOLDER");
            System.out.println("Name: " + entryName);

            zipOutput.putNextEntry(new ZipEntry(entryName));
            System.out.println("..........................");
            zipOutput.closeEntry();

        } else if (entry.getKey().equals(ZipHandler1.ZIP)) {
            System.out.println("..........................");
            System.out.println("type: ZIP");
            System.out.println("Name: " + entryName);

            zipOutput.putNextEntry(new ZipEntry(entryName));

            ByteArrayOutputStream innerZipByteArray = new ByteArrayOutputStream(BUFFER_SIZE);
            ZipOutputStream innerZipOutput = new ZipOutputStream(
          new BufferedOutputStream(innerZipByteArray));

            processZip((Deque<Map<Integer,Object[]>>)values[1], innerZipOutput);
            innerZipOutput.flush();
            IOUtils.write(zzzz.toByteArray(), zipOutput);
            innerZipOutput.close();
            zipOutput.closeEntry();
            System.out.println("..........................");
        }
            System.out.println("..........................");
            zipOutput.closeEntry();
        }
    }
}

Upvotes: 3

Views: 3385

Answers (1)

R TheWriter
R TheWriter

Reputation: 163

Thanks to me!

The answer was pretty hard for me to find, maybe obvious to others.

short answer: I WAS CLOSING THE INNER ZIP STREAM TOO SOON

Ok, the problem is storing a ZIP within a ZIP right? Well, I was getting an error because the inner zip was closed too soon

Solution:

Create inner zip file like this: ByteArrayOutputStream innerZipBufferOutput = new ByteArrayOutputStream(BUFFER_SIZE); ZipOutputStream innerZipOutput = new ZipOutputStream( new BufferedOutputStream(innerZipBufferOutput));

Write bytes (info) to the inner zip

close inner zip Stream innerZipOutput.close(); **THIS I was closing at the wrong time*

At this point data in form of bytes is in innerZipBufferOutput

Set entry in outer zip file for the inner zip file

ZipEntry newEntry = new ZipEntry(entryName);
zipOutput.putNextEntry(newEntry);  

Write the inner zip into the outer zip

zipOutput.write(innerZipBufferOutputByteArray);

Close outer zip

zipOutput.flush();
zipOutput.closeEntry();

Voila!

Example (code extract) -- you have a ZipOutputStream (outer zip file)

ByteArrayOutputStream innerZipBufferOutput = new ByteArrayOutputStream(BUFFER_SIZE);
ZipOutputStream innerZipOutput = new ZipOutputStream(new BufferedOutputStream(innerZipBufferOutput)); 

<<process - e.g. create your inner zip file>>

innerZipOutput.flush();
innerZipOutput.close();

ZipEntry newEntry = new ZipEntry(<<your entry name>>); 

zipOutput.setMethod(ZipOutputStream.STORED);

byte[] innerZipBufferOutputByteArray = innerZipBufferOutput.toByteArray();

//Create the nested ZIP inside the outer ZIP 
ZipEntry newEntry = new ZipEntry(entryName); 
zipOutput.putNextEntry(newEntry);

zipOutput.write(innerZipBufferOutputByteArray);


zipOutput.closeEntry();
zipOutput.close();

Upvotes: 4

Related Questions