1: using System.IO.Compression;
2: using System.IO;
3: using System.Text;
4:
5: namespace Ia.Cl.Model
6: {
7: ////////////////////////////////////////////////////////////////////////////
8: ////////////////////////////////////////////////////////////////////////////
9:
10: /// <summary publish="true">
11: /// Zip
12: /// </summary>
13: /// <remarks>
14: /// Copyright © 2021-2022 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
15: ///
16: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
17: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
18: ///
19: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
20: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21: ///
22: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
23: ///
24: /// Copyright notice: This notice may not be removed or altered from any source distribution.
25: /// </remarks>
26: public class Zip
27: {
28: ////////////////////////////////////////////////////////////////////////////
29:
30: /// <summary>
31: ///
32: /// </summary>
33: public Zip() { }
34:
35: ////////////////////////////////////////////////////////////////////////////
36:
37: /// <summary>
38: ///
39: /// </summary>
40: public static byte[] ZipString(string s)
41: {
42: var bytes = Encoding.UTF8.GetBytes(s);
43:
44: using (var msi = new MemoryStream(bytes))
45: {
46: using (var mso = new MemoryStream())
47: {
48: using (var gs = new GZipStream(mso, CompressionMode.Compress))
49: {
50: //msi.CopyTo(gs);
51: CopyTo(msi, gs);
52: }
53:
54: return mso.ToArray();
55: }
56: }
57: }
58:
59: ////////////////////////////////////////////////////////////////////////////
60:
61: /// <summary>
62: ///
63: /// </summary>
64: public static string UnzipString(byte[] bytes)
65: {
66: using (var msi = new MemoryStream(bytes))
67: {
68: using (var mso = new MemoryStream())
69: {
70: using (var gs = new GZipStream(msi, CompressionMode.Decompress))
71: {
72: //gs.CopyTo(mso);
73: CopyTo(gs, mso);
74: }
75:
76: return Encoding.UTF8.GetString(mso.ToArray());
77: }
78: }
79: }
80:
81: ////////////////////////////////////////////////////////////////////////////
82:
83: /// <summary>
84: ///
85: /// </summary>
86: private static void CopyTo(Stream src, Stream dest)
87: {
88: byte[] bytes = new byte[4096];
89:
90: int cnt;
91:
92: while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0)
93: {
94: dest.Write(bytes, 0, cnt);
95: }
96: }
97:
98: ////////////////////////////////////////////////////////////////////////////
99: ////////////////////////////////////////////////////////////////////////////
100: }
101:
102: ////////////////////////////////////////////////////////////////////////////
103: ////////////////////////////////////////////////////////////////////////////
104: }