ZlibDeflateInflate.cs :  » Development » DotNetZip » Ionic » ToolsAndTests » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » DotNetZip 
DotNetZip » Ionic » ToolsAndTests » ZlibDeflateInflate.cs
// 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 ZlibCodec
// class, which is part of the Ionic.Zlib namespace.
// 
// ------------------------------------------------------------------
//

using System;
using System.Text;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;  // HashAlgorithm
using Ionic.Zlib;

// to allow fast ngen
[assembly: AssemblyTitle("ZlibDeflateInflate.cs")]
[assembly: AssemblyDescription("Demonstrate compression and decompression with the ZlibCodec 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 ZlibDeflateInflate
    {
        private HashAlgorithm alg = new SHA256CryptoServiceProvider();

        public ZlibDeflateInflate () {}

        public byte[] ComputeHash(byte[] data)
        {
            return alg.ComputeHash(data);
        }

        
        public byte[] ComputeHash(string text)
        {
            return alg.ComputeHash(UTF8Encoding.UTF8.GetBytes( text ));
        }

        private static string ByteArrayToString(byte[] buffer)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (byte b in buffer)
                sb.Append(b.ToString("X2"));

            return (sb.ToString());
        }

        
        
        public void Run()
        {
            System.Console.WriteLine("\nThis program demonstrates compression of strings with the\nIonic.Zlib.ZlibCodec class.\n");
        
            string textToCompress = File.ReadAllText("ZlibDeflateInflate.cs");
            string hashOfOriginal = ByteArrayToString(ComputeHash(textToCompress));
            System.Console.WriteLine("hash of original:     {0}", hashOfOriginal);
            System.Console.WriteLine("length of original:   {0}", textToCompress.Length);
        
            byte[] compressed = ZlibCodecCompress(textToCompress);
            System.Console.WriteLine("length of compressed: {0}", compressed.Length);

            double compRatio = 100 * (1.0 - (1.0 * compressed.Length) / (1.0 * textToCompress.Length)) ;

            System.Console.WriteLine("compression rate:     {0:N1}%", compRatio);

            string decompressed = ZlibCodecDecompress(compressed);
            string hashOfDecompressed = ByteArrayToString(ComputeHash(textToCompress));
            System.Console.WriteLine("hash of decompressed: {0}", hashOfDecompressed);
            System.Console.WriteLine();
            
            if (hashOfOriginal == hashOfDecompressed)
                System.Console.WriteLine("Round trip SUCCESS: After compress and decompress, we obtained the original text.");
            else                
                System.Console.WriteLine("Round trip FAIL: After compress and decompress, we did not obtain the original text.");
        }


        private byte[] ZlibCodecCompress(string textToCompress)
        {
            int outputSize = 2048;
            byte[] output = new Byte[ outputSize ];
            byte[] uncompressed = UTF8Encoding.UTF8.GetBytes( textToCompress );
            int lengthToCompress = uncompressed.Length;

            // If you want a ZLIB stream, set this to true.  If you want
            // a bare DEFLATE stream, set this to false.
            bool wantRfc1950Header = false;

            using ( MemoryStream ms = new MemoryStream())
            {
                ZlibCodec compressor = new ZlibCodec();
                compressor.InitializeDeflate(CompressionLevel.BestCompression, wantRfc1950Header);  
            
                compressor.InputBuffer = uncompressed;
                compressor.AvailableBytesIn = lengthToCompress;
                compressor.NextIn = 0;
                compressor.OutputBuffer = output;

                foreach (var f in new FlushType[] { FlushType.None, FlushType.Finish } )
                {
                    int bytesToWrite = 0;
                    do
                    {
                        compressor.AvailableBytesOut = outputSize;
                        compressor.NextOut = 0;
                        compressor.Deflate(f);

                        bytesToWrite = outputSize - compressor.AvailableBytesOut ;
                        if (bytesToWrite > 0)
                            ms.Write(output, 0, bytesToWrite);
                    }
                    while (( f == FlushType.None && (compressor.AvailableBytesIn != 0 || compressor.AvailableBytesOut == 0)) ||
                           ( f == FlushType.Finish && bytesToWrite != 0));
                }

                compressor.EndDeflate();

                ms.Flush();
                return ms.ToArray();
            }
        }

      
        private string ZlibCodecDecompress(byte[] compressed)
        {
            int outputSize = 2048;
            byte[] output = new Byte[ outputSize ];
            
            // If you have a ZLIB stream, set this to true.  If you have
            // a bare DEFLATE stream, set this to false.
            bool expectRfc1950Header = false;
            
            using ( MemoryStream ms = new MemoryStream())
            {
                ZlibCodec compressor = new ZlibCodec();
                compressor.InitializeInflate(expectRfc1950Header);
            
                compressor.InputBuffer = compressed;
                compressor.AvailableBytesIn = compressed.Length;
                compressor.NextIn = 0;
                compressor.OutputBuffer = output;

                foreach (var f in new FlushType[] { FlushType.None, FlushType.Finish } )
                {
                    int bytesToWrite = 0;
                    do
                    {
                        compressor.AvailableBytesOut = outputSize;
                        compressor.NextOut = 0;
                        compressor.Inflate(f);

                        bytesToWrite = outputSize - compressor.AvailableBytesOut ;
                        if (bytesToWrite > 0)
                            ms.Write(output, 0, bytesToWrite);
                    }
                    while (( f == FlushType.None && (compressor.AvailableBytesIn != 0 || compressor.AvailableBytesOut == 0)) ||
                           ( f == FlushType.Finish && bytesToWrite != 0));
                }

                compressor.EndInflate();

                return UTF8Encoding.UTF8.GetString( ms.ToArray() );
            }
        }

      
        public static void Usage()
        {
            Console.WriteLine("\nZlibDeflateInflate: <usage statement here>.\n");
            Console.WriteLine("Usage:\n  ZlibDeflateInflate [-arg1 <value>] [-arg2]");
        }


        public static void Main(string[] args)
        {
            try 
            {
                new ZlibDeflateInflate()
                    .Run();
            }
            catch (System.Exception exc1)
            {
                Console.WriteLine("Exception: {0}", exc1.ToString());
                Usage();
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.