ARTICLE

How to: compress and decompress data

Posted by Muhammad Shakir Articles | VB.NET Articles April 24, 2008
This article shows that how we can compress and decompress our data.
 
Reader Level:

"Compression and Decompression" is most common word when you transfer your data from one place to another. Sometimes you have large size of data that can not transfer (e.g. mail attachment). You use a software (Zip) to compress your data and able to transfer it. And on other end, you decompress your data and get the original data.
 
But if you are developing your own application that transfer data from one place to another (FTP) that can take much time to transfer data because of size of data. At that time, when you want to embed zipping method in your application so that you just pass the data and after zipping, data can transfer; Zip software will not be helpful.

I search a lot of material, how to do; I got some code. I modify that code according to my requirement and develop two function; one compress data and other decompress it. You just need to pass your data file with complete path and it will compress the file & decompress too. Even you may use your own extension and embed it in your application.
 
System.IO.Compression is package and using GZip Compression & decompression method.

Use it and Enjoy!
 
Compression

Public Shared Sub Compress(ByVal strPath As String)

    Dim current As DateTime

    Dim dstFile As String = ""

    ' "C:\\temp\\compressed-file.gzip";

    Dim fsIn As FileStream = Nothing

    Dim fsOut As FileStream = Nothing

    Dim gzip As GZipStream = Nothing

    Dim buffer As Byte()

    Dim count As Integer = 0

    Try

        current = DateTime.Now

        dstFile = Application.StartupPath + "\" + "Request" + current.Day.ToString() + current.Month.ToString() + current.Year.ToString() + current.Hour.ToString() + current.Minute.ToString() + current.Second.ToString() + ".zip"

        ' may use own extension

        fsOut = New FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None)

        gzip = New GZipStream(fsOut, CompressionMode.Compress, True)

        fsIn = New FileStream(strPath, FileMode.Open, FileAccess.Read, FileShare.Read)

        buffer = New Byte(fsIn.Length - 1) {}

        count = fsIn.Read(buffer, 0, buffer.Length)

        fsIn.Close()

        fsIn = Nothing

 

        ' compress to the destination file

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

    Catch ex As Exception

        ' handle or display the error

        System.Diagnostics.Debug.Assert(False, ex.ToString())

    Finally

        If gzip IsNot Nothing Then

            gzip.Close()

            gzip = Nothing

        End If

        If fsOut IsNot Nothing Then

            fsOut.Close()

            fsOut = Nothing

        End If

        If fsIn IsNot Nothing Then

            fsIn.Close()

            fsIn = Nothing

        End If

    End Try

End Sub

Decompression

Public Shared Sub Decompress(ByVal strPath As String)

    Dim current As DateTime

    Dim dstFile As String = ""

 

    Dim fsIn As FileStream = Nothing

    Dim fsOut As FileStream = Nothing

    Dim gzip As GZipStream = Nothing

    Const bufferSize As Integer = 4096

    Dim buffer As Byte() = New Byte(bufferSize - 1) {}

    Dim count As Integer = 0

 

    Try

        current = DateTime.Now

        dstFile = Application.StartupPath + "\" + current.Day.ToString() + current.Month.ToString() + current.Year.ToString() + current.Hour.ToString() + current.Minute.ToString() + current.Second.ToString() + ".txt"

        fsIn = New FileStream(strPath, FileMode.Open, FileAccess.Read, FileShare.Read)

        fsOut = New FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None)

        gzip = New GZipStream(fsIn, CompressionMode.Decompress, True)

        While True

            count = gzip.Read(buffer, 0, bufferSize)

            If count <> 0 Then

                fsOut.Write(buffer, 0, count)

            End If

            If count <> bufferSize Then

                ' have reached the end

                Exit While

            End If

        End While

    Catch ex As Exception

        ' handle or display the error

        System.Diagnostics.Debug.Assert(False, ex.ToString())

    Finally

        If gzip IsNot Nothing Then

            gzip.Close()

            gzip = Nothing

        End If

        If fsOut IsNot Nothing Then

            fsOut.Close()

            fsOut = Nothing

        End If

        If fsIn IsNot Nothing Then

            fsIn.Close()

            fsIn = Nothing

        End If

    End Try

End Sub

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# Corner (http://www.c-sharpcorner.com/).

Login to add your contents and source code to this article
share this article :
post comment
 

i have one .dat file in both format compress and decompress + decompress tool also. i want to know what kind of compression is this and how to compress it again same like decompressed file can anyone help me in this regards here are links http://www.4shared.com/file/PhqxsyC3/dbcivilization_compress_.html http://www.4shared.com/file/uwRprFkN/dbcivilization_Decompress_.html Thanx in Advance

Posted by Naveen Daniya Nov 02, 2011

It depends either you have write permission on ftp server. It will be same as we do on local, if you know algorithm through which it had compressed

Posted by Muhammad Shakir Feb 15, 2011

how to decompress on FTP server from loacal machine using .net

Posted by sant desh Feb 04, 2011

hi. I have been attached source file that would be easy to zip and unzip the file. Above error was a path issue. I hope, it will resolve now. 


Thanks

Posted by Muhammad Shakir Oct 24, 2010

hi i am getting error in this line 
dstFile = Application.StartupPath + "\\" + "Request" + current.Day.ToString() + current.Month.ToString() + current.Year.ToString() + current.Hour.ToString() + current.Minute.ToString() + current.Second.ToString() + ".zip"// may use own extension

the error is 
Error 1 An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Application.get' C:\Documents and Settings\VISHANK\My Documents\Visual Studio 2008\WebSites\WebSite8\fileupload.aspx.cs 47 23 C:\...\WebSite8\


could you tell me the solution 

Posted by vishank desai Oct 24, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor