-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cs
51 lines (44 loc) · 1.42 KB
/
test.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace TestApp
{
class Program
{
static UnicodeEncoding uniEncoding = new UnicodeEncoding();
public static byte[] Compress(string str, Encoding enc)
{
using (MemoryStream input = new MemoryStream(enc.GetBytes(str)))
using (var dstStream = new MemoryStream(100))
{
using (var compressionStream = new DeflateStream(dstStream, CompressionMode.Compress))
{
input.CopyTo(compressionStream);
}
return dstStream.ToArray();
}
}
public static byte[] Decompress(byte[] compressedData)
{
using (MemoryStream compressedStream = new MemoryStream(compressedData))
using (MemoryStream decompressedStream = new MemoryStream())
using (DeflateStream decompressionStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedStream);
return decompressedStream.GetBuffer();
}
}
static int Main()
{
string original = "hello world from c#";
byte[] compressed = Compress(original, uniEncoding);
Console.WriteLine("Compressed data has " + compressed.Length + " bytes");
byte[] decompressed = Decompress(compressed);
string output = uniEncoding.GetString(decompressed);
Console.WriteLine("Compressed the string: " + original);
Console.WriteLine("Into the string : " + output);
return 0;
}
}
}