C语言编码实现文件压缩,C语言文件压缩 代码,该如何处理

C语言文件压缩 代码

求压缩文件的C源代码,谢谢了

------解决方案--------------------

lzss的源代码:

#include

#include

#include

#include

#define N 4096 /* size of ring buffer */

#define F 18 /* upper limit for match_length */

#define THRESHOLD 2 /* encode string into position and length

if match_length is greate

r thhan this */

#define NIL N /* index for root of binary search t

rees

*/

unsigned long int

textsize = 0, /* text size counter */

codesize = 0, /* code size counter */

printcount = 0; /* counter for reporting progress every 1K b

ytes

*/

unsigned char

text_buf[N + F - 1]; /* ring buffer of size N,

with extra F-1 bytes to facilitate string comparison

*/

int match_position, match_length, /* of longest match. These a

re

set by the InsertNode() procedure. */

lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right chi

ldre

&

parents -- These constitute binary search trees. */

FILE *infile, *outfile; /* input & output files */

void InitTree(void) /* initialize trees */

{

int i;

/* For i = 0 to N - 1, rson[i] and lson[i] will be the right and

left children of node i. These nodes need not be initialized.

Also, dad[i] is the parent of node i. These are initialized to

NIL (= N), which stands for 'not used. '

For i = 0 to 255, rson[N + i + 1] is the root of the tree

for strings that begin with character i. These are initialized

to NIL. Note there are 256 trees. */

for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;

for (i = 0; i < N; i++) dad[i] = NIL;

}

void InsertNode(int r)

/* Inserts string of length F, text_buf[r..r+F-1], into one of the

trees (text_buf[r] 'th tree) and returns the longest-match positio

n

and length via the global variables match_position and

match_length.

If match_length = F, then removes the old node in favor of the ne

w

one, because the old one will be deleted sooner.

Note r plays double role, as tree node and position in buffer. */

{

int i, p, cmp;

unsigned char *key;

cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];

rson[r] = lson[r] = NIL; match_length = 0;

for ( ; ; ) {

if (cmp > = 0) {

if (rson[p] != NIL) p = rson[p];

else { rson[p] = r; dad[r] = p; return; }

} else {

if (lson[p] != NIL) p = lson[p];

else { lson[p] = r; dad[r] = p; return; }

}

for (i = 1; i < F; i++)

if ((cmp = key[i] - text_buf[p + i]) != 0) break;

if (i > match_length) {

match_position = p;

if ((match_length = i) > = F) break;

}

}

dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];

dad[lson[p]] = r; dad[rson[p]] = r;

if (rson[dad[p]] == p) rson[dad[p]] = r;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值