forked from svn2github/dotnetzip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZlibStreamExample.cs
More file actions
123 lines (104 loc) · 4.39 KB
/
Copy pathZlibStreamExample.cs
File metadata and controls
123 lines (104 loc) · 4.39 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// ZlibDeflateInflate.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2009 by Dino Chiesa
// All rights reserved!
//
// This code module is part of DotNetZip, a zipfile class library.
//
// ------------------------------------------------------------------
//
// This code is licensed under the Microsoft Public License.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// Purpose: Demonstrate compression and decompression with the ZlibStream
// class, which is part of the Ionic.Zlib namespace.
//
// ------------------------------------------------------------------
//
using System;
using System.Reflection;
using Ionic.Zlib;
// to allow fast ngen
[assembly: AssemblyTitle("ZlibStreamExample.cs")]
[assembly: AssemblyDescription("Demonstrate compression and decompression using the ZlibStream class")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Dino Chiesa")]
[assembly: AssemblyProduct("DotNetZip Examples")]
[assembly: AssemblyCopyright("Copyright © Dino Chiesa 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.1.1.1")]
namespace Ionic.ToolsAndTests
{
public class ZlibStreamExample
{
/// <summary>
/// Converts a string to a MemoryStream.
/// </summary>
static System.IO.MemoryStream StringToMemoryStream(string s)
{
byte[] a = System.Text.Encoding.ASCII.GetBytes(s);
return new System.IO.MemoryStream(a);
}
/// <summary>
/// Converts a MemoryStream to a string. Makes some assumptions about the content of the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
static String MemoryStreamToString(System.IO.MemoryStream ms)
{
byte[] ByteArray = ms.ToArray();
return System.Text.Encoding.ASCII.GetString(ByteArray);
}
static void CopyStream(System.IO.Stream src, System.IO.Stream dest)
{
byte[] buffer = new byte[1024];
int len = src.Read(buffer, 0, buffer.Length);
while (len > 0)
{
dest.Write(buffer, 0, len);
len = src.Read(buffer, 0, buffer.Length);
}
dest.Flush();
}
[STAThread]
public static void Main(System.String[] args)
{
try
{
System.IO.MemoryStream msSinkCompressed;
System.IO.MemoryStream msSinkDecompressed;
ZlibStream zOut;
String originalText = "Hello, World! This String will be compressed... ";
System.Console.Out.WriteLine("original: {0}", originalText);
// first, compress:
msSinkCompressed = new System.IO.MemoryStream();
zOut = new ZlibStream(msSinkCompressed, CompressionMode.Compress, CompressionLevel.BestCompression, true);
CopyStream(StringToMemoryStream(originalText), zOut);
zOut.Close();
// at this point, msSinkCompressed contains the compressed bytes
// now, decompress:
msSinkCompressed.Seek(0, System.IO.SeekOrigin.Begin);
msSinkDecompressed = new System.IO.MemoryStream();
zOut = new ZlibStream(msSinkDecompressed, CompressionMode.Decompress, true);
CopyStream(msSinkCompressed, zOut);
// at this point, msSinkDecompressed contains the decompressed bytes
string decompressed = MemoryStreamToString(msSinkDecompressed);
System.Console.Out.WriteLine("decompressed: {0}", decompressed);
System.Console.WriteLine();
if (originalText == decompressed)
System.Console.WriteLine("A-OK. Compression followed by decompression gets the original text.");
else
System.Console.WriteLine("The compression/decompression cycle failed.");
}
catch (System.Exception e1)
{
Console.WriteLine("Exception: " + e1);
}
}
}
}