Important alert: (current site time 7/16/2013 3:33:36 AM EDT)
 

winzip icon

Huffman Compression/Decompression

Email
Submitted on: 8/25/2000 11:17:19 AM
By: Fredrik Qvarfort 
Level: Intermediate
User Rating: By 61 Users
Compatibility: VB 5.0, VB 6.0
Views: 57022
(About the author)
 
     This is a Compression/Decompression routine using Huffman Encoding/Decoding, and works best on normal text files. The code is *highly* optimized, and to show how fast this is I can mention another code sample found here on planetsourcecode (from August 1st 2000) which also uses Huffman Encoding. That code took 127 seconds to compress a 1.8mb textfile, but with my code this takes less than 1 second!! (that's an approvement of 12700%!!). That other code was worth 7 excellent points, how many is this worth? ;)

 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/25/2000 8:52:45 PMFredrik Qvarfort

This code works for *ALL* kinds of data types, it supports all the ASCII characters. But the best ratio is given on textfiles, where some characters appear more often than others and some characters does not appear at all. Binary files often contains all ASCII values and therefore the Huffman Algorithm can not compress the data much (or at all).
(If this comment was disrespectful, please report it.)

 
9/3/2000 6:51:18 PMmichael

Hi Frederik,
I have compressed a file that was
>6 MB.
The Result :
Source size = 6117120 bytes
Dest size = 1534173 bytes
Ratio = 25%
Time spend = 1,5632 sec.
>
I mean this is a exelent work from you
but I have rated so.
I´m looking your next work impatient :-)
>
by michael
(If this comment was disrespectful, please report it.)

 
9/7/2000 10:12:09 AMYaron Budowski

Fredrik: You said you wanted to post a Lempel-Ziv Compression Program.
Why not use both Huffman and Lempel-Ziv? This is actually called "Deflation" (What used in Winzip).
Here are some sites for this compression method:

http://www.cdrom.com/pub/infozip/zlib/
http://www.cdrom.com/pub/infozi p/zlib/zlib_docs.html
http://www.faqs.org/faqs/compression-faq/
http://www.cdrom.com/pub/infoz ip/zlib/rfc-deflate.html
http://www.internz.com/compression-pointers.html

Hope
that helps,

Yaron B.

Budowski@bezeqint.net
(If this comment was disrespectful, please report it.)

 
9/9/2000 10:48:31 AMFredrik Qvarfort

I might look into that Deflate routine (but i am using the ZLib DLL already and this would be probably 7-8x slower in VB so I don't know..)

For you who requested a self-extracting EXE you can use the code 11335 and replace the .bas file with my .cls file to improve performance.

"You can put more afford (effort?) to enhance it,
it may bring a great challenging for
you..."

Is it *possible* to improve upon this code? I think not (at least not in VB). I believe this is actually the fastest possible solution. Then you can design 1000 different implementations but that's not what a .cls file is designed for. ;)
(If this comment was disrespectful, please report it.)

 
9/11/2000 11:01:57 AMFredrik Qvarfort

I uploaded a new version, where the only bug I could find is fixed. I also added some error trapping so now you can not try to compress a file that does not exist (if you tried it before you got a Subscript-out-of-bounds error). New is also support for a Progress Event! ;)

(If this comment was disrespectful, please report it.)

 
9/11/2000 4:58:22 PMTom Williams

Will this zip be compatible for unzipping with PKZIP, WINZIP, etc?
(If this comment was disrespectful, please report it.)

 
9/11/2000 5:10:40 PMTom Williams

FYI - Fredrik

I noticed that your latest screen shot shows a longer compression duration of the same file over the older screen shot.

The previous screen shot showed a compressed time of 0,988125 s and of course the current screen shot shows a time of 1,318125 s.

This seems to be a roughly 25% decrease in performance.
(If this comment was disrespectful, please report it.)

 
9/11/2000 6:08:47 PMFredrik Qvarfort

Some of the decrease in performance can probably be found in the new Event that is raised, if you comment everything that has with the new event to do you might get some better performance. I might have run that old run before I added the simple crc too. I also had my CPU at a higher Mhz but recently I had to downclock it 'cause I forgot to plugin the fan a couple of weeks ago. Doh. ;)

Unfortunately this compression is not compatible with Winzip (as that uses a LZ77 routine just like ZLib on top of the Huffman? Not sure).
(If this comment was disrespectful, please report it.)

 
9/13/2000 4:04:17 PMMaleficus

Really a great code. Im waiting for your other works.
(If this comment was disrespectful, please report it.)

 
11/9/2000 3:17:35 PMChris Howie

