资源简介

基于Qt实现的霍夫曼编码的压缩解压缩程序。压缩、解压缩采用单独一个线程,ui界面一个线程。

资源截图

代码片段和文件信息

#include “huffman.h“

Huffman::Huffman(QThread *parent) :
    QThread(parent)
{
    for(int k=0;k<256;k++)
    {
        for(int mask=128;mask>0;mask/=2)
        {
            if((k&mask)==0) dic[k].append(“0“);
            else dic[k].append(“1“);
        }
    }
}
void Huffman::run()
{
    if(isCompress)
    {
        this->ComAnalyse();
        this->GenerateCompressFile();
        emit message(“Compress Complete!“);
    }
    else
    {
        this->DecomAnalyse();
        emit message(“Decompress complete!“);
    }
}

void Huffman::Compress(QString comFile QString after)
{
    if(comFile==after)
    {
        emit error(“The input file name and the output file name cann‘t be the same!“);
        return;
    }
    inFile.setFileName(comFile);
    outFile.setFileName(after);
    if(!CheckFile()) return;
    isCompress=true;
    this->start();

}
void Huffman::Decompress(QString decomFile QString after)
{
    if(decomFile==after)
    {
        emit error(“The input file name and the output file name cann‘t be the same!“);
        return;
    }
    inFile.setFileName(decomFile);
    outFile.setFileName(after);
    if(!CheckFile()) return;
    isCompress=false;
    this->start();
}

bool Huffman::CheckFile()
{
    if(!inFile.exists())
    {
        emit error(“File doesn‘t exist!“);
        return false;
    }
    if(outFile.exists())
    {
        return QMessageBox::question(NULL“OverWrite?““File exists! OverWrite it?“);
    }
    return true;
}
void Huffman::ComAnalyse()
{
    statisticMap.clear();
    char c;
    inFile.open(QIODevice::ReadOnly);
    emit message(“Analysing File!“);
    while(!inFile.atEnd())
    {
        inFile.getChar(&c);
        if(statisticMap.contains(c)) statisticMap[c]++;
        else statisticMap[c]=0;
    }
    emit message(“Generating Huffman Tree!“);
    tree.Generate(statisticMapcodeMap);
    inFile.close();
}
void Huffman::GenerateCompressFile()
{
    emit message(“Generating compressed file!“);
    outFile.open(QIODevice::WriteOnly);
    QByteArray headercontent;
    char c;
    int mapSize=codeMap.size();
    int zero=0;
    inFile.open(QIODevice::ReadOnly);
    while(!inFile.atEnd())
    {
        inFile.getChar(&c);
        content.append(codeMap[c]);
    }
    zero=8-content.size()%8;
    if(zero==8) zero=0;
    for(int i=0;i    header.append((char)zero);
    header.append((char)mapSize);
    QMap::iterator it;
    for(it=codeMap.begin();it!=codeMap.end();it++)
    {
        header.append(it.key());
        header.append(this->formatCodeMap(it.value()));
    }
    outFile.write(header);
    bool ok;
    QByteArray after;
    after.clear();
    for(int i=0;i    {
        after.append((char)content.mid(i8).toInt(&ok2));
        emit progress((i*100)/content.size());
    }
    emit progress(100);
    outFile.write

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        308  2014-11-25 02:46  HuffmanCompress\.git\config

     文件         73  2014-11-25 02:45  HuffmanCompress\.git\description

     文件        103  2014-11-25 15:03  HuffmanCompress\.git\FETCH_HEAD

     文件         23  2014-11-25 02:45  HuffmanCompress\.git\HEAD

     文件        177  2014-11-25 02:45  HuffmanCompress\.git\hooks\README.sample

     文件        992  2014-11-25 15:02  HuffmanCompress\.git\index

     文件        113  2014-11-25 02:45  HuffmanCompress\.git\info\exclude

     文件        511  2014-11-25 15:02  HuffmanCompress\.git\logs\HEAD

     文件        511  2014-11-25 15:02  HuffmanCompress\.git\logs\refs\heads\master

     文件        137  2014-11-25 02:46  HuffmanCompress\.git\logs\refs\remotes\origin\master

     文件        178  2014-11-25 02:45  HuffmanCompress\.git\objects\09\3262960ad888792d5b9647a9d06dc6bc74d2bd

     文件        385  2014-11-25 02:46  HuffmanCompress\.git\objects\1c\a30ab5323b43c106cdffb4c193df6858fe82ad

     文件        389  2014-11-25 14:59  HuffmanCompress\.git\objects\20\1c6c1bfa980afcf0ad1cf11fa0fad1ff7f6fe8

     文件        231  2014-11-25 15:02  HuffmanCompress\.git\objects\24\ebd5a4e7ca13a66b9e843be99db3a54a799f66

     文件        217  2014-11-25 02:45  HuffmanCompress\.git\objects\41\2eeda78dc9de1186c2e0e1526764af82ab3431

     文件        256  2014-11-25 02:46  HuffmanCompress\.git\objects\45\b8f27fea588a2ba31327fc293586a2f8576749

     文件        626  2014-11-25 14:59  HuffmanCompress\.git\objects\55\1343d0342357cc339dd75c6d31d6a93e5cfbf4

     文件        367  2014-11-25 02:45  HuffmanCompress\.git\objects\58\bcbf82a078b82bee68534fe414dde7a232c0af

     文件       1697  2014-11-25 14:59  HuffmanCompress\.git\objects\59\b65163a5b58932b80adf92ab9f45ea073641ad

     文件       2616  2014-11-25 02:46  HuffmanCompress\.git\objects\77\715acd3ac9fd97aafbd2ddd65bec4195afdeec

     文件        325  2014-11-25 02:46  HuffmanCompress\.git\objects\81\d219f77891bc5093f82d811bbb8b51cf0b4006

     文件        151  2014-11-25 02:45  HuffmanCompress\.git\objects\81\f745c71892b00fd155d217344d6d4173b53329

     文件        707  2014-11-25 14:59  HuffmanCompress\.git\objects\84\d96e7695a4d9edaa3a1600262b1dfa2f43874f

     文件         89  2014-11-25 02:45  HuffmanCompress\.git\objects\8f\eef8a2ed121c69dfcd7ac978d0385d8c13de59

     文件        442  2014-11-25 14:59  HuffmanCompress\.git\objects\a2\4569ab80afa81b8a4ad9a9e984c041ef442db1

     文件        389  2014-11-25 02:46  HuffmanCompress\.git\objects\a8\a485037c1284acaed82c0c6848dfd90d1ea546

     文件        141  2014-11-25 02:46  HuffmanCompress\.git\objects\b4\8f94ec827033ef073fb3c7f060837e90b935ec

     文件        557  2014-11-25 02:46  HuffmanCompress\.git\objects\c5\52b764e605891a104d2377689f1852aa5171a8

     文件        589  2014-11-25 02:46  HuffmanCompress\.git\objects\cc\fe2b80c5e63d633a1c2332725c76a542a17b0e

     文件        171  2014-11-25 02:46  HuffmanCompress\.git\objects\d0\4cf081343ac8cf994f1e460c5278326cc941fb

............此处省略58个文件信息

评论

共有 条评论