zlib库介绍三:gzip(.gz格式)文件读写

gzip格式文件是一个数据压缩文件,文件大体上分为头部、数据部和尾部。

头部和尾部主要是一些文档属性和校验信息(rfc1952),数据部主要是用deflate方法压缩得到的数据。

zlib开源库使用的是deflate算法,因此,可以运用zlib库对gzip文件进行解压。

为便于gzip文件的读写,在zlib库中提供一组用于gzip读写的函数:

文件打开:

gzopen (const char *path, const char *mode);  
path:文件路径
mode:可为"rb"或"wb"。  

数据读取:
 int gzread (gzFile file,voidp buf, unsigned len);  
 

写入数据:

int gzwrite (gzFile file,const voidp buf, unsigned len);  
 

 写入数据:
int gzprintf (gzFile file,const char *format, ...);  
与fprintf用法相同。 

 数据读取:
char * gzgets (gzFile file,char *buf, int len);  
与fgets用法相同。  


 写入数据:

int gzputc (gzFile file, intc);  
与fputc用法相同。  


 数据读取:

int gzgetc (gzFilefile);  
与fgetc用法相同。  


刷新缓冲区:

int gzflush (gzFile file, intflush);  
与fflush作用相同。  


数据寻址:

z_off_t gzseek (gzFile file,z_off_t offset, int whence);  
不支持SEEK_END  

如果文件是开启为"读取",则SEEK_SET及SEEK_CUR。  
如果文件是开启为"写入",仅支援向前SEEK。  


数据寻址:

int gzrewind (gzFilefile);  
与gzseek(file, 0L, SEEK_SET)相同作用,仅在读取时有效。  

返回当前位置:

z_off_t gztell (gzFilefile);  
返回值 : 目前指针位置(解压缩后的位置)  


是否在尾部:

int gzeof (gzFilefile);  
返回值 : 1 - EOF, 0 - not EOF  
 

关闭文件:

int gzclose (gzFilefile);  
返回值 : zlib error number  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用zlib仅能解压缩gzip格式文件,而tar.gz格式文件是先用tar工具打包,再用gzip进行压缩。因此,要解压缩tar.gz文件,需要先用tar工具解包,然后再用zlib解压缩。 以下是一个使用zlib解压缩tar.gz文件的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <zlib.h> #include <tar.h> #define CHUNK_SIZE 1024 int main(int argc, char* argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s <compressed file> <output dir>\n", argv[0]); return 1; } const char* compressed_file = argv[1]; const char* output_dir = argv[2]; // 打开压缩文件 FILE* fp = fopen(compressed_file, "rb"); if (!fp) { fprintf(stderr, "Failed to open file: %s\n", compressed_file); return 1; } // 获取压缩文件大小 fseek(fp, 0, SEEK_END); size_t compressed_size = ftell(fp); fseek(fp, 0, SEEK_SET); // 分配缓冲区 char* compressed_data = (char*)malloc(compressed_size); if (!compressed_data) { fprintf(stderr, "Failed to allocate memory\n"); fclose(fp); return 1; } // 取压缩文件数据 size_t read_size = fread(compressed_data, 1, compressed_size, fp); fclose(fp); if (read_size != compressed_size) { fprintf(stderr, "Failed to read file: %s\n", compressed_file); free(compressed_data); return 1; } // 解压缩数据 char out[CHUNK_SIZE]; z_stream strm = { 0 }; strm.avail_in = compressed_size; strm.next_in = (Bytef*)compressed_data; strm.avail_out = CHUNK_SIZE; strm.next_out = (Bytef*)out; inflateInit(&strm); int ret = Z_OK; while (ret == Z_OK) { ret = inflate(&strm, Z_NO_FLUSH); if (ret == Z_STREAM_ERROR) { fprintf(stderr, "Failed to decompress data\n"); inflateEnd(&strm); free(compressed_data); return 1; } if (strm.avail_out == 0) { // 保存解压缩后的数据到文件中 // 这里需要根据实际情况来决定如何保存解压缩后的数据 // 例如,可以使用 fwrite 函数将数据入到文件中 // 也可以使用系统调用的 write 函数将数据入到文件中 // 还可以将数据保存到内存中,以便后续处理 strm.avail_out = CHUNK_SIZE; strm.next_out = (Bytef*)out; } } inflateEnd(&strm); free(compressed_data); // 打开tar文件 FILE* tar_fp = fmemopen(out, strlen(out), "rb"); if (!tar_fp) { fprintf(stderr, "Failed to open tar file from memory\n"); return 1; } // 解包tar文件 struct tar_header hdr; while (fread(&hdr, sizeof(hdr), 1, tar_fp) == 1) { if (hdr.name[0] == '\0') { break; } // 解压缩文件 size_t file_size = strtol(hdr.size, NULL, 8); char* file_data = (char*)malloc(file_size); if (!file_data) { fprintf(stderr, "Failed to allocate memory\n"); fclose(tar_fp); return 1; } size_t read_size = fread(file_data, 1, file_size, tar_fp); if (read_size != file_size) { fprintf(stderr, "Failed to read file data\n"); fclose(tar_fp); free(file_data); return 1; } // 保存文件到输出目录 char file_path[1024]; snprintf(file_path, sizeof(file_path), "%s/%s", output_dir, hdr.name); FILE* out_fp = fopen(file_path, "wb"); if (!out_fp) { fprintf(stderr, "Failed to create file: %s\n", file_path); fclose(tar_fp); free(file_data); return 1; } fwrite(file_data, 1, file_size, out_fp); fclose(out_fp); free(file_data); } fclose(tar_fp); printf("Decompression finished: %s -> %s\n", compressed_file, output_dir); return 0; } ``` 这个示例程序会取一个tar.gz文件,将文件中的数据解压缩后保存到指定的输出目录中。您需要在命令行中指定要解压缩的文件和输出目录的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翰墨之道

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值