0

I have following method to zip some data as 'file' (practically this is byte array) in memory.

Result will be saved in database therefore i need this array (method returns byte[]).

How can i unzip - data saved as zipped file in memory? I expect exactly what i gave as param 'data' in zip method.

public static byte[] zip(String name, String data) {

    ByteArrayOutputStream baos = null;
    ZipOutputStream zos = null;
    try {
        baos = new ByteArrayOutputStream();
        zos = new ZipOutputStream(baos);
        ZipEntry zipEntry = new ZipEntry(name);
        byte[] bytes = data.getBytes();
        zipEntry.setSize(bytes.length);
        zos.putNextEntry(zipEntry);
        zos.write(bytes);
        return baos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (Objects.nonNull(zos)) {
            try {
                zos.closeEntry();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                zos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (Objects.nonNull(baos)) {
            try {
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    throw new RuntimeException();
}
Artur
  • 217
  • 1
  • 5
  • 12
  • So you found [`ZipOutputStream`](https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipOutputStream.html). Is it so hard to imagine that the opposite operation is done by [`ZipInputStream`](https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipInputStream.html), i.e. to read from a zip file? – Andreas Jan 04 '18 at 23:15
  • I do not have a 'physical' file. File is stored in memory. Therefore post is not duplicated. unzip should take the parameter: byte[] and return 'data' – Artur Jan 04 '18 at 23:19
  • Understood, but why are you saying that? Your `ZipOutputStream` didn't create a file, right? It didn't even create a byte array. You had to use a `ByteArrayOutputStream` to connect the `ZipOutputStream` to a byte array. So...... `ZipInputStream` doesn't read from a file or a byte array either. What do you ***think*** it needs to read from a byte array? – Andreas Jan 04 '18 at 23:23
  • And your question *is* a duplicate, because the [**accepted answer**](https://stackoverflow.com/a/23870501/5221149) is also not reading from a file!!! --- Perhaps you should read the answer, and perhaps you should also take a moment to read the **documentation**, i.e. the javadoc of the classes. – Andreas Jan 04 '18 at 23:26
  • I think my `ZipOutputStream` has created byte array by `ByteArrayOutputStream` 'return baos.toByteArray()'. I think so... – Artur Jan 04 '18 at 23:31
  • Ok, I'll make it explicit. You wrote to zip using `baos = new ByteArrayOutputStream(); new ZipOutputStream(baos)`, so you read from zip using `new ZipInputStream(new ByteArrayInputStream(byteArray))` – Andreas Jan 05 '18 at 02:56

0 Answers0