1: /*
2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
3:
4: Permission is hereby granted, free of charge, to any person obtaining a copy
5: of this software and associated documentation files (the "Software"), to deal
6: in the Software without restriction, including without limitation the rights
7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8: copies of the Software, and to permit persons to whom the Software is
9: furnished to do so, subject to the following conditions:
10:
11: The above copyright notice and this permission notice shall be included in
12: all copies or substantial portions of the Software.
13:
14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20: THE SOFTWARE.*/
21:
22: #region Usings
23: using System.IO;
24: using System.IO.Compression;
25: #endregion
26:
27: namespace Utilities.Compression
28: { 29: /// <summary>
30: /// Utility class used for compressing data
31: /// using deflate.
32: /// </summary>
33: public static class Deflate
34: { 35: #region Static Functions
36:
37: /// <summary>
38: /// Compresses data
39: /// </summary>
40: /// <param name="Bytes">The byte array to be compressed</param>
41: /// <returns>A byte array of compressed data</returns>
42: public static byte[] Compress(byte[] Bytes)
43: { 44: using (MemoryStream Stream = new MemoryStream())
45: { 46: using (DeflateStream ZipStream = new DeflateStream(Stream, CompressionMode.Compress, true))
47: { 48: ZipStream.Write(Bytes, 0, Bytes.Length);
49: ZipStream.Close();
50: return Stream.ToArray();
51: }
52: }
53: }
54:
55: /// <summary>
56: /// Decompresses data
57: /// </summary>
58: /// <param name="Bytes">The byte array to be decompressed</param>
59: /// <returns>A byte array of uncompressed data</returns>
60: public static byte[] Decompress(byte[] Bytes)
61: { 62: using (MemoryStream Stream = new MemoryStream())
63: { 64: using (DeflateStream ZipStream = new DeflateStream(new MemoryStream(Bytes), CompressionMode.Decompress, true))
65: { 66: byte[] Buffer = new byte[4096];
67: while (true)
68: { 69: int Size = ZipStream.Read(Buffer, 0, Buffer.Length);
70: if (Size > 0) Stream.Write(Buffer, 0, Size);
71: else break;
72: }
73: ZipStream.Close();
74: return Stream.ToArray();
75: }
76: }
77: }
78:
79: #endregion
80: }
81: }