Laden...

GZip Helper-Klasse

Erstellt von TiltonJH vor 14 Jahren Letzter Beitrag vor 14 Jahren 4.221 Views
TiltonJH Themenstarter:in
87 Beiträge seit 2006
vor 14 Jahren
GZip Helper-Klasse

Beschreibung:

Da ich im Forum immer mal wieder auf Fragen und Probleme bezüglich GZip stoße und mich das selber mal beschäftigt hat, werde ich hier mal meine GZIP Klasse als Lösung rein stellen.

Im Prinzip macht sie den GZipStream mit statischen (helper-)Methoden verfügbar und somit leichter ver-/anwendbar.


using System;
using System.IO;
using System.IO.Compression;

namespace Tools.Packer.GZIP
{
	/// <summary>
	/// Provides methods used to compress and decompress using a System.IO.Compression.GZipStream.
	/// </summary>
	public static class GZIP
	{
		private const int bufferSize = 1 << 12; // Pow(2, 12) == 4096
		private const string canNotRead = "Can not read.";
		private const string canNotWrite = "Can not write.";
		private const string canNotSeek = "Can not seek.";

		/// <summary>
		/// Compresses a System.Byte[], using a System.IO.Compression.GZipStream.
		/// </summary>
		/// <param name="bytes">A uncompressed System.Byte[].</param>
		/// <returns>A compressed System.Byte[].</returns>
		/// <exception cref="System.ArgumentNullException">bytes is null.</exception>
		public static byte[] Compress(byte[] bytes)
		{
			if (bytes == null)
				throw new ArgumentNullException("bytes");

			using (var uncompressed = new MemoryStream(bytes))
			using (var compressed = new MemoryStream())
			{
				Compress(uncompressed, compressed);
				return compressed.ToArray();
			}
		}
		/// <summary>
		/// Compresses a System.IO.Stream, using a System.IO.Compression.GZipStream.
		/// </summary>
		/// <param name="stream">A uncompressed System.IO.Stream to compress.</param>
		/// <exception cref="System.ArgumentNullException">stream is null.</exception>
		/// <exception cref="System.ArgumentException">Can not read from stream.</exception>
		/// <exception cref="System.ArgumentException">Can not write to stream.</exception>
		/// <exception cref="System.ArgumentException">Can not seek in stream.</exception>
		public static void Compress(Stream stream)
		{
			if (stream == null)
				throw new ArgumentNullException("stream");
			if (!stream.CanRead)
				throw new ArgumentException(canNotRead, "stream");
			if (!stream.CanWrite)
				throw new ArgumentException(canNotWrite, "stream");
			if (!stream.CanSeek)
				throw new ArgumentException(canNotSeek, "stream");

			using (var ms = new MemoryStream())
			{
				stream.Position = 0;
				Compress(stream, ms);
				ms.Position = 0;
				stream.Position = 0;
				stream.SetLength(ms.Length);
				byte[] buffer = new byte[bufferSize];
				while (true)
				{
					int read = ms.Read(buffer, 0, buffer.Length);
					if (read == 0)
						break;
					stream.Write(buffer, 0, read);
				}
			}
			stream.Flush();
		}
		/// <summary>
		/// Compresses a System.IO.Stream, using a System.IO.Compression.GZipStream.
		/// </summary>
		/// <param name="uncompressed">A System.IO.Stream containing uncompressed data.</param>
		/// <param name="compressed">A System.IO.Stream to write the compressed data into.</param>
		/// <exception cref="System.ArgumentNullException">uncompressed is null.</exception>
		/// <exception cref="System.ArgumentNullException">compressed is null.</exception>
		/// <exception cref="System.ArgumentException">Can not read from uncompressed.</exception>
		/// <exception cref="System.ArgumentException">Can not write to compressed.</exception>
		public static void Compress(Stream uncompressed, Stream compressed)
		{
			if (uncompressed == null)
				throw new ArgumentNullException("uncompressed");
			if (compressed == null)
				throw new ArgumentNullException("compressed");
			if (!uncompressed.CanRead)
				throw new ArgumentException(canNotRead, "uncompressed");
			if (!compressed.CanWrite)
				throw new ArgumentException(canNotWrite, "compressed");

			using (var gzip = new GZipStream(compressed, CompressionMode.Compress, true))
			{
				byte[] buffer = new byte[bufferSize];
				while (true)
				{
					int read = uncompressed.Read(buffer, 0, buffer.Length);
					if (read == 0)
						break;
					gzip.Write(buffer, 0, read);
				}
				gzip.Flush();
			}
		}

