Click here to Skip to main content
13,346,966 members (72,757 online)
Click here to Skip to main content

Tagged as


38 bookmarked
Posted 29 Aug 2010

Fast and Simple Huffman Compressor

, 2 Apr 2012
Fast and simple huffman compressor
/*                                                                            */
/*  Sample console unit for simple_Huffman class  v.3.0   (c) 2012 Jan Mojzis */
/*      Notes:                                                                */
/*            x console syntax: <input> <output> <c/d> /compress/decompress   */
/*                                                                            */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "huffman.h"

int main(int argc, char *argv[])
    if (argc < 4) { printf("syntax: program <INPUT> <OUTPUT> <c/d>\n"); system("PAUSE"); return 0; }
    FILE *finput, *foutput;
    BYTE *input, *output = NULL;
    BYTE op = (BYTE) argv[3][0];
	int delka;
    simple_Huffman *huf = new simple_Huffman();
    // open files
    finput = fopen(argv[1], "rb");
    if (!finput) { printf("error upon opening %s\n", argv[1]); system("PAUSE"); return 0; }
    foutput = fopen(argv[2], "wb");
    if (!foutput) { printf("error upon opening %s\n", argv[2]); system("PAUSE"); fclose(finput); return 0; }

    // get the length of input 
    fseek(finput, 0L, SEEK_END);
    delka = ftell(finput);
    fseek(finput, 0L, SEEK_SET);
    // read input 
    input = new BYTE[delka];
    fread(input, 1, delka, finput);    
    clock_t before,after;
    if (op == 'c')
    before = clock();       
    int outsize = huf->Compress(input, delka);
	output = huf->getOutput();
    printf("time: %.2lfs\n", ((double)clock()-before) / 1000.0);
       fwrite(output, outsize, 1, foutput);
    if (op == 'd')
    before = clock();
    int outsize = huf->Decompress(input, delka);
	output = huf->getOutput();
    printf("time: %.2lfs\n", ((double)clock()-before) / 1000.0);
       fwrite(output, outsize, 1, foutput);
	// test file compression
	int outsize;
	//outsize = huf->CompressFile("laclavik_information_extraction.ppt", "laclavik_information_extraction.ppt.pak");
	//outsize = huf->CompressFile("LIC.txt", "LIC.txt.pak");
	//outsize = huf->CompressFile("esej_clanok.rtf", "esej_clanok.rtf.pak");
	//outsize = huf->DecompressFile("LIC.txt.pak", "LIC.txt.dek2");
	//outsize = huf->DecompressFile("esej_clanok.rtf.pak", "esej_clanok.rtf.dek2");
	//outsize = huf->DecompressFile("laclavik_information_extraction.ppt.pak", "laclavik_information_extraction.ppt.dek2");
    delete huf;
    delete input;
    return EXIT_SUCCESS;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jan Mojzis
Software Developer (Junior) SOMI Systems
Slovakia Slovakia
Got 1st and 2nd degree from computer science in 2011 on university of Matej Bel, city of Banska Bystrica. Currently work for computer-oriented company SOMI Systems in Slovakia.
Working for SOMI Systems a.s.,

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 2 Apr 2012
Article Copyright 2010 by Jan Mojzis
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid