当前位置:首页 » 编程语言 » 无损压缩开源C语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

无损压缩开源C语言

发布时间: 2022-05-18 21:51:02

‘壹’ 谁有无损音频压缩编码的c语言的源代码

到底是要哪种格式的呢,开源应该也有

~~~~~~

‘贰’ (20分)用C语言编译的文件压缩解压缩程序

是用霍夫曼树做的
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct head
{
unsigned char b; /*the charactor*/
long count; /*the frequency*/
long parent,lch,rch; /*make a tree*/
char bits[256]; /*the haffuman code*/
}
header[512],tmp;

void compress()
{
char filename[255],outputfile[255],buf[512];
unsigned char c;
long i,j,m,n,f;
long min1,pt1,flength;
FILE *ifp,*ofp;
printf("source filename:");
gets(filename);
ifp=fopen(filename,"rb");
if(ifp==NULL)
{
printf("source file open error!\n");
return;
}
printf("destination filename:");
gets(outputfile);
ofp=fopen(outputfile,"wb");
if(ofp==NULL)
{
printf("destination file open error!\n");
return;
}
flength=0;
while(!feof(ifp))
{
fread(&c,1,1,ifp);
header[c].count++;
flength++;
}
flength--;
header[c].count--;
for(i=0;i<512;i++)
{
if(header[i].count!=0) header[i].b=(unsigned char)i;
else header[i].b=0;
header[i].parent=-1;
header[i].lch=header[i].rch=-1;
}
for(i=0;i<256;i++)
{
for(j=i+1;j<256;j++)
{
if(header[i].count<header[j].count)
{
tmp=header[i];
header[i]=header[j];
header[j]=tmp;
}
}
}
for(i=0;i<256;i++) if(header[i].count==0) break;
n=i;
m=2*n-1;
for(i=n;i<m;i++)
{
min1=999999999;
for(j=0;j<i;j++)
{
if(header[j].parent!=-1) continue;
if(min1>header[j].count)
{
pt1=j;
min1=header[j].count;
continue;
}
}
header[i].count=header[pt1].count;
header[pt1].parent=i;
header[i].lch=pt1;
min1=999999999;
for(j=0;j<i;j++)
{
if(header[j].parent!=-1) continue;
if(min1>header[j].count)
{
pt1=j;
min1=header[j].count;
continue;
}
}
header[i].count+=header[pt1].count;
header[i].rch=pt1;
header[pt1].parent=i;
}
for(i=0;i<n;i++)
{
f=i;
header[i].bits[0]=0;
while(header[f].parent!=-1)
{
j=f;
f=header[f].parent;
if(header[f].lch==j)
{
j=strlen(header[i].bits);
memmove(header[i].bits+1,header[i].bits,j+1);
header[i].bits[0]='0';
}
else
{
j=strlen(header[i].bits);
memmove(header[i].bits+1,header[i].bits,j+1);
header[i].bits[0]='1';
}
}
}
fseek(ifp,0,SEEK_SET);
fwrite(&flength,sizeof(int),1,ofp);
fseek(ofp,8,SEEK_SET);
buf[0]=0;
f=0;
pt1=8;
while(!feof(ifp))
{
c=fgetc(ifp);
f++;
for(i=0;i<n;i++)
{
if(c==header[i].b) break;
}
strcat(buf,header[i].bits);
j=strlen(buf);
c=0;
while(j>=8)
{
for(i=0;i<8;i++)
{
if(buf[i]=='1') c=(c<<1)|1;
else c=c<<1;
}
fwrite(&c,1,1,ofp);
pt1++;
strcpy(buf,buf+8);
j=strlen(buf);
}
if(f==flength) break;
}
if(j>0)
{
strcat(buf,"00000000");
for(i=0;i<8;i++)
{
if(buf[i]=='1') c=(c<<1)|1;
else c=c<<1;
}
fwrite(&c,1,1,ofp);
pt1++;
}
fseek(ofp,4,SEEK_SET);
fwrite(&pt1,sizeof(long),1,ofp);
fseek(ofp,pt1,SEEK_SET);
fwrite(&n,sizeof(long),1,ofp);
for(i=0;i<n;i++)
{
fwrite(&(header[i].b),1,1,ofp);
c=strlen(header[i].bits);
fwrite(&c,1,1,ofp);
j=strlen(header[i].bits);
if(j%8!=0)
{
for(f=j%8;f<8;f++)
strcat(header[i].bits,"0");
}
while(header[i].bits[0]!=0)
{
c=0;
for(j=0;j<8;j++)
{
if(header[i].bits[j]=='1') c=(c<<1)|1;
else c=c<<1;
}
strcpy(header[i].bits,header[i].bits+8);
fwrite(&c,1,1,ofp);
}
}
fclose(ifp);
fclose(ofp);
printf("compress successfully!\n");
return;
}
void uncompress()
{
char filename[255],outputfile[255],buf[255],bx[255];
unsigned char c;
long i,j,m,n,f,p,l;
long flength;
FILE *ifp,*ofp;
printf("source filename:");
gets(filename);
ifp=fopen(filename,"rb");
if(ifp==NULL)
{
printf("source file open error!\n");
return;
}
printf("destination filename:");
gets(outputfile);
ofp=fopen(outputfile,"wb");
if(ofp==NULL)
{
printf("destination file open error!\n");
return;
}
fread(&flength,sizeof(long),1,ifp);
fread(&f,sizeof(long),1,ifp);
fseek(ifp,f,SEEK_SET);
fread(&n,sizeof(long),1,ifp);
for(i=0;i<n;i++)
{
fread(&header[i].b,1,1,ifp);
fread(&c,1,1,ifp);
p=(long)c;
header[i].count=p;
header[i].bits[0]=0;
if(p%8>0) m=p/8+1;
else m=p/8;
for(j=0;j<m;j++)
{
fread(&c,1,1,ifp);
f=c;
itoa(f,buf,2);
f=strlen(buf);
for(l=8;l>f;l--)
{
strcat(header[i].bits,"0");
}
strcat(header[i].bits,buf);
}
header[i].bits[p]=0;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strlen(header[i].bits)>strlen(header[j].bits))
{
tmp=header[i];
header[i]=header[j];
header[j]=tmp;
}
}
}
p=strlen(header[n-1].bits);
fseek(ifp,8,SEEK_SET);
m=0;
bx[0]=0;
while(1)
{
while(strlen(bx)<(unsigned int)p)
{
fread(&c,1,1,ifp);
f=c;
itoa(f,buf,2);
f=strlen(buf);
for(l=8;l>f;l--)
{
strcat(bx,"0");
}
strcat(bx,buf);
}
for(i=0;i<n;i++)
{
if(memcmp(header[i].bits,bx,header[i].count)==0) break;
}
strcpy(bx,bx+header[i].count);
c=header[i].b;
fwrite(&c,1,1,ofp);
m++;
if(m==flength) break;
}
fclose(ifp);
fclose(ofp);
printf("Uncompress successfully!\n");
return;
}
int main()
{
int c;
printf("1--Compress file\n");
printf("2--Uncompress file\n");
printf("Select 1 or 2:");
c=getch();
printf("%c\n",c);
if(c=='1') compress();
else if(c=='2') uncompress();
return 0;
}