This is nice code, but can you make it search to more than two leafs? Maybe you could add a parameter
(If this comment was disrespectful, please report it.)

 
11/20/2000 5:56:14 AMLouise

Could you give me your email address, i have something want to discuss with you and want to ask for help. it was really urgent.
(If this comment was disrespectful, please report it.)

 
11/21/2000 4:58:43 PMTim Miron

Bravo! this is truely ingenius work you've posted here, I have a question, how d you think this compression does on BMP files compared to a RLE compcression (keep in mind that most BMPs have at least a fair bit of repition, depending on the graphic.)???
(If this comment was disrespectful, please report it.)

 
11/22/2000 7:21:01 AMIcecubeRyder

Is there any way of password protecting these files as in winzip?

(If this comment was disrespectful, please report it.)

 
11/22/2000 11:10:14 AMFredrik Qvarfort

Tim_Miron:

I believe you will get better performance from the RLE compression when dealing with large bitmaps, and specially those with high color-depth (as you said lot of repeting data). I saw a RLE post on PSC a week or so ago you could compare with, it's at..

http://www.planet-source-code.com/xq/ASP/txtCodeId.12790/lngWId.1/qx/vb/scri pts/ShowCode.htm

(If this comment was disrespectful, please report it.)

 
2/4/2001 10:59:58 AMNimit Sarup

Could you kindly explain what are the progress values,and how come the values are 7,5 and 88 for encoded function;and 89 and 11 for decoded function.Could you kindly explain how is your algorithm optimised OR different from the conventional algorithm.
I am a final year engineering student and your program has come in real handy.If it is possible for you to find time please mail me at nimitsarup@Hotmail.com.
Thanks a million.

(If this comment was disrespectful, please report it.)

 
4/9/2001 8:10:43 AMRyan

Hmm. found a bug in the source code...
(If this comment was disrespectful, please report it.)

 
4/9/2001 8:12:54 AMRyan

Weird... posting screwed up... Let me try again.
In the sub EncodeByte(),
If (ByteLen = 0) Then
ReDim Preserve ByteArray(0 To ByteLen + 3)
If (ByteLen > 0) Then ...

If ByteLen = 0, why is there a need for
the 2nd "If"? Since it won't be used at
all.

After which, there is
"If ((lLength = 0) Or (lLength > ByteLen)) Then"
And that is before adding the CRC and other data,
why not put that near the end? And why
just "lLength > ByteLen" since even if
lLength = ByteLen, the "compressed" length
will be BIGGER than the original file.

Regards
(If this comment was disrespectful, please report it.)

 
4/16/2001 4:49:04 AMAAA

First off, I'd like to praise you for the wonderful piece of code!

