save up to 40%

How to use GzipStream class to compress a file?

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

How to use GzipStream class to compress a file?

Rating:

The steps to use GzipStream 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 emp\TestFile.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 GzipStream object as follows:

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

  7. Compress the file.

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

  8. Close the GzipStream class.

    mycompress.Close()

  9. Display the result.

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


Rating:



Other articles

Click here to Article home

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