18

I am trying to convert an array of bytes into a ZIP file. I got bytes using the following code:

byte[] originalContentBytes= new Verification().readBytesFromAFile(new File("E://file.zip"));

private byte[] readBytesFromAFile(File file) {
    int start = 0;
    int length = 1024;
    int offset = -1;
    byte[] buffer = new byte[length];
    try {
        //convert the file content into a byte array
        FileInputStream fileInuptStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(
                fileInuptStream);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        while ((offset = bufferedInputStream.read(buffer, start, length)) != -1) {
            byteArrayOutputStream.write(buffer, start, offset);
        }

        bufferedInputStream.close();
        byteArrayOutputStream.flush();
        buffer = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
    } catch (FileNotFoundException fileNotFoundException) {
        fileNotFoundException.printStackTrace();
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }

    return buffer;
}

But my problem now is with converting the byte array back into a ZIP file - how can it be done?

Note : The specified ZIP contains two files.

Perception
  • 79,279
  • 19
  • 185
  • 195
Mohan
  • 877
  • 10
  • 20
  • 32
  • What exactly do you want? Do you want to write the bytes back to disk into a zip file? Or do you want to read the contents? The bytes how you read them are not decoded yet. – morja Dec 03 '11 at 10:47
  • @ morja -> yes i want to write the bytes back to disk in the form of zip file. – Mohan Dec 03 '11 at 10:52
  • Well, but then just write the bytes back to disk with a FileOutputStream and name the file .zip. Dont you want to write the extracted files? – morja Dec 03 '11 at 10:56
  • @morja -> yes i tried using FileOutputStream but i can't get the exact zip file. – Mohan Dec 03 '11 at 11:01
  • I still dont fully understand what you want to do... can you update your question and describe step by step or with an example what you want to achieve? – morja Dec 03 '11 at 11:24

3 Answers3

31

To get the contents from the bytes you can use

ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(bytes));
ZipEntry entry = null;
while ((entry = zipStream.getNextEntry()) != null) {

    String entryName = entry.getName();

    FileOutputStream out = new FileOutputStream(entryName);

    byte[] byteBuff = new byte[4096];
    int bytesRead = 0;
    while ((bytesRead = zipStream.read(byteBuff)) != -1)
    {
        out.write(byteBuff, 0, bytesRead);
    }

    out.close();
    zipStream.closeEntry();
}
zipStream.close(); 
ramjaane
  • 13
  • 6
morja
  • 8,297
  • 2
  • 39
  • 59
  • 1
    ya this helps me to get the entry name which is present inside the zip file. Using this we can only read the content. but how can we store it to disk. – Mohan Dec 03 '11 at 11:03
  • 1
    You can read the bytes from the zipStream and then write it with a FileOutputStream. Or directly write it out again. See my update. – morja Dec 03 '11 at 11:32
8

You probably are looking for code like this:

ZipInputStream z = new ZipInputStream(new ByteArrayInputStream(buffer))

now you can get the zip file contents via getNextEntry()

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
0

Here is a helper method

private fun getZipData(): ByteArray {
    val zipFile: File = getTempZipFile() // Return a zip File

    val encoded = Files.readAllBytes(Paths.get(zipFile.absolutePath))
    zipFile.delete() // If you wish to delete the zip file

    return encoded
}
Thiago
  • 12,778
  • 14
  • 93
  • 110