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 ?

有帮助吗?

解决方案

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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top