public static byte[] Compress(byte[] data, int blockSize) { MemoryStream strm = new MemoryStream(data); MemoryStream ostrm = new MemoryStream(); BZip2OutputStream bzos = new BZip2OutputStream(ostrm, blockSize); int ch = strm.ReadByte(); while (ch != -1) { bzos.WriteByte((byte)ch); ch = strm.ReadByte(); } return(ostrm.GetBuffer()); }
/// <summary> /// Compress <paramref name="instream">input stream</paramref> sending /// result to <paramref name="outputstream">output stream</paramref> /// </summary> public static void Compress(Stream instream, Stream outstream, int blockSize) { System.IO.Stream bos = outstream; System.IO.Stream bis = instream; int ch = bis.ReadByte(); BZip2OutputStream bzos = new BZip2OutputStream(bos, blockSize); while (ch != -1) { bzos.WriteByte((byte)ch); ch = bis.ReadByte(); } bis.Close(); bzos.Close(); }
/// <summary> /// Compress <paramref name="inStream">input stream</paramref> sending /// result to <paramref name="outStream">output stream</paramref> /// </summary> /// <param name="inStream">The stream to compress.</param> /// <param name="outStream">The stream to write compressed data to.</param> /// <param name="blockSize">The block size to use.</param> /// <remarks>Both streams are closed on completion</remarks> public static void Compress(Stream inStream, Stream outStream, int blockSize) { if (inStream == null) { throw new ArgumentNullException("inStream"); } if (outStream == null) { throw new ArgumentNullException("outStream"); } using ( inStream ) { using (BZip2OutputStream bzos = new BZip2OutputStream(outStream, blockSize)) { int ch = inStream.ReadByte(); while (ch != -1) { bzos.WriteByte((byte)ch); ch = inStream.ReadByte(); } } } }