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
| using System;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace CompressAndDecompress {
class Program {
static string body;
static string originalBody;
static byte[] bodyGZip;
static void Main (string[] args) {
body = "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz";
originalBody = body;
Compress ();
Decompress ();
if (originalBody != body)
Console.WriteLine ("Marche po...");
else
Console.WriteLine ("Marche");
Console.ReadKey ();
}
static void Compress () {
byte[] bodyBytesArray = Encoding.Unicode.GetBytes (body);
MemoryStream bodyGzipMemoryStream = new MemoryStream ();
GZipStream gzipStream = new GZipStream (bodyGzipMemoryStream, CompressionMode.Compress);
gzipStream.Write (bodyBytesArray, 0, bodyBytesArray.Length);
bodyGzipMemoryStream.Position = 0;
bodyGZip = new byte[bodyGzipMemoryStream.Length];
bodyGzipMemoryStream.Read (bodyGZip, 0, bodyGZip.Length);
}
static void Decompress () {
MemoryStream bodyGzipMemoryStream = new MemoryStream (bodyGZip);
GZipStream gzipStream = new GZipStream (bodyGzipMemoryStream, CompressionMode.Decompress, true);
MemoryStream bodyMemoryStream = new MemoryStream ();
byte[] buffer = new byte[1024];
int readBytes;
// gzipStream.Read (buffer, 0, buffer.Length);
while ((readBytes = gzipStream.Read (buffer, 0, buffer.Length)) != 0)
bodyMemoryStream.Write (buffer, 0, readBytes);
bodyMemoryStream.Position = 0;
byte[] bodyBytesArray = new byte[bodyMemoryStream.Length];
bodyMemoryStream.Read (bodyBytesArray, 0, bodyBytesArray.Length);
body = Encoding.Unicode.GetString (bodyBytesArray);
}
}
} |
Partager