Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Why a VB.NET implementation of LZW compression?

I am working with a team of VB.NET developers on a large project, and it is best for us to have all of the components in native VB to simplify maintainence and upgrades. I also thought this might be a good introduction to LZW for people who don't know C.

Credit where Credit is Due

The source code is a nearly direct port of the LZW implementation by Mark Nelson on his web site on C. I even retained many of his comments. Be sure to look at his code here and view his C implementation here.

His original article was published in the October, 1989 issue of Dr. Dobb's Journal, which implies that Mark is very likely at least as ancient as I am.

Demo Code

Running the Code

Start a new Windows Forms project in VB.NET. Add clsLZW.vb to the project. Draw a button in the center of the main form, view the form's code, and paste the following code just above the form's End Class statement:

Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles Button1.Click
    ' test the LZW program:

    ' Place a bunch of files in c:\testdir

    ' Create c:\testdir\lzw and c:\testdir\postlzw

    ' Run this program

    ' test it with a command file such as:

    ' c:

    ' cd \testdir

    ' for %%1 in (*.*) do fc /b %%1 

    '       postlzw\%%1 >> results.txt


    Button1.Enabled = False          
    Dim di As New IO.DirectoryInfo("c:\testdir")
    For Each fi As IO.FileInfo In di.GetFiles
        ' print filename

        Dim g As Graphics = Me.CreateGraphics
        g.FillRectangle(New SolidBrush(Me.BackColor), _
                        0, 0, Me.Width, 100)
        g.DrawString(fi.Name, Me.Font, Brushes.Black, 0, 0)
        g.Dispose()

        ' compress...

        Dim lzw1 As New clsLZW
        lzw1.brInput = _
          New IO.BinaryReader(IO.File.Open(fi.FullName, _
          IO.FileMode.Open))
        lzw1.bwOutput = _
             New IO.BinaryWriter(IO.File.Open("c:\testdir\lzw\" & _
             fi.Name & ".lzw14", _
             IO.FileMode.OpenOrCreate, IO.FileAccess.Write))
        lzw1.compress()
        lzw1.brInput.Close()
        lzw1.bwOutput.Close()

        ' decompress
        Dim lzw2 As New clsLZW
        lzw2.brInput = _
             New IO.BinaryReader(IO.File.Open("c:\testdir\lzw\" _
             & fi.Name & ".lzw14", IO.FileMode.Open))
        lzw2.bwOutput = _
             New IO.BinaryWriter(IO.File.Open("c:\testdir\postlzw\" _ 
             & fi.Name, IO.FileMode.OpenOrCreate, _
             IO.FileAccess.Write))
        lzw2.expand()
        lzw2.brInput.Close()
        lzw2.bwOutput.Close()
    Next
    Button1.Enabled = True
End Sub

Testing the Results

To test, create the file structure specified by the comments in the above Sub. You should choose a variety of files.

The batch command 'For %%1...' uses the FileCompare utility (fc) to verify that the uncompressed files match the originals.

If you choose to run it outside of the batch or command file, and at a command prompt, change the three %% to % so it will work.

For %1 in (*.*) do fc /b %1 postlzw\%1 >> results.txt        

Usage

Refer to the sample code for usage. I have not tested multiple compressions per instantiation of the class, so it is best to create a new instance for each compression or decompression that you wish to complete.

I had to use this to build a multi-file archive, so the calling program maintains control of the streams. Multiple files can be written to the same stream without closing it, however, the input stream is exausted until the end of the file is reached. This can be changed with minor modifications to prevent the need to write temporary files.

Best wishes and good luck with your VB coding!

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralLZW coding
neelu.j
0:26 10 Dec '08  
hi, i want LZW algorithm coding in verilog................ can any one help please.......... i want to do it as my project. if you can help me, please send it to neelu_12345123@yahoo.co.in Smile
GeneralRe: LZW coding
prasannacse528
22:16 26 Feb '09  
http://www.codeproject.com/KB/recipes/VB_LZW.aspx[^]
GeneralGreat Work
TylerGall
17:29 23 Mar '07  
Really great work on this. By the way, you work with my mom, heh. You work at MWA, correct? Great work, and good luck on your project. Big Grin
GeneralWhere download vb.net study document?
xjf15
22:38 11 Dec '06  

I am a newible study vb.net(2005),Where download study vb.net study document?

xjf15
GeneralRE: Compressed file came out larger!
Richard Vantrease
16:19 14 Sep '06  
I received the same results. I almost doubled the size of my files.

It seems like this might be a good method to compress text type files, but possibly not binaries.

Maybe it would be good to add some filter logic to screen out pre-compressed file formats.

Richard Vantrease
GeneralRe: RE: Compressed file came out larger!
Behind The Scene
7:49 4 Jan '07  
Screening out precompressed data? Not only would that make the compressor slower, but there is no way to determine what is precompressed before compression. You could use an extension, but I would trust that.

ROFLOLMFAO

GeneralGood work!
Eric Engler
6:36 28 Aug '06  
Gzip and Deflate are built into version 2 of the Framework (see the System.IO.Compression namespace), but it's cool to see this low-level code implemented in VB. Thanks!
GeneralCompressed file came out larger!
Dankarmy
5:27 27 Aug '06  
I am compressing a 34mb file and it came out at 48mb!

The file was a .flac music file.

Also it took ages to compress.
GeneralRe: Compressed file came out larger!
jmueller
5:17 28 Aug '06  
I'm not sure what exactly you expect to happen when you compress something that's already compressed, using an algorithm specifically designed for music compression. A general-purpose (and fairly aged) compression algorithm is supposed to do a better job, after the fact?
GeneralRe: Compressed file came out larger!
ChipLeader
5:36 26 Jun '07  
Sure but when you use the zip or rar or ace or uha algorithme you NEVER come up with a bigger file at the end (or something like 0,02% more)
Not a 1:1,5 factor.D'Oh!
Anyway, very good for txt, doc and xls filesBig Grin
Clap Clap *applause* for this low level implementation

THUG4LIFE


Last Updated 24 Aug 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010