📄 compress.h
字号:
#include "iostream.h"
bool compress(char *);
bool extract(char *);
struct compress
{
unsigned a:1;
unsigned b:1;
unsigned c:1;
unsigned d:1;
unsigned e:1;
unsigned f:1;
unsigned g:1;
unsigned h:1;
};
typedef struct compress COMPRESS;
bool compress(char *_file_to_compress)
{
FILE *file_to_compress,*file_compressed;
char c; //存放从待压缩的文件中读取字符
COMPRESS compressed_array[ARRAY_LENGTH];
int line_of_compressed_array=0;
int effective_number_of_last_byte=0;
file_to_compress=fopen(_file_to_compress,"rt");
if (file_to_compress==NULL)
return false;
file_compressed=fopen("zip.txt","wb");
while(!feof(file_to_compress))
{
c=fgetc(file_to_compress);
if (c!='1' && c!='0' && c!=-1)
{
cout<<"The file contains illegal character (not 0 or 1) !"<<endl;
return false;
}
else
{
if (c==-1)
break;
switch(effective_number_of_last_byte)
{
case 0:
if (c=='1')
compressed_array[line_of_compressed_array].a=1;
else
compressed_array[line_of_compressed_array].a=0;
effective_number_of_last_byte++;
break;
case 1:
if (c=='1')
compressed_array[line_of_compressed_array].b=1;
else
compressed_array[line_of_compressed_array].b=0;
effective_number_of_last_byte++;
break;
case 2:
if (c=='1')
compressed_array[line_of_compressed_array].c=1;
else
compressed_array[line_of_compressed_array].c=0;
effective_number_of_last_byte++;
break;
case 3:
if (c=='1')
compressed_array[line_of_compressed_array].d=1;
else
compressed_array[line_of_compressed_array].d=0;
effective_number_of_last_byte++;
break;
case 4:
if (c=='1')
compressed_array[line_of_compressed_array].e=1;
else
compressed_array[line_of_compressed_array].e=0;
effective_number_of_last_byte++;
break;
case 5:
if (c=='1')
compressed_array[line_of_compressed_array].f=1;
else
compressed_array[line_of_compressed_array].f=0;
effective_number_of_last_byte++;
break;
case 6:
if (c=='1')
compressed_array[line_of_compressed_array].g=1;
else
compressed_array[line_of_compressed_array].g=0;
effective_number_of_last_byte++;
break;
case 7:
if (c=='1')
compressed_array[line_of_compressed_array].h=1;
else
compressed_array[line_of_compressed_array].h=0;
line_of_compressed_array++;
effective_number_of_last_byte=0;
break;
default:
break;
}
}
}
effective_number_of_last_byte--;
fprintf(file_compressed,"%d,%d",line_of_compressed_array,effective_number_of_last_byte);
int j=0;
COMPRESS *q=compressed_array;
while(j<=line_of_compressed_array)
{
fwrite(q,1,1,file_compressed);
q=q+1;
j++;
}
fclose(file_compressed);
fclose(file_to_compress);
char anwser;
cout<<"Have a look at the file?"<<endl;
cin>>anwser;
if (anwser=='y')
system("notepad.exe file_compressed.txt");
return true;
}
bool extract(char *_file_compressed)
{
FILE *file_compressed,*extract_file;
int line_of_compressed_array=0,i=0;
int effective_number_of_last_byte=0;
COMPRESS compressed_array[ARRAY_LENGTH],p,*q;
q=compressed_array;
file_compressed=fopen(_file_compressed,"rb");
if (file_compressed==NULL)
{
cout<<"The compressed file doesn't exist !"<<endl;
return false;
}
fscanf(file_compressed,"%d,%d",&line_of_compressed_array,&effective_number_of_last_byte);
extract_file=fopen("extract.txt","wt");
while(!feof(file_compressed))
{
fread(&p,1,1,file_compressed);
q->a=p.a;
q->b=p.b;
q->c=p.c;
q->d=p.d;
q->e=p.e;
q->f=p.f;
q->g=p.g;
q->h=p.h;
q=q+1;
}
while(i<line_of_compressed_array)
{
if (compressed_array[i].a==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
if (compressed_array[i].b==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
if (compressed_array[i].c==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
if (compressed_array[i].d==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
if (compressed_array[i].e==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
if (compressed_array[i].f==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
if (compressed_array[i].g==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
if (compressed_array[i].h==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
i++;
}
if (effective_number_of_last_byte>=0) //大于等于零说明还有不满一字节的数据未写入
{
i=0;
switch(effective_number_of_last_byte)
{
default:
if (compressed_array[line_of_compressed_array].a==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
i++;
if (i>effective_number_of_last_byte)
break;
if (compressed_array[line_of_compressed_array].b==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
i++;
if (i>effective_number_of_last_byte)
break;
if (compressed_array[line_of_compressed_array].c==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
i++;
if (i>effective_number_of_last_byte)
break;
if (compressed_array[line_of_compressed_array].d==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
i++;
if (i>effective_number_of_last_byte)
break;
if (compressed_array[line_of_compressed_array].e==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
i++;
if (i>effective_number_of_last_byte)
break;
if (compressed_array[line_of_compressed_array].f==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
i++;
if (i>effective_number_of_last_byte)
break;
if (compressed_array[line_of_compressed_array].g==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
i++;
if (i>effective_number_of_last_byte)
break;
if (compressed_array[line_of_compressed_array].h==1)
fputc('1',extract_file);
else
fputc('0',extract_file);
break;
}
}
fputc(-1,extract_file);
fclose(file_compressed);
fclose(extract_file);
char anwser;
cout<<"Have a look at the file (y or n) ?"<<endl;
cin>>anwser;
if (anwser=='y')
system("notepad.exe extract.txt");
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -