Question

I want to open a specific file and zip it and send the byte array of the zipped result over UDP.

Now I checked the java zip API but it only give me to save the zipped file to the computer and opening it again and send it would be inefficiency.

Is there any way to do what I need without writing an implementation of the compression algorithm of my own ?

Was it helpful?

Solution

You cand wrap your ZipOutputStream around a ByteArrayOutputStream and get the bytes out of the ByteArrayOutputStream. Something like this:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
//write the entries
zos.close();
byte[] bytes = baos.toByteArray();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top