using System;
using System.IO;
namespace ICSharpCode.SharpZipLib.BZip2
{
///
/// An example class to demonstrate compression and decompression of BZip2 streams.
///
public static class BZip2
{
///
/// Decompress the input writing
/// uncompressed data to the output stream
///
/// The readable stream containing data to decompress.
/// The output stream to receive the decompressed data.
/// Both streams are closed on completion if true.
public static void Decompress(Stream inStream, Stream outStream, bool isStreamOwner)
{
if (inStream == null)
throw new ArgumentNullException(nameof(inStream));
if (outStream == null)
throw new ArgumentNullException(nameof(outStream));
try
{
using (BZip2InputStream bzipInput = new BZip2InputStream(inStream))
{
bzipInput.IsStreamOwner = isStreamOwner;
Core.StreamUtils.Copy(bzipInput, outStream, new byte[4096]);
}
}
finally
{
if (isStreamOwner)
{
// inStream is closed by the BZip2InputStream if stream owner
outStream.Dispose();
}
}
}
///
/// Compress the input stream sending
/// result data to output stream
///
/// The readable stream to compress.
/// The output stream to receive the compressed data.
/// Both streams are closed on completion if true.
/// Block size acts as compression level (1 to 9) with 1 giving
/// the lowest compression and 9 the highest.
public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int level)
{
if (inStream == null)
throw new ArgumentNullException(nameof(inStream));
if (outStream == null)
throw new ArgumentNullException(nameof(outStream));
try
{
using (BZip2OutputStream bzipOutput = new BZip2OutputStream(outStream, level))
{
bzipOutput.IsStreamOwner = isStreamOwner;
Core.StreamUtils.Copy(inStream, bzipOutput, new byte[4096]);
}
}
finally
{
if (isStreamOwner)
{
// outStream is closed by the BZip2OutputStream if stream owner
inStream.Dispose();
}
}
}
}
}