‘叁’ 如何用c语言压缩解压文件夹

压缩是一种有效的减小数据量的方法,目前已经被广泛应用于各种类型的信息系统之中。

一种压缩文本文件的方法如下:

1. 原始文本文件中的非字母的字符,直接拷贝到压缩文件中;

2.
原始文件中的词(全部由字母组成),如果是第一次出现,则将该词加入到一个词的列表中,并拷贝到压缩文件中;否则该词不拷贝到压缩文件中,而是将该词在词的列表中的位置拷贝到压缩文件中。

3. 词的列表的起始位置为 1 。 词的定义为文本中由大小写字母组成的最大序列。大写字母和小写字母认为是不同的字母,即 abc 和 Abc
是不同的词。词的例子如下: * x-ray 包括两个词 x 和 ray * mary's 包括两个词 mary 和 s * a c-Dec 包括三个词 a 和
c 和 Dec 编写一个程序,输入为一组字符串,输出为压缩后的文本。

输入:

输入为一段文本,你可以假设输入中不会出现数字、每行的长度不会超过 80 个字符,并且输入文本的大小不会超过 10M。

输出:

压缩后的文本。

输入:
Please, please do it--it would please Mary very,
very much.

Thanks

输出:
Please, please do it--4 would 2 Mary very,
7 much.