I did some comparisons of the compression ratios between RLE and Huffman. (Using my RLE example: http://www.planet-source-code.com/xq/ASP/txtCodeId.8418/lngWId.1/qx/vb/scripts/ShowCode. htm )

On the images I've used, Huffman produced better compression most of the time. However, I also found that compressing a file with RLE and then with Huffman yielded even better results! Results comparable with JPEG for large enough files! (Well this is quite reasonable since the JPEG algorithm uses RLE and/or Huffman after doing the quantizations)

Anyways keep up the great work.
(If this comment was disrespectful, please report it.)

 
5/14/2001 2:56:00 PMRudy Alex Kohn

What can i say other than this >> This piece of code is great!
(If this comment was disrespectful, please report it.)

 
6/18/2001 8:03:14 AMhenawy

how do i make the program recognize the original extention of the file after decompression process(.jpg,.exe ....etc) ???? after i decompress the file i can't know what it was and i dont wana write saol.txt.new and all these things...p.s. its a gr8 code
(If this comment was disrespectful, please report it.)

 
6/18/2001 8:08:14 AMhenawy

if anyone knows how plzzzzzzz contact me ..i need to know and by the way..do u have any documentaions explaining that algorithm and y u choosed these valuse i'll apreciate if u send it to me sir..thanks alot
(If this comment was disrespectful, please report it.)

 
1/9/2002 12:55:51 AMChris

Let's see if we can get PSC to keep my post as one piece this time... when compressing an 8,192 byte of all char code 255, an Illegal Page Fault is brought up. I checked the code, and a CopyMemory call is causing this.
(If this comment was disrespectful, please report it.)

 
3/13/2002 11:14:04 AMChad M. Kovac

Uploading a modified version of this code that will function with Microsoft Access as a Module. - I'm working on the code to further enhance it allowing multiple files to be compressed into a single file which is where compression routines really shine I think. Search my submissions to view the MS Access code.
(If this comment was disrespectful, please report it.)

 
9/4/2002 11:11:34 AMMark Robert Strange

This code is so sweet it'll make you cry. 5 Globes
(If this comment was disrespectful, please report it.)

 
10/6/2002 9:49:57 AM

I have only one question:

Is it possible to change this class file to unpack a file to memory?

Imagine this:

I have about 1000 texts packed with your program (1 text per file) and they are recorded in a cd-rom, well i NEED to unpack them directly to a textbox/listbox/richtextbox without creating any temporary file first, which means that the unpack routine, should NOT write any information unless to the text/list/rich box.

this may seem a little to much to ask, but since your code is so GREAT i was expecting that maybe you could be so kind to arrange something like this...
The only this i could get was copying the ByteArray() with Clippboard.Copy to memory :( and not the actual unpack data in ascii format but binary :(

is there any function/class to convert binary data to string?

hope that you ALL can help,

Best Regards
RootShell

(RootShell@netcabo.pt)
(If this comment was disrespectful, please report it.)

 
11/10/2002 3:21:36 AMJosh Yu

This code has gone a long way. I was toying with it and yes, you can still improve the speed of compression/decompression by replacing he CharCount array with a Dictionary, in this case you don't have to parse the entire ascii character set for non-zero counts. There are several FOR-NEXT loops that can still be optimize, including the progress bar. For both text and binary file test, the time it takes to compress and decompress has been reduced an average of 46.7%. Another issue with this code is, it reads all of the file data in memory, what if we are dealing with a 10MB file? An alternative approach is to process the file in a 2Kb file read increment. The code runs faster too and there's no degradation of performance. At some point, speed is measured in ticks and not in seconds.
(If this comment was disrespectful, please report it.)

 
3/6/2003 3:56:19 PMSudif

Very useful. Thanks for sharing.
(If this comment was disrespectful, please report it.)

 
9/2/2003 6:58:10 AM

Just tried your compression code on some access databases. Which are being compressed from 70k to about 27k, which is great. But after uncompressing, Access and my VB application throws an error, "Unrecognised database format". Have you had any previous reports of this problem. Cheers Nick
(If this comment was disrespectful, please report it.)

 
9/16/2003 6:08:03 PMmike payne

I am recieving errors trying to use this code. I have my own file type which i have saved and the compression does not work. The program returns an error here :
t = LOF(Filenr)
ReDim ByteArray(0 To LOF(Filenr)) '- 1)
Get #Filenr, , ByteArray()

Could you help me with this please? THanks for your time.

(If this comment was disrespectful, please report it.)

 
1/4/2005 1:23:06 PM

So simple to pick up the module and to reuse
(If this comment was disrespectful, please report it.)

 
6/17/2005 9:14:05 AM

Source: 54kb
Destination 150kb

weird...:S
(If this comment was disrespectful, please report it.)

 
7/27/2005 6:47:46 AMAndrew Vos

Any chance of converting to vb.net???
please say yes :>
(If this comment was disrespectful, please report it.)

 
7/27/2005 2:13:14 PMAndrew Vos

Why not? do u post on Code Project? Im sorry im just too far away from VB6 to regress. Tried twice today and couldnt handle it. :)
(If this comment was disrespectful, please report it.)

 
2/22/2006 11:20:50 AMJirapun

This code is easy to use for Learn. Thanks
(If this comment was disrespectful, please report it.)

 
3/27/2006 5:35:31 PMSPY-3

Works great on compressing larger text files smaller ones it makes bigger but it does real well on the large ones! 5 globes!
(If this comment was disrespectful, please report it.)

 
12/22/2006 6:00:09 AMBabak K.

Yeah, sounds fast enough!
i had a question and emailed it to You ;)
(If this comment was disrespectful, please report it.)

 
1/18/2007 7:43:08 PMPhill

Works Great (Except for 0 Sized files)
If anyone else has this problem, here is my fix.

If (ByteLen <= 4) Then
ReDim ByteArray(0)
Else
Call CopyMem(ByteArray(0), ByteArray(4), ByteLen - 4)
ReDim Preserve ByteArray(0 To ByteLen - 5)
End If

(If this comment was disrespectful, please report it.)

 
2/1/2007 10:55:35 AMdatatransfers667

Call me ignorant, but what does the CopyMem do in you code? I love your code and the CopyMem functions are the only ones I do not understand.
(If this comment was disrespectful, please report it.)

 
5/13/2008 8:10:55 PMRoby66

Great work! Thanks for sharing!
I give you 5 globes
(If this comment was disrespectful, please report it.)

 
8/13/2011 2:56:40 PMHuffmanUser

This code has received great reviews, but it is quite dated. I've tried to port it to VB.NET, but I've encountered a number of significant problems.

Does anyone have a version that is more recent than the code that Qvarfort posted?

Thanks.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.