compressing files in JAVA -
my project has requirement have receive file via rest service(using jersey) , store in database.
the file size around 2-4mb.
the received file can either zip or pdf format. before storing in database compress it.
i googled , found there many available classes gzip, zip, deflater... thought of using deflater looked simple.i have written following code zipping.
deflater deflater = new deflater(); deflater.setinput(data); bytearrayoutputstream outputstream = new bytearrayoutputstream(data.length); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); outputstream.write(buffer, 0, count); } outputstream.close(); byte[] output = outputstream.tobytearray(); byte[] output = outputstream.tobytearray();
could 1 please suggest use case if use above code fine or have use other classes perform same.
thanks, kitty
bytearrayoutputstream caches compressed output in memory. have wrap around fileoutputstream avoid oom issue while writing in case of big files.
Comments
Post a Comment