home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipNet.msi / Data1.cab / StreamDemo.cs < prev    next >
Encoding:
Text File  |  2002-02-04  |  5.7 KB  |  182 lines

  1. /*
  2.  * Xceed Zip for .NET - StreamDemo Sample Application
  3.  * Copyright (c) 2000-2002 - Xceed Software Inc.
  4.  * 
  5.  * [StreamDemo.cs]
  6.  * 
  7.  * This console application demonstrates how to use the CompressedStream class
  8.  * to compress/decompress data as it is written to/read from a Stream.
  9.  * 
  10.  * This file is part of Xceed Zip for .NET. The source code in this file 
  11.  * is only intended as a supplement to the documentation, and is provided 
  12.  * "as is", without warranty of any kind, either expressed or implied.
  13.  */
  14.  
  15. using System;
  16. using System.IO;
  17. using Xceed.Compression;
  18.  
  19. namespace Xceed.Compression.Samples.StreamDemo
  20. {
  21.   /// <summary>
  22.   /// Demonstrates how to use the CompressedStream class.
  23.   /// </summary>
  24.   public class StreamDemo
  25.   {
  26.     #region Compression methods
  27.  
  28.     /// <summary>
  29.     /// Compresses <paramref name="sourceFile"/> into <paramref name="destFile"/>, using
  30.     /// the default compression algorithm (deflate).
  31.     /// </summary>
  32.     /// <param name="sourceFile">Name of the file to compress.</param>
  33.     /// <param name="destFile">Name of the output file.</param>
  34.     /// <remarks>
  35.     /// If <paramref name="destFile"/> exists, it will be overwitten.
  36.     /// </remarks>
  37.     private static void DoCompress( String sourceFile, String destFile )
  38.     {
  39.       Console.WriteLine( "Compressing file {0} to {1} ...", sourceFile, destFile );
  40.       Console.WriteLine();
  41.  
  42.       // Open the source file into a FileStream
  43.       FileStream sourceStream = new FileStream( sourceFile, FileMode.Open, FileAccess.Read );
  44.  
  45.       // Open the destination file into a FileStream
  46.       FileStream destStream = new FileStream( destFile, FileMode.Create, FileAccess.Write );
  47.  
  48.       // Creates a CompressedStream around the destination FileStream, so that all
  49.       // data written to that stream will be compressed.
  50.       CompressedStream compStream = new CompressedStream( destStream );
  51.  
  52.       // Copy the stream
  53.       StreamCopy( sourceStream, compStream );
  54.     }
  55.  
  56.     #endregion
  57.  
  58.     #region Decompression methods
  59.  
  60.     /// <summary>
  61.     /// Decompresses <paramref name="sourceFile"/> into <paramref name="destFile"/>, using
  62.     /// the default decompression algorithm (deflate).
  63.     /// </summary>
  64.     /// <param name="sourceFile">Name of the file to decompress.</param>
  65.     /// <param name="destFile">Name of the output file.</param>
  66.     /// <remarks>
  67.     /// If <paramref name="destFile"/> exists, it will be overwitten.
  68.     /// </remarks>
  69.     private static void DoDecompress( String sourceFile, String destFile )
  70.     {
  71.       Console.WriteLine( "Decompressing file {0} to {1} ...", sourceFile, destFile );
  72.       Console.WriteLine();
  73.  
  74.       // Open the source file into a FileStream
  75.       FileStream sourceStream = new FileStream( sourceFile, FileMode.Open, FileAccess.Read );
  76.  
  77.       // Open the destination file into a FileStream
  78.       FileStream destStream = new FileStream( destFile, FileMode.Create, FileAccess.Write );
  79.  
  80.       // Creates a CompressedStream around the source FileStream so that all
  81.       // data read from that stream will be decompressed.
  82.       CompressedStream compStream = new CompressedStream( sourceStream );
  83.  
  84.       // Copy the stream
  85.       StreamCopy( compStream, destStream );
  86.     }
  87.  
  88.     #endregion
  89.  
  90.     #region Private utility methods
  91.  
  92.     /// <summary>
  93.     /// Copies the contents of <paramref name="sourceStream"/> into <paramref name="destStream"/>.
  94.     /// </summary>
  95.     /// <param name="sourceStream">Input stream.</param>
  96.     /// <param name="destStream">Output stream.</param>
  97.     /// <remarks>
  98.     /// When done, this function closes both streams.
  99.     /// </remarks>
  100.     private static void StreamCopy( Stream sourceStream, Stream destStream )
  101.     {
  102.       try
  103.       {
  104.         int bytesRead;
  105.         byte[] buffer = new byte[ 32768 ];
  106.  
  107.         while( ( bytesRead = sourceStream.Read( buffer, 0, buffer.Length ) ) > 0 )
  108.           destStream.Write( buffer, 0, bytesRead );
  109.       }
  110.       finally
  111.       {
  112.         sourceStream.Close();
  113.         destStream.Close();
  114.       }
  115.     }
  116.  
  117.     #endregion
  118.  
  119.     #region Entry-point and non-compress related methods
  120.  
  121.     /// <summary>
  122.     /// Entry-point for the console application.
  123.     /// </summary>
  124.     /// <param name="args">
  125.     /// Arguments supplied on the command line.
  126.     /// </param>
  127.     public static void Main(string[] args)
  128.     {
  129.       try
  130.       {
  131.         Console.WriteLine();
  132.         Console.WriteLine( "Xceed Zip for .NET - StreamDemo" );
  133.         Console.WriteLine( "====================================" );
  134.         Console.WriteLine();
  135.  
  136.         if( args.Length != 3 )
  137.         {
  138.           PrintUsage();
  139.           return;
  140.         }
  141.  
  142.         switch( args[ 0 ] )
  143.         {
  144.           case "-c":
  145.             DoCompress( args[ 1 ], args[ 2 ] );
  146.             break;
  147.  
  148.           case "-d":
  149.             DoDecompress( args[ 1 ], args[ 2 ] );
  150.             break;
  151.  
  152.           default:
  153.             PrintUsage();
  154.             break;
  155.         }
  156.       }
  157.       catch( Exception except )
  158.       {
  159.         Console.WriteLine();
  160.         Console.WriteLine( "ERROR: The following exception occured:" );
  161.         Console.WriteLine( except.ToString() );
  162.         Console.WriteLine();
  163.       }
  164.  
  165.       Console.WriteLine( "Done!" );
  166.     }
  167.  
  168.     /// <summary>
  169.     /// Displays usage guidelines for the command-line application.
  170.     /// </summary>
  171.     private static void PrintUsage()
  172.     {
  173.       Console.WriteLine( "Usage: StreamDemo.exe [-c | -d] sourceFile destFile\n" );
  174.       Console.WriteLine();
  175.       Console.WriteLine( "  -c : Compress sourceFile into destFile" );
  176.       Console.WriteLine( "  -d : Decompress sourceFile into destFile" );
  177.     }
  178.  
  179.     #endregion
  180.   }
  181. }
  182.