# Mijn kleine compressiehelper...verbeterde versie

<datetime class="hidden">2004-04-06T11:31</datetime>

<!-- category -- mostlylucidcouk, Imported -->
Ik heb mijn compressie helper klasse eerder geplaatst en realiseerde me bijna direct dat het leed aan 'legacy lag' - dus het was zo veel veranderd in de maanden dat het was echt slecht!
Hoe dan ook, hieronder gepresenteerd is een aangepaste versie van die klasse (die ik heb ook geprobeerd mijn nieuwe opmaak schema op) als een herinnering, het is gewoon een eenvoudige wrapper rond [SharpZipLib](http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx).

gebruik van systeem;

gebruik van System.Text;

gebruik van System.IO;

gebruik van ICSharpCode.SharpZipLib;

namespace SerializableJob.Compressie

{

public enum CompressionType

{

GZip,

BZip2,

Zip

}

public class Compressie

{

public static CompressionType CompressionProvider = CompressionType.GZip;

private static Stream OutputStream(Stream inputStream)

{

switch(CompressionProvider)

{

case CompressionType.BZip2:

nieuwe ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(inputStream);

case CompressionType.GZip:

nieuwe ICSharpCode.SharpZipLib.GZip.GZipOutputStream(inputStream) teruggeven;

case CompressionType.Zip:

nieuwe ICSharpCode.SharpZipLib.Zip.ZipOutputStream(inputStream) teruggeven;

standaard:

nieuwe ICSharpCode.SharpZipLib.GZip.GZipOutputStream(inputStream) teruggeven;

}

}

private static Stream InputStream(Stream inputStream)

{

switch(CompressionProvider)

{

case CompressionType.BZip2:

return new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(inputStream);

case CompressionType.GZip:

nieuwe ICSharpCode.SharpZipLib.GZip.GZipInputStream(inputStream) teruggeven;

case CompressionType.Zip:

nieuwe ICSharpCode.SharpZipLib.Zip.ZipInputStream(inputStream) teruggeven;

standaard:

nieuwe ICSharpCode.SharpZipLib.GZip.GZipInputStream(inputStream) teruggeven;

}

}

publieke statische byte[] Compress(byte[] bytesToCompress)

{

MemoryStream ms = new MemoryStream();

Stream s = OutputStream(ms);

s.Write(bytesToCompress,0, bytesToCompress.Lengte);

s.Close();

return ms.ToArray();

}

public static string Compress(string stringToCompress)

{

byte[] gecomprimeerdData = CompressToByte(stringToCompress);

string strOout = Convert.ToBase64String(compressedData);

retourstreut;

}

publieke statische byte[] CompressToByte(string stringToCompress)

{

byte[] bytData = Encoding.Unicode.GetBytes(stringToCompress);

return Compress(bytData);

}

public string DeCompress(string stringToDecompress)

{

string outString = string.Leeg;

if (stringToDecompress ==null)

{

gooi nieuwe ArgumentNullException("stringToDecompress","Je probeerde een lege string" te gebruiken);

}

proberen

{

byte[] inArr = Convert.FromBase64String(stringToDecompress.Trim());

outString = System.Text.Encoding.Unicode.GetString(DeCompress(inArr));

}

vangst (NullReferenceException nEx)

{

nEx.Message retourneren;

}

terug te keren String;

}

publieke statische byte[] DeCompress(byte[] bytesToDecompress)

{

byte[] writeData = new byte[4096];

Stream s2 = InputStream(new MemoryStream(bytesToDecompress));

MemoryStream outStream = new MemoryStream();

while(true)

{

int size = s2.Read(writeData,0,writeData.Lengte);

if(size&gt;0)

{

outStream.Write(writeData,0,size);

}

andere

{

breuk;

}

}

s2.Close();

byte[] outArr = outStream.ToArray();

outStream.Close();

retournerenArr;

}

}

}
?>