How to use the DeflateStream class to compress a file?

September 2nd, 2009 by uCertify Leave a reply »

The steps to use the DeflateStream class to compress a file are as follows:

  1. Import the following namespaces.

    Imports System.IO
    Imports System.IO.Compression
  2. Provide a file name that is to be compressed as follows:

    Dim filename As String= New String(@”C:Myapp empTestFile.txt”)

  3. Create a FileStream object as follows:

    Dim file As FileStream = New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)

  4. Create a buffer as follows:

    Dim buffer(file.Length) As Byte
    Dim count As Integer
    count = file.Read(buffer, 0, buffer.Length)
    file.Close()
  5. Create a MemoryStream object as follows:

    Dim ms As MemoryStream = New MemoryStream()

  6. Create a DeflateStream object as follows:

    Dim mycompress As DeflateStream = New DeflateStream(ms, CompressionMode.Compress, true)

  7. Compress the file as follows.

    mycompress.Write(buffer, 0,buffer.Length)

  8. Close the DeflateStream class as follows:

    mycompress.Close()

  9. Display the result.

    Console.WriteLine(“Original size: {0}, Compressed size: {1}”, buffer.Length, ms.Length)

Like this post? Share it with others
If you like this article, please leave a comment or subscribe this blog via RSS or via e-mail, Bookmark and share through your network. Click the AddThis button below. Thanks.
  • Share/Bookmark
Advertisement

Leave a Reply

uCertify.com | Our Company | Articles | Contact Us | News and Press Release | uCertify India | Entries (RSS)
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD    Cisco Certification: CCNA, CCENT    A+, Network+, Security+ Project+
Oracle Certification: OCP 11g, OCP 10g, OCA 11g, OCA 10g    CIW foundation    EC-212-32,    CISSP    Photoshop ACE CS4    Adobe Flash ACE, PMP, CAPM
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.