Thanks

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#defineLEN1<<20
intisArabic(charc){
return('a'<=c&&c<='z')||('A'<=c&&c<='Z');
}
intmain()
{
chardict[LEN];
char*index[100000];
charbuf[82];
intnWord=0;
inti,j;
charc;
char*inFile="G:\in.txt",*outFile="G:\out.txt";
FILE*inp,*outp;

if((inp=fopen(inFile,"r"))==NULL){
printf("cannotopen ");
exit(1);
}
if((outp=fopen(outFile,"w"))==NULL){
printf("outfail ");
}
index[0]=dict;
do{
/*getaword*/
i=0;
do{
c=fgetc(inp);
buf[i++]=c;
}while(isArabic(c));

buf[i-1]=0;
/*putittodict*/
if(i>1){
for(j=0;j<nWord;j++){
if(strcmp(index[j],buf)==0){
break;
}
}
if(j==nWord){
strcpy(index[nWord],buf);
index[nWord+1]=index[nWord]+strlen(buf)+1;
nWord++;
/*printf("new:%s ",buf);*/
}else{
sprintf(buf,"%d",j+1);
/*printf("found:%s ",buf);*/
}
}
/*putittooutputfile*/
if(c!=EOF)
fprintf(outp,"%s%c",buf,c);
else
fprintf(outp,"%s",buf);
}while(c!=EOF);

fclose(inp);
fclose(outp);
/*system("PAUSE");*/
returnEXIT_SUCCESS;
}

‘肆’ 如何用c语言实现压缩图片内存大小

是(row,col,value),这样把所有不为零的值组成一个向量。这种存储方式比二维数组节省了不少空间,当然还可以进一步节省,因为三元组里面row或者col重复存储了,一行或者一列存一次就行了,按这种思路走下去就是行压缩存储了。
那具体什么是行压缩存储呢?行压缩存储的思想就是,把所有不为零的值按行访问的顺序组成一个向量,然后再把每一行值不为0的列的下标存下来,这个两个向量的大小和稀疏矩阵中不为0的值得个数相同,当然要实现对行压缩矩阵的访问,还要把每一行的不为0的列的下标在第二个向量中开始的位置存下来,有人把这个叫做指针。有了这三个向量就可以实现对矩阵实现高效的按行访问了。行压缩存储比三元组优秀的不仅是空间的压缩,还有就是行访问时的高效。三元组如果是有序的,可以二分查找来访问一行,但是行压缩存储按行访问时的时间复杂度是常数级的。 大家可以参考下面这个行压缩矩阵示意图:

‘伍’ c语言编写文件压缩程序的设计思路 急啊

这个,我觉得应该找一个成熟的库,看需求了,比如Huffman算法的,或者直接开源的zlib之类的,然后你的c调用库的API接口,就可以了。其实你的c代码只是对库的功能的一个简单封装,最多提供一些带压缩的文件从哪里来,要放到哪里去这些。数据压缩是一个很大的课题,看你的需求了。

‘陆’ 用C语言简单演示如何借助zlib库实现文件的压缩和解压缩