		/// <summary>
		/// Decompresses a System.Byte[], using a System.IO.Compression.GZipStream.
		/// </summary>
		/// <param name="bytes">A compressed System.Byte[].</param>
		/// <returns>A uncompressed System.Byte[].</returns>
		/// <exception cref="System.ArgumentNullException">bytes is null.</exception>
		public static byte[] Decompress(byte[] bytes)
		{
			if (bytes == null)
				throw new ArgumentNullException("bytes");

			using (var compressed = new MemoryStream(bytes))
			using (var uncompressed = new MemoryStream())
			{
				Decompress(compressed, uncompressed);
				return uncompressed.ToArray();
			}
		}
		/// <summary>
		/// Decompresses a System.IO.Stream, using a System.IO.Compression.GZipStream.
		/// </summary>
		/// <param name="stream">A compressed System.IO.Stream to decompress.</param>
		/// <exception cref="System.ArgumentNullException">stream is null.</exception>
		/// <exception cref="System.ArgumentException">Can not read from stream.</exception>
		/// <exception cref="System.ArgumentException">Can not write to stream.</exception>
		/// <exception cref="System.ArgumentException">Can not seek in stream.</exception>
		public static void Decompress(Stream stream)
		{
			if (stream == null)
				throw new ArgumentNullException("stream");
			if (!stream.CanRead)
				throw new ArgumentException(canNotRead, "stream");
			if (!stream.CanWrite)
				throw new ArgumentException(canNotWrite, "stream");
			if (!stream.CanSeek)
				throw new ArgumentException(canNotSeek, "stream");

			using (var ms = new MemoryStream())
			{
				stream.Position = 0;
				Decompress(stream, ms);
				ms.Position = 0;
				stream.Position = 0;
				stream.SetLength(ms.Length);
				byte[] buffer = new byte[bufferSize];
				while (true)
				{
					int read = ms.Read(buffer, 0, buffer.Length);
					if (read == 0)
						break;
					stream.Write(buffer, 0, read);
				}
			}
			stream.Flush();
		}
		/// <summary>
		/// Decompresses a System.IO.Stream, using a System.IO.Compression.GZipStream.
		/// </summary>
		/// <param name="compressed">A System.IO.Stream containing compressed data.</param>
		/// <param name="uncompressed">A System.IO.Stream to write the uncompressed data into.</param>
		/// <exception cref="System.ArgumentNullException">compressed is null.</exception>
		/// <exception cref="System.ArgumentNullException">uncompressed is null.</exception>
		/// <exception cref="System.ArgumentException">Can not read from compressed.</exception>
		/// <exception cref="System.ArgumentException">Can not write to uncompressed.</exception>
		public static void Decompress(Stream compressed, Stream uncompressed)
		{
			if (compressed == null)
				throw new ArgumentNullException("compressed");
			if (uncompressed == null)
				throw new ArgumentNullException("uncompressed");
			if (!compressed.CanRead)
				throw new ArgumentException(canNotRead, "compressed");
			if (!uncompressed.CanWrite)
				throw new ArgumentException(canNotWrite, "uncompressed");

			using (var gzip = new GZipStream(compressed, CompressionMode.Decompress))
			{
				byte[] buffer = new byte[bufferSize];
				while (true)
				{
					int read = gzip.Read(buffer, 0, buffer.Length);
					if (read == 0)
						break;
					uncompressed.Write(buffer, 0, read);
				}
				uncompressed.Flush();
			}
		}
	}
}

EDIT: Hab noch mal was am bufferSize gemacht.

Schlagwörter: GZip, Stream, GZipStream, compress, decompress, uncompressed

"In der Informatik geht es genauso wenig um Computer wie in der Astonomie um Teleskope."
Edsger W. Dijkstra

The Humble Programmer by Edsger W. Dijkstra