How to the compress file

Following code explain how to compress file.

Following code explain how to compress file.

Public Sub CompressGivenFile(ByVal srcFile As String, ByVal destFile As String)

Dim buffer As Byte() = Nothing
Dim sourceStream As FileStream = Nothing
Dim destinationStream As FileStream = Nothing
Dim compressedStream As GZipStream = Nothing

sourceStream = New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)

buffer = New Byte(sourceStream.Length) {}
Dim checkCounter As Integer = sourceStream.Read(buffer, 0, buffer.Length)
destinationStream = New FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)

compressedStream = New GZipStream(destinationStream, CompressionMode.Compress, True)

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

Catch ex As ApplicationException
MessageBox.Show(ex.Message, "An Error occured during compression", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Related Articles

Upload and Compressed file

By using the code you can upload file and then compressed it. It Retrieve file information and upload to server.

More articles: Compress files Decompress files Zip files


Author: vijetha10 Jun 2010 Member Level: Gold   Points : 0

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: