專案 檔案壓縮與解壓縮

2021-07-29 19:38:56 字數 3330 閱讀 8604

專案簡介:

專案實現:

#pragma once

#include #include using namespace std;

template struct less

};template struct greater

};templatestruct less

};template >

class _heap

_heap(t* a,size_t sz) }

void _min_heap() }

void push(const t& x)

void pop() }

bool empty()

size_t size()

t& top()

protected:

void _adjustdown(size_t parent)

else if(_con(_a[child],_a[parent]))

else

}} void _adjustup(size_t child)

else

}}private:

vector_a;

};#pragma once

#include #include "heap.h"

using namespace std;

template struct huffmannode

};template class huffmantree

~huffmantree()

huffmantree(t* a ,size_t size,const t& invalid)

node* getroot()

protected:

node* _createhuffmantree(t* a ,size_t size,const t& invalid)

if(right)

parent=new node(left->_weight+right->_weight);

else

parent=new node(left->_weight);

parent->_left=left;

parent->_right=right;

//小頂堆為空哈夫曼樹建立完成

if(minheap.empty())

return parent;

//完成雙親節點的建立後並把雙親節點壓入堆底

minheap.push(parent);

} return null;

}private:

node* _root;

};#pragma once

#include #include "huffman.h"

#include #include using namespace std;

typedef long long longtype;

struct charinfo

charinfo(const char ch)

:_ch(ch)

bool operator!=(const charinfo& c)

charinfo operator+(const charinfo& c)

bool operator<(const charinfo& c) };

class filecompression

} void compress(const char* filename)

//1.統計各字元出現的次數

unsigned char ch = fgetc(fread);

while (!feof(fread))

//2.生成哈夫曼樹

huffmantreeh(_info, 256, charinfo());

huffmannode* root = h.getroot();

//3.生成哈夫曼樹編碼

string str;

huffmancode(root, str);

//4.壓縮檔案

fseek(fread, 0, seek_set);

ch = fgetc(fread);

unsigned char value = 0;

int _bit = 7;

開啟檔案寫壓縮後的編碼

string write=filename;

write+=".compress";

file* fwrite = fopen(write.c_str(), "wb");

while (!feof(fread))

if (_bit< 0)

cur++;

} ch = fgetc(fread);

} fputc(value, fwrite);//最後乙個位元組沒寫滿8位也要把data寫入檔案

寫配置檔案

writeconfig(filename);

fclose(fread);

fclose(fwrite);

cout<<"壓縮成功"<_info[ch]._count=(longtype)atoi(buff);

ch=fgetc(fconfig);

} //2.構建哈夫曼樹

huffmantreehf(_info,256,charinfo());

huffmannode*root=hf.getroot();

huffmannode*cur=root;

//3.解壓縮

ch=fgetc(pf);

int count=root->_weight._count;

int pos=7;

while(count)

else

if(cur->_left == null && cur->_right == null)

}pos=7;

ch=fgetc(pf);

} fclose(pf);

fclose(fconfig);

fclose(fin);

cout<<"解壓縮成功"<* root,string& code)

huffmancode(root->_left,code+'0');

huffmancode(root->_right,code+'1'); }

void writeconfig(const char* filename)

} fclose(fcon);

} private:

charinfo _info[256];

};

檔案壓縮與解壓縮

類 zipfile 構造方法 zipfile file file 開啟供閱讀的zip檔案,由指定的file物件給出。zipfile file file,int mode 開啟新的zipfile以使用指定模式從指定file物件讀取 zipfile string name 開啟zip檔案進行閱讀 方法摘...

檔案壓縮與解壓縮

public class zipfileutil zipoutputstream zostream null fileinputstream fistream null fileoutputstream fostream null try zostream.closeentry issuccess ...

壓縮與解壓縮

1 compress和uncompress 壓縮或者解壓縮資料,壓縮後檔案自動加上副檔名.z 2 gzip gunzip 壓縮解壓縮檔案,gz,比compress有效 例如 gzip ye.txt 壓縮ye.tar檔案,並且壓縮後副檔名加長ye.txt.gz gzip d ye.txt.gz 解壓縮...