/** * De-compresses given byte array with a zip input stream with entry name {@link * #OBJECT_ZIP_ENTRY}. * * @param compressedBytes bytes to un-compress * @return byte array with de-compressed source. * @throws IOException if there is an unrecoverable problem while de-compressing bytes. */ public byte[] decompress(final byte[] compressedBytes) throws IOException { ZipInputStream zis = null; ByteArrayOutputStream baos = null; ByteArrayInputStream bais = null; try { bais = new ByteArrayInputStream(compressedBytes); zis = new ZipInputStream(bais); // Get entry so that we can start reading. zis.getNextEntry(); baos = new ByteArrayOutputStream(compressedBytes.length); IOUtils.copyInputToOuputStream(zis, baos); baos.flush(); return baos.toByteArray(); } finally { IOUtils.closeHard(bais); IOUtils.closeHard(zis); IOUtils.closeHard(baos); } }
/** * Compresses given byte array by creating a zip output stream with entry name {@link * #OBJECT_ZIP_ENTRY}. * * @param bytes bytes to compress * @return byte array with compressed source. * @throws IOException if there is an unrecoverable problem while compressing bytes. * @see #decompress(byte[]) */ public byte[] compress(final byte[] bytes) throws IOException { ByteArrayOutputStream baos = null; ZipOutputStream zos = null; ByteArrayInputStream bais = null; try { baos = new ByteArrayOutputStream(); bais = new ByteArrayInputStream(bytes); zos = new ZipOutputStream(baos); // Create entry as required by ZipOutputStream final ZipEntry entry = new ZipEntry(OBJECT_ZIP_ENTRY); zos.putNextEntry(entry); IOUtils.copyInputToOuputStream(bais, zos); zos.flush(); baos.flush(); zos.closeEntry(); return baos.toByteArray(); } finally { IOUtils.closeHard(bais); IOUtils.closeHard(baos); IOUtils.closeHard(zos); } }