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

  1. 'Xceed Zip for .NET - StreamDemo for VB.NET Sample Application
  2. 'Copyright (c) 2000-2002 - Xceed Software Inc
  3. '
  4. '[StreamDemo.vb]
  5. '
  6. 'This console application demonstrates how touse the CompressedStream class
  7. 'to compress/decompress data as it is written to/read from a Stream.
  8. '
  9. 'This file is part of Xceed Zip for .NET. The source code in this file
  10. 'is only intended as a supplement to the documentation and is provided "as is"
  11. 'without warranty of any kind, either expresed or implied.
  12.  
  13. Imports System
  14. Imports System.IO
  15. Imports Xceed.Compression
  16.  
  17.  
  18. Module Module1
  19.  
  20. #Region " Compression Methods "
  21.  
  22.   'Compress sourceFile into destFile using the default compression algorithm (deflate).
  23.   'sourceFile is the name of the file to compress. 
  24.   'destFile is the name of the output file.
  25.   '
  26.   'If destFile exists, it will be overwritten.
  27.   Private Sub DoCompress(ByVal sourceFile As String, ByVal destFile As String)
  28.     Console.WriteLine("Compressing file {0} to {1} ...", sourceFile, destFile)
  29.     Console.WriteLine()
  30.  
  31.     'Open the source file into a FileStream
  32.     Dim sourceStream As New FileStream(sourceFile, FileMode.Open, FileAccess.Read)
  33.  
  34.     'Open the destination file into a FileStream
  35.     Dim destStream As New FileStream(destFile, FileMode.Create, FileAccess.Write)
  36.  
  37.     'Creates a CompressedStream around the destination FileStream so that 
  38.     'all data written to that stream will be compressed.
  39.     Dim compStream As New CompressedStream(destStream)
  40.  
  41.     'Copy the stream
  42.     StreamCopy(sourceStream, compStream)
  43.   End Sub
  44. #End Region
  45.  
  46. #Region " Decompression Methods "
  47.  
  48.   'Decompress sourceFile into destFile using the default compression algorithm (deflate).
  49.   'sourceFile is the name of the file to decompress.
  50.   'destFile is the name of the output file.
  51.   '
  52.   'If destFile exists, it will be overwritten.
  53.   Private Sub DoDecompress(ByVal sourceFile As String, ByVal destFile As String)
  54.     Console.WriteLine("Decompression file {0} to {1} ...", sourceFile, destFile)
  55.     Console.WriteLine()
  56.  
  57.     'Open the source file into a FileStream
  58.     Dim sourceStream As New FileStream(sourceFile, FileMode.Open, FileAccess.Read)
  59.  
  60.     'Open the destination file into a FileStream
  61.     Dim destStream As New FileStream(destFile, FileMode.Create, FileAccess.Write)
  62.  
  63.     'Creates a CompressedStream around the source FileStream so that all
  64.     'data read from that stream will be decompressed.
  65.     Dim compStream As New CompressedStream(sourceStream)
  66.  
  67.     'Copy the stream
  68.     StreamCopy(compStream, destStream)
  69.   End Sub
  70. #End Region
  71.  
  72. #Region " Private Utility Methods "
  73.  
  74.   'Copies the contents of sourceStream into destStream.
  75.   'sourceStream is the input stream.
  76.   'destStream is the output stream.
  77.   '
  78.   'When done, this function closes both streams.
  79.   Private Sub StreamCopy(ByVal sourceStream As Stream, ByVal destStream As Stream)
  80.     Try
  81.       Dim bytesRead As Integer
  82.       Dim buffer(32768) As Byte
  83.  
  84.       bytesRead = sourceStream.Read(buffer, 0, buffer.Length)
  85.  
  86.       While (bytesRead > 0)
  87.         destStream.Write(buffer, 0, bytesRead)
  88.         bytesRead = sourceStream.Read(buffer, 0, buffer.Length)
  89.       End While
  90.  
  91.     Finally
  92.       sourceStream.Close()
  93.       destStream.Close()
  94.     End Try
  95.   End Sub
  96. #End Region
  97.  
  98. #Region " Entry-point and non-compress related methods "
  99.  
  100.   'Entry point for the console application. 
  101.   'The arguments are supplied on the command line
  102.   Sub Main()
  103.     Try
  104.       Console.WriteLine()
  105.       Console.WriteLine("Xceed Zip for .BET - VB.NET StreamDemo")
  106.       Console.WriteLine("======================================")
  107.       Console.WriteLine()
  108.  
  109.       Dim args() As String = GetCommandLineArgs()
  110.  
  111.       If args.Length <> 3 Then
  112.         PrintUsage()
  113.         Return
  114.       End If
  115.  
  116.       Select Case args(0)
  117.         Case "-c"
  118.           DoCompress(args(1), args(2))
  119.         Case "-d"
  120.           DoDecompress(args(1), args(2))
  121.         Case Else
  122.           PrintUsage()
  123.           Return
  124.       End Select
  125.     Catch except As Exception
  126.       Console.WriteLine()
  127.       Console.WriteLine("ERROR: The following exception occured:")
  128.       Console.WriteLine(except.ToString())
  129.       Console.WriteLine()
  130.     End Try
  131.  
  132.     Console.WriteLine("Done!")
  133.   End Sub
  134.  
  135.   'Displays usage guidelines for the command-line application
  136.   Private Sub PrintUsage()
  137.     Console.WriteLine("Usage: StreamDemoVB.exe [-c | -d] sourceFile destFile")
  138.     Console.WriteLine()
  139.     Console.WriteLine("   -c : Compress sourceFile into destFile")
  140.     Console.WriteLine("   -d : Decompress sourceFile into destFile")
  141.   End Sub
  142.  
  143.   'Get the command-line arguments in a function that returns 
  144.   'them in an object containing an array.
  145.   Function GetCommandLineArgs() As String()
  146.     Dim commandLine As String = Microsoft.VisualBasic.Command()
  147.     Dim argument As String
  148.     Dim stringOpened As Boolean = False
  149.     Dim I As Long
  150.     Dim args(0) As String
  151.  
  152.     argument = ""
  153.     For I = 0 To commandLine.Length - 1
  154.       If commandLine.Chars(I) = " " And Not stringOpened Then
  155.         If argument.Length > 0 Then
  156.           ReDim Preserve args(UBound(args) + 1)
  157.           args(UBound(args) - 1) = argument.ToString()
  158.           argument = ""
  159.         End If
  160.       Else
  161.         If commandLine.Chars(I) = """" Then
  162.           stringOpened = Not stringOpened
  163.         Else
  164.           argument = argument & commandLine.Chars(I)
  165.         End If
  166.       End If
  167.     Next
  168.  
  169.     If (argument.Length > 0) Then
  170.       ReDim Preserve args(UBound(args) + 1)
  171.       args(UBound(args) - 1) = argument.ToString()
  172.     End If
  173.  
  174.     If UBound(args) > 1 Then
  175.       ReDim Preserve args(UBound(args) - 1)
  176.     End If
  177.  
  178.     Return args
  179.   End Function
  180. #End Region
  181.  
  182. End Module
  183.