/// <summary> /// Compresses a GZIP file. /// </summary> /// <param name="bytes"> The uncompressed bytes. </param> /// <returns> The compressed bytes. </returns> /// <exception cref="IOException"> if an I/O error occurs. </exception> public static byte[] Gzip(byte[] bytes) { /* create the streams */ var @is = new ByteArrayInputStream(bytes); try { var bout = new ByteArrayOutputStream(); var os = new GZIPOutputStream(bout); try { /* copy data between the streams */ var buf = new byte[4096]; var len = 0; while ((len = @is.read(buf, 0, buf.Length)) != -1) { os.write(buf, 0, len); } } finally { os.close(); } /* return the compressed bytes */ return(bout.toByteArray()); } finally { @is.close(); } }
/// <summary> /// Compresses a BZIP2 file. /// </summary> /// <param name="bytes"> The uncompressed bytes. </param> /// <returns> The compressed bytes without the header. </returns> /// <exception cref="IOException"> if an I/O erorr occurs. </exception> public static byte[] Bzip2(byte[] bytes) { var @is = new ByteArrayInputStream(bytes); try { var bout = new ByteArrayOutputStream(); OutputStream os = new CBZip2OutputStream(bout, 1); try { var buf = new byte[4096]; var len = 0; while ((len = @is.read(buf, 0, buf.Length)) != -1) { os.write(buf, 0, len); } } finally { os.close(); } /* strip the header from the byte array and return it */ bytes = bout.toByteArray(); var bzip2 = new byte[bytes.Length - 2]; Array.Copy(bytes, 2, bzip2, 0, bzip2.Length); return(bzip2); } finally { @is.close(); } }