Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert byte array to ZIP file

Tags:

java

zip

zipfile

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.

like image 786
Mohan Avatar asked Dec 03 '11 10:12

Mohan


People also ask

How do I create a byte array zip file?

1- ByteArrayOutputStream & ZipOutputStreamUsing ByteArrayOutputStream and ZipOutputStream classes provided by the JDK, you can generate a zip file out of multiple files. That's it.

How do I read a byte array zip file?

The ZIP file does not need to be unzipped, so ZipInputStream is not necessary. Use a FileInputStream to read the ZIP file and a ByteArrayOutputStream to write its contents to. Get the latter's byte[]. Use ByteArrayInputStream and FileOutputStream to reverse the process.

How do I write a byte array to a file?

In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file. Example: Java.

How do you convert a byte array into a string?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.


2 Answers

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(); 
like image 58
morja Avatar answered Oct 11 '22 13:10

morja


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

like image 39
Has QUIT--Anony-Mousse Avatar answered Oct 11 '22 12:10

Has QUIT--Anony-Mousse