-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGzipReader.cs
67 lines (59 loc) · 1.49 KB
/
GzipReader.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.IO;
using System.IO.Compression;
using System.Net;
// sample code
namespace beastie
{
class GzipReader
{
public static byte[] Decompress(byte[] gzip)
{
using (GZipStream stream = new GZipStream(new MemoryStream(gzip),
CompressionMode.Decompress))
{
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}
static void Main(string[] args)
{
try
{
Console.WriteLine("*** Decompress web page ***");
Console.WriteLine(" Specify file to download");
Console.WriteLine("Downloading: {0}", args[0]);
// Download url.
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
byte[] data = client.DownloadData(args[0]);
byte[] decompress = Decompress(data);
string text = System.Text.ASCIIEncoding.ASCII.GetString(decompress);
Console.WriteLine("Size from network: {0}", data.Length);
Console.WriteLine("Size decompressed: {0}", decompress.Length);
Console.WriteLine("First chars: {0}", text.Substring(0, 5));
}
}
finally
{
Console.WriteLine("[Done]");
Console.ReadLine();
}
}
}
}