當前位置:首頁 » 編程語言 » 無損壓縮開源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
/?就能看到用法了,在你的程序裡面調用它的相應命令行是最簡單的方法,自己編寫壓縮演算法的話,先不談效率,起碼演算法就是相當復雜的了