/*
* Copyright (C) 2007 Eskil Bylund
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.IO;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.GZip;
namespace DCSharp
{
///
/// Helper methods for (de)compressing data in streams.
///
public static class FileUtil
{
#region Compression
public enum Compression
{
BZip2,
GZip
}
///
/// Returns a stream that can be used to read the compressed data in a stream.
///
/// The stream with the compressed data.
/// The compression type that was used.
/// The new stream.
public static Stream GetDecompressionStream(Stream inStream,
Compression compression)
{
// TODO: Require a version of SharpZipLib that provides the
// IsStreamOwner property on BZip2InputStream.
switch (compression)
{
case Compression.BZip2:
BZip2InputStream stream = new BZip2InputStream(inStream);
//stream.IsStreamOwner = false;
return stream;
case Compression.GZip:
GZipInputStream gzStream = new GZipInputStream(inStream);
gzStream.IsStreamOwner = false;
return gzStream;
}
throw new NotImplementedException("Algorithm not implemented.");
}
///
/// Returns a stream that will compress the data written to it.
///
/// The stream that will hold the compressed data.
/// The compression type to use.
///
public static Stream GetCompressionStream(Stream outStream,
Compression compression)
{
switch (compression)
{
case Compression.BZip2:
return new BZip2OutputStream(outStream, 1);
case Compression.GZip:
return new GZipOutputStream(outStream);
}
throw new NotImplementedException("Algorithm not implemented.");
}
///
/// Decompresses data from one stream and writes it to another.
///
/// The stream with the compressed data.
/// The stream that will hold the decompressed data.
/// The compression type to use.
public static void Decompress(Stream inStream, Stream outStream,
Compression compression)
{
using (Stream stream = GetDecompressionStream(inStream, compression))
{
WriteToStream(stream, outStream);
}
}
///
/// Compresses data from one stream and writes it to another.
///
/// The stream with the data to compress.
/// The stream that will hold the compressed data.
/// The compression type to use.
public static void Compress(Stream inStream, Stream outStream,
Compression compression)
{
using (outStream = GetCompressionStream(outStream, compression))
{
WriteToStream(inStream, outStream);
}
}
///
/// Compresses data from one file and writes it to another.
///
/// The file with the data to compress.
/// The file to write the compressed data to.
/// The compression type to use.
public static void Compress(string inFile, string outFile,
Compression compression)
{
using (FileStream inStream = new FileStream(inFile, FileMode.Open,
FileAccess.Read, FileShare.Read),
outStream = new FileStream(outFile,
FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
{
Compress(inStream, outStream, compression);
}
}
private static void WriteToStream(Stream inStream, Stream outStream)
{
int size;
byte[] buffer = new byte[4096];
do
{
size = inStream.Read(buffer, 0, buffer.Length);
if (size > 0)
{
outStream.Write(buffer, 0, size);
}
}
while (size > 0);
}
#endregion
}
}