0

I am getting a byte array representing a ZIP file from the JSON response sent by a server, and on the Android side I want to recreate the ZIP from the array.

I am using this code to achieve this but it is not creating a ZIP file in my device storage.

Extract byte array from JSON:

String zipBinary = mJsonObject.getString("zip");

File path for storing zip file:

String unzipLocation = Environment.getExternalStorageDirectory() + "/sample.zip"; 

Main code:

public CreateZipMultipleFiles(String filePath, byte[] sourceFile) {
    try {
        ByteArrayOutputStream fout = new ByteArrayOutputStream();

        ZipOutputStream zout = new ZipOutputStream(fout);
        ZipEntry ze = new ZipEntry(filePath);
        ze.setSize(sourceFile.length);
        zout.putNextEntry(ze);
        zout.write(sourceFile);
        zout.closeEntry();

        zout.close();

        System.out.println("Zip Byte file has been created!");
    } catch (IOException ioe) {
        System.out.println("IOException :" + ioe);
    }
}

How can I achieve this goal?

Perception
  • 79,279
  • 19
  • 185
  • 195
Krunal Shah
  • 1,438
  • 1
  • 17
  • 29
  • what's issue do you in this code? – Tofeeq Ahmad Mar 02 '13 at 06:31
  • Question: Is a file not created. Or is THE file you want not created? – cjds Mar 02 '13 at 06:43
  • zip file is not create – Krunal Shah Mar 02 '13 at 07:09
  • 1
    Why are you using zip output stream. Surely you can just write the bytes as is, since they represent a zip file, then stick the zip extension on the end – IAmGroot Mar 02 '13 at 11:39
  • See [Creating a zip file with android](http://stackoverflow.com/questions/9225673/creating-a-zip-file-with-android) or [Create a zip file](http://stackoverflow.com/questions/5789430/creating-a-zip-file-with-some-files-on-sdcard) or android javadoc on [ZipOutputStream](http://developer.android.com/reference/java/util/zip/ZipOutputStream.html). – harpun Mar 02 '13 at 12:02

0 Answers0