0

I have a chararray which holds coordinates in every index. I want to compress the array with zip algorithm and write the zipped version of the array to a file. My idea was that i run the chararray through a zip stream and write it afterwards directly to a file like test.txt. The problem is, that nothing is written to the file after the execution of the code. Can somebody please help me solve that problem? Kind regards Lorenzo

Here my current code:

    byte[] bytes = Charset.forName("UTF-8").encode(CharBuffer.wrap(sample)).array();
    FileOutputStream out = new FileOutputStream(cscFile, true);
    byte[] compressed = new byte[bytes.length];
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    ZipInputStream zi = new ZipInputStream(bi);
    ZipEntry entry = null;
    while ((entry = zi.getNextEntry()) != null) {
    zi.read(compressed);
    for(int i = 0; i<bytes.length;i++){
    out.write(compressed[i]);
    }

    out.flush();
    out.close();
    zi.closeEntry();
}
zi.close(); 

1 Answers1

2

You will probably have to slightly modify your code with something like this:

    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File("your zip file name")));
    ZipEntry entry = new ZipEntry("zipped file name");
    entry.setSize(bytes.length);
    zos.putNextEntry(entry);
    zos.write(bytes);
    zos.closeEntry();
    zos.close();

Explanation: To compress writen data, use ZipOutputStream and give created FileOutputStream as constructor argument. After that, create ZipEntry, which represents file inside your zip file and write contents of byte array into it.

Hope it helps.

See similar question here: https://stackoverflow.com/a/357892/3115098

Community
  • 1
  • 1
Jiri Kusa
  • 473
  • 6
  • 16