Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1551 | Anzahl registrierter User: 1841 | Besucher online: 5
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Serialisiert Typ in ein komprimiertes bytearray


Autor: Steffen Richter
Sprache: C#
Bewertung:
7.67 (2 votes)
Anzahl der Aufrufe: 8012
  
Kick it on dotnet-kicks.de  

Beschreibung:

Serialisiert Typ in ein komprimiertes bytearray und wieder zurück

Abgelegt unter: Serialisierung, komprimieren, dekomprimieren, serialisieren.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;
    }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.