<
From version < 1.2 >
edited by Robert Jan Daams
on 2025/02/04 11:01
To version < 1.3 >
edited by Robert Jan Daams
on 2025/02/04 11:15
>
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,0 +1,136 @@
1 += Usefull code snippets =
2 +
3 +== C# .NET ==
4 +
5 +== Zip Stream class ==
6 +
7 +{{info}}
8 +This is a Zip Stream class you can use to extract a zipstream.
9 +{{/info}}
10 +
11 +{{code language="Csharp" layout="LINENUMBERS"}}
12 +/// <summary>
13 +/// Helper class for compress en decompress of file data
14 +/// </summary>
15 +public static class ZipStreamHelper {
16 + #region Compress and encode
17 + /// <summary>
18 + /// Comprimeer een string en geeft deze terug in Base64 string.
19 + /// </summary>
20 + /// <param name="content">filecontent</param>
21 + /// <returns>gecomprimeerde string</returns>
22 + public static string CompressToBase64String(string content) {
23 + //string to byte[]
24 + byte[] contentArray = stringToByteArray(content);
25 + // Compress
26 + byte[] compressed = Compress(contentArray);
27 + return base64_encode(compressed);
28 + }
29 +
30 + /// <summary>
31 + /// Zet een string om naar een ByteArray
32 + /// </summary>
33 + /// <param name="content">filecontent</param>
34 + /// <returns>ByteArray</returns>
35 + public static byte[] stringToByteArray(string content) {
36 + System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
37 + return encoding.GetBytes(content);
38 + }
39 +
40 + /// <summary>
41 + /// comprimeer een ByteArray
42 + /// </summary>
43 + /// <param name="data">filedata als ByteArray</param>
44 + /// <returns>gecomprimeerde ByteArray</returns>
45 + public static byte[] Compress(byte[] data) {
46 + using(var compressedStream = new MemoryStream())
47 + using(var zipStream = new GZipStream(compressedStream, CompressionMode.Compress)) {
48 + zipStream.Write(data, 0, data.Length);
49 + zipStream.Close();
50 + return compressedStream.ToArray();
51 + }
52 + }
53 +
54 + /// <summary>
55 + /// Zet een ByteArray om naar een Base64 string
56 + /// </summary>
57 + /// <param name="data">gecomprimeerde ByteArray</param>
58 + /// <returns>Base 64 string</returns>
59 + public static string base64_encode(byte[] data) {
60 + if (data == null)
61 + return string.Empty;
62 + return Convert.ToBase64String(data);
63 + }
64 +
65 + /// <summary>
66 + /// Zet een string om naar een Base64 string
67 + /// </summary>
68 + /// <param name="data">string</param>
69 + /// <returns>Base 64 string</returns>
70 + public static string string_base64_encode(string data) {
71 + if (string.IsNullOrEmpty(data))
72 + return string.Empty;
73 +
74 + //string to byte[]
75 + byte[] contentArray = stringToByteArray(data);
76 + return base64_encode(contentArray);
77 + }
78 + #endregion
79 +
80 + #region Decompress and decode
81 + // Decode and decompress
82 + /// <summary>
83 + /// Decomprimeer een Base64 string naar een string
84 + /// </summary>
85 + /// <param name="contentBase64">GZIP Base64 string</param>
86 + /// <returns>string</returns>
87 + public static string DecompressBase64StringToString(string contentBase64) {
88 + // Decompress
89 + byte[] decoded = base64_decode(contentBase64);
90 + byte[] decompressed = Decompress(decoded);
91 +
92 + return byteArrayTostring(decompressed);
93 + }
94 +
95 + /// <summary>
96 + /// Zet een ByteArray om in een normale string
97 + /// </summary>
98 + /// <param name="data">ByteArray</param>
99 + /// <returns>string</returns>
100 + public static string byteArrayTostring(byte[] data) {
101 + System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
102 + return enc.GetString(data);
103 + }
104 +
105 + /// <summary>
106 + /// Zet een Base64 string om naar een ByteArray
107 + /// </summary>
108 + /// <param name="encodedData">Base64 string</param>
109 + /// <returns>gecomprimeerde ByteArray</returns>
110 + public static byte[] base64_decode(string encodedData) {
111 + byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData);
112 + return encodedDataAsBytes;
113 + }
114 +
115 + /// <summary>
116 + /// Zet een gecomprimeerde ByteArray om in een ByteArray
117 + /// </summary>
118 + /// <param name="data">gecomprimeerde ByteArray</param>
119 + /// <returns>ByteArray (gedecomprimeerd)</returns>
120 + public static byte[] Decompress(byte[] data) {
121 + using(var compressedStream = new MemoryStream(data))
122 + using(var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
123 + using(var resultStream = new MemoryStream()) {
124 + var buffer = new byte[4096];
125 + int read;
126 +
127 + while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0) {
128 + resultStream.Write(buffer, 0, read);
129 + }
130 +
131 + return resultStream.ToArray();
132 + }
133 + }
134 + #endregion
135 +}
136 +{{/code}}
TerraIndex
asd