问题的根源在于这些网友对于字符串和字节流的概念非常的模糊,对文本文件和二进制文件的区别常常模棱两可,其实字节流可以表示所有的数据,二进制文件才是任何文件的本质。字节流是一个字节接一个字节,并没有结束符号,所以需要给它一个长度信息。二进制文件是一个字节接一个字节,并没有换行符之类的。文件压缩的时候,可以通过源文件的长度自动计算缓冲区的长度,压缩后写入目标文件之前,需先保留源文件和目标数据的长度作为解压缩的依据,参考如下代码:#include #include #include int main(int argc, char* argv[]) { FILE* file; uLong flen; unsigned char* fbuf = NULL; uLong clen; unsigned char* cbuf = NULL; /* 通过命令行参数将srcfile文件的数据压缩后存放到dstfile文件中 */ if(argc < 3) { printf("Usage: zcdemo srcfile dstfile\n"); return -1; } if((file = fopen(argv[1], "rb")) == NULL) { printf("Can\'t open %s!\n", argv[1]); return -1; } /* 装载源文件数据到缓冲区 */ fseek(file, 0L, SEEK_END); /* 跳到文件末尾 */ flen = ftell(file); /* 获取文件长度 */ fseek(file, 0L, SEEK_SET); if((fbuf = (unsigned char*)malloc(sizeof(unsigned char) * flen)) == NULL) { printf("No enough memory!\n"); fclose(file); return -1; } fread(fbuf, sizeof(unsigned char), flen, file); /* 压缩数据 */ clen = compressBound(flen); if((cbuf = (unsigned char*)malloc(sizeof(unsigned char) * clen)) == NULL) { printf("No enough memory!\n"); fclose(file); return -1; } if(compress(cbuf, &clen, fbuf, flen) != Z_OK) { printf("Compress %s failed!\n", argv[1]); return -1; } fclose(file); if((file = fopen(argv[2], "wb")) == NULL) { printf("Can\'t create %s!\n", argv[2]); return -1; } /* 保存压缩后的数据到目标文件 */ fwrite(&flen, sizeof(uLong), 1, file); /* 写入源文件长度 */ fwrite(&clen, sizeof(uLong), 1, file); /* 写入目标数据长度 */ fwrite(cbuf, sizeof(unsigned char), clen, file); fclose(file); free(fbuf); free(cbuf); return 0; }文件解压缩的时候,可以通过保留信息得到缓冲区和数据流的大小,这样解压缩后直接保存即可,参考如下代码:#include #include #include int main(int argc, char* argv[]) { FILE* file; uLong flen; unsigned char* fbuf = NULL; uLong ulen; unsigned char* ubuf = NULL; /* 通过命令行参数将srcfile文件的数据解压缩后存放到dstfile文件中 */ if(argc < 3) { printf("Usage: zudemo srcfile dstfile\n"); return -1; } if((file = fopen(argv[1], "rb")) == NULL) { printf("Can\'t open %s!\n", argv[1]); return -1; } /* 装载源文件数据到缓冲区 */ fread(&ulen, sizeof(uLong), 1, file); /* 获取缓冲区大小 */ fread(&flen, sizeof(uLong), 1, file); /* 获取数据流大小 */ if((fbuf = (unsigned char*)malloc(sizeof(unsigned char) * flen)) == NULL) { printf("No enough memory!\n"); fclose(file); return -1; } fread(fbuf, sizeof(unsigned char), flen, file); /* 解压缩数据 */ if((ubuf = (unsigned char*)malloc(sizeof(unsigned char) * ulen)) == NULL) { printf("No enough memory!\n"); fclose(file); return -1; } if(uncompress(ubuf, &ulen, fbuf, flen) != Z_OK) { printf("Uncompress %s failed!\n", argv[1]); return -1; } fclose(file); if((file = fopen(argv[2], "wb")) == NULL) { printf("Can\'t create %s!\n", argv[2]); return -1; } /* 保存解压缩后的数据到目标文件 */ fwrite(ubuf, sizeof(unsigned char), ulen, file); fclose(file); free(fbuf); free(ubuf); return 0; }

‘柒’ 用C语言设计 简单的数据压缩与解压缩程序

把所有的数据先读出来,存到数组中,然后遍历:
假设已搜到前i个不同的,且第i+1个与第i个不同,已将前i个存入临时数组,i>=1。如果第i+1和第i+2个不同,则第i+1个一并按序存入临时数组。如果第i+1个和第i+2个相同,则前i个做一次压缩处理,清空临时数组。
找连续相同的有多少个容易得多,就不具体说明了
我可以帮助你,你先设置我最佳答案后,我网络Hii教你。

‘捌’ 如何用C语言实现数据压缩

首先选择一个压缩算法

然后按照算法实现压缩代码,调用接口就可以
常见的 可以使用哈夫曼编码压缩,或者使用开源的压缩代码,比如lzo, gzip, lzma等等。

‘玖’ 用C语言如何对文件进行压缩

winrar软件安装后,所在的安装目录下有个rar.exe,开一个命令窗口到该目录下运行rar
/?就能看到用法了,在你的程序里面调用它的相应命令行是最简单的方法,自己编写压缩算法的话,先不谈效率,起码算法就是相当复杂的了