Quick Search for:  in language:    
12700,routine,using,Huffman,works,best,normal
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Visual Basic Stats

 Code: 3,149,602. lines
 Jobs: 144. postings

 How to support the site

 
Sponsored by:

 

You are in:

 
 
PDC 2003: Make the connection
Login



Latest Code Ticker for Visual Basic.
Click here to see a screenshot of this code!HistoryWriter Add-In V.1
By Zvonko Kostovski on 9/3

(Screen Shot)

Click here to see a screenshot of this code!Control Winamp
By Dennis Wollentin on 9/3

(Screen Shot)

Fake Virus Version 1.0
By C.J. Laing on 9/3


][String Encoder
By Andron Smth on 9/3


Font Catalog
By Bill Adams on 9/3


professional search
By Ziad Said on 9/3


Click here to see a screenshot of this code!Drew's Meta Tag Generator
By Drew Phillips on 9/3

(Screen Shot)

Fast BruteForce Class
By §e7eN on 9/3


Click here to see a screenshot of this code!Lotus Domino Exploit Scanner
By §e7eN on 9/3

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Huffman Compression/Decompression

Print
Email
 
winzip icon
Submitted on: 8/25/2000 11:17:19 AM
By: Fredrik Qvarfort 
Level: Intermediate
User Rating: By 48 Users
Compatibility:VB 5.0, VB 6.0

Users have accessed this code 16392 times.
 
(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 Winzipto 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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 PM:Fredrik 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).
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/3/2000 6:51:18 PM:michael
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/7/2000 10:12:09 AM:Yaron 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/info
zip/zlib/
http://www.cdrom.com/pub/info
zip/zlib/zlib_docs.html
http://www.faqs
.org/faqs/compression-faq/
http://www.c
drom.com/pub/infozip/zlib/rfc-deflate.ht
ml
http://www.internz.com/compression-p
ointers.html
Hope that 
helps,
Yaron 
B.
Budowski@bezeqint.net
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/9/2000 10:48:31 AM:Fredrik 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. ;)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/11/2000 11:01:57 AM:Fredrik 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! ;)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/11/2000 4:58:22 PM:Tom Williams
Will this zip be compatible for 
unzipping with PKZIP, WINZIP, etc?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/11/2000 5:10:40 PM:Tom 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/11/2000 6:08:47 PM:Fredrik 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).
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/13/2000 4:04:17 PM:Maleficus
Really a great code. Im waiting for 
your other works.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/9/2000 3:17:35 PM:Chris Howie
This is nice code, but can you make it 
search to more than two leafs?  Maybe 
you could add a parameter 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/20/2000 5:56:14 AM:Louise
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. 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/21/2000 4:58:43 PM:Tim 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.)???
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/22/2000 7:21:01 AM:IcecubeRyder
Is there any way of password protecting 
these files as in winzip?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/22/2000 11:10:14 AM:Fredrik 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.co
m/xq/ASP/txtCodeId.12790/lngWId.1/qx/vb/
scripts/ShowCode.htm
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/4/2001 10:59:58 AM:Nimit 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.
             
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/9/2001 8:10:43 AM:Ryan
Hmm. found a bug in the source code...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/9/2001 8:12:54 AM:Ryan
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/16/2001 4:49:04 AM:AAA
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/S
howCode.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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/14/2001 2:56:00 PM:Rudy Alex Kohn
What can i say other than this >> This 
piece of code is great!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/18/2001 8:03:14 AM:henawy
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/18/2001 8:08:14 AM:henawy
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/9/2002 12:55:51 AM:Chris
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/13/2002 11:14:04 AM:Chad 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/4/2002 11:11:34 AM:Mark Robert Strange
This code is so sweet it'll make you 
cry. 5 Globes
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/10/2002 3:21:36 AM:Josh 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. 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/6/2003 3:56:19 PM:Sudif
Very useful. Thanks for sharing.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Visual Basic Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.