/// <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(); } }
/// <exception cref="System.IO.IOException"/> private void InternalReset() { if (needsReset) { needsReset = false; WriteStreamHeader(); this.output = new CBZip2OutputStream(@out); } }
public static byte[] CompressData(byte[] data) { using (MemoryStream compressed = new MemoryStream()) { using (CBZip2OutputStream s = new CBZip2OutputStream(compressed)) { s.Write(data, 0, data.Length); s.Finish(); byte[] cdata = new byte[compressed.Length]; compressed.Seek(0, SeekOrigin.Begin); compressed.Read(cdata, 0, (int)compressed.Length); return(cdata); } } }