HOME > ZIP 파일의 구조 – PKZIP, WinZip 등 > 예제 소스 보기
PKZIP 파일 복원 예제
void
zip_decompress(const char*
src_filename)
{
//파일 열기
FILE*
fin = fopen(src_filename, "rb");
if(fin == NULL){
printf("%s
open failed\n", src_filename);
exit(1);
}
else{
fprintf(stderr,"%s
open succeeded\n",src_filename);
}
fseek(fin,0,SEEK_SET);
//헤더 내용 확인
long local_file_header_signature;
fread(&local_file_header_signature,sizeof(long),1,fin);
if( local_file_header_signature!=0x04034b50 ){
fprintf(stderr,"local_file_header_signature
wrong!!\n");
exit(1);
}
else{
fprintf(stderr,"local_file_header_signature
right!!\n");
}
short version_needed;
fread(&version_needed,sizeof(short),1,fin);
fprintf(stderr,"version
needed to extract = %d\n",version_needed);
short general_purpose_bit_flag;
fread(&general_purpose_bit_flag,sizeof(short),1,fin);
fprintf(stderr,"general_purpose_bit_flag
= %d\n",general_purpose_bit_flag);
short compression_method;
fread(&compression_method,sizeof(short),1,fin);
fprintf(stderr,"compression
method = %d\n",compression_method);
short last_mod_file_time;
fread(&last_mod_file_time,sizeof(short),1,fin);
fprintf(stderr,"last
mod file time = %d\n",last_mod_file_time);
short last_mod_file_date;
fread(&last_mod_file_date,sizeof(short),1,fin);
fprintf(stderr,"last
mod file date = %d\n",last_mod_file_date);
long crc32;
fread(&crc32,sizeof(long),1,fin);
fprintf(stderr,"crc32
= %X\n",crc32);
long compressed_size;
fread(&compressed_size,sizeof(long),1,fin);
fprintf(stderr,"compressed_size
= %d\n",compressed_size);
long uncompressed_size;
fread(&uncompressed_size,sizeof(long),1,fin);
fprintf(stderr,"uncompressed_size
= %d\n",uncompressed_size);
short file_name_length;
fread(&file_name_length,sizeof(short),1,fin);
fprintf(stderr,"file
name length= %d\n",file_name_length);
short extra_field_length;
fread(&extra_field_length,sizeof(short),1,fin);
fprintf(stderr,"extra
field length = %d\n",extra_field_length);
char *filename=new char[file_name_length+1];
fread(filename,file_name_length,1,fin);
filename[file_name_length]='\0';
fprintf(stderr,"file
name = %s\n",filename);
//복원하기
z_stream
z;
unsigned char *inbuf;
unsigned char
*outbuf;
int INBUFSIZE;
int OUTBUFSIZE;
int compressedSize=compressed_size;
int orgSize=uncompressed_size;
char *compressedData=new
char[compressedSize];
fread(compressedData,compressedSize,1,fin);
char *orgData=new char[orgSize];
orgData[0]='\0';
INBUFSIZE=compressedSize;
OUTBUFSIZE=orgSize;
inbuf
= (unsigned char
*)malloc(INBUFSIZE);
outbuf
= (unsigned char
*)malloc(OUTBUFSIZE);
inbuf=(unsigned char*)compressedData;
strcpy((char*)outbuf, "garbage");
z.zalloc = (alloc_func)0;
z.zfree = (free_func)0;
z.opaque = (voidpf)0;
z.next_in
= inbuf;
z.avail_in = 0;
z.next_out = outbuf;
int err = inflateInit2(&z,-(MAX_WBITS));
while (z.total_out
< orgSize && z.total_in < compressedSize) {
z.avail_in
= z.avail_out = 4096;
err
= inflate(&z, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
if (err != Z_OK) {
fprintf(stderr,
"inflate error: %d\n", err);
exit(1);
}
}
err
= inflateEnd(&z);
if
(err != Z_OK) {
fprintf(stderr, "inflateEnd error: %d\n", err);
exit(1);
}
fprintf(stderr,"%s",outbuf);
}