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