1
2 using System;
3 using System.Collections.Generic;
4 using System.IO.Compression;
5 using System.IO;
6 using System.Collections;
7
8 namespace Utilities
9 {
10 class Compression
11 {
12 public static byte[] Compress(byte[] data)
13 {
14 MemoryStream ms = new MemoryStream();
15 DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);
16 ds.Write(data, 0, data.Length);
17 ds.Flush();
18 ds.Close();
19 return ms.ToArray();
20 }
21 public static byte[] Decompress(byte[] data)
22 {
23 const int BUFFER_SIZE = 256;
24 byte[] tempArray = new byte[BUFFER_SIZE];
25 List<byte[]> tempList = new List<byte[]>();
26 int count = 0, length = 0;
27
28 MemoryStream ms = new MemoryStream(data);
29 DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);
30
31 while ((count = ds.Read(tempArray, 0, BUFFER_SIZE)) > 0)
32 {
33 if (count == BUFFER_SIZE)
34 {
35 tempList.Add(tempArray);
36 tempArray = new byte[BUFFER_SIZE];
37 }
38 else
39 {
40 byte[] temp = new byte[count];
41 Array.Copy(tempArray, 0, temp, 0, count);
42 tempList.Add(temp);
43 }
44 length += count;
45 }
46
47 byte[] retVal = new byte[length];
48
49 count = 0;
50 foreach (byte[] temp in tempList)
51 {
52 Array.Copy(temp, 0, retVal, count, temp.Length);
53 count += temp.Length;
54 }
55
56 return retVal;
57 }
58 }
59 }