Feedback

C# - Serialisiert Typ in ein komprimiertes bytearray

Veröffentlicht von am 8/21/2007
(2 Bewertungen)
Serialisiert Typ in ein komprimiertes bytearray und wieder zurück
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;

/// <summary>
/// Static class which provides Helperfunctions for De/Compression
/// </summary>
public static class CompressionHelpers
{
    public static byte[] CompressTypeToByteArray<T>(T input) where T : class
    {
        byte[] uncompressed = SerializeTypeToByteArray(input);
        return CompressByteArray(uncompressed);
    }

    public static T DecompressByteArrayToType<T>(byte[] buffer) where T : class
    {
        byte[] decompressed = DecompressByteArray(buffer);
        return DeserializeByteArrayToType<T>(
            decompressed);
    }


    public static byte[] DecompressByteArray(byte[] compressedByteArray)
    {
        MemoryStream compressedStream = new MemoryStream(compressedByteArray);
        DeflateStream compressedzipStream =
            new DeflateStream(compressedStream, CompressionMode.Decompress, true);
        MemoryStream decompressedStream = new MemoryStream();
        const int blockSize = 1024;

        byte[] buffer = new byte[blockSize];
        int bytesRead;
        while ((bytesRead = compressedzipStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            decompressedStream.Write(buffer, 0, bytesRead);
        }

        compressedzipStream.Close();
        decompressedStream.Position = 0;
        byte[] decompressedArray = decompressedStream.ToArray();
        decompressedStream.Close();
        decompressedStream.Dispose();
        compressedzipStream.Close();
        compressedzipStream.Dispose();
        return decompressedArray;
    }

    public static byte[] CompressByteArray(byte[] uncompressedByteArray)
    {
        MemoryStream msCompressed = new MemoryStream();
        DeflateStream compressedzipStream =
            new DeflateStream(msCompressed, CompressionMode.Compress, true);

        compressedzipStream.Write(uncompressedByteArray, 0, uncompressedByteArray.Length);
        compressedzipStream.Flush();
        compressedzipStream.Close();

        byte[] compressedByteArray = msCompressed.ToArray();
        msCompressed.Close();
        msCompressed.Dispose();

        return compressedByteArray;
    }

    public static T DeserializeStreamToType<T>(Stream stream) where T : class
    {
        BinaryFormatter bf = new BinaryFormatter();
        T users = bf.Deserialize(stream) as T;
        return users;
    }

    public static T DeserializeByteArrayToType<T>(byte[] stream) where T : class
    {
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream(stream);
        T users = bf.Deserialize(ms) as T;
        ms.Close();
        ms.Dispose();
        return users;
    }

    public static byte[] SerializeTypeToByteArray<T>(T input) where T : class
    {
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        bf.Serialize(ms, input);

        byte[] array = ms.ToArray();
        ms.Close();
        ms.Dispose();
        return array;
    }
}

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!