zlib simple example (gzseek)

Very simple zlib example. The neat thing is that zlib is transparent to uncompressed files, so you can just replace all your file seeking with zlib code and it’ll work with compressed and uncompressed code. It’s worth noting that gzseek does not support SEEK_END, which can cause issues.


#include <zlib.h>
#include <stdio.h>

int main() {
  gzFile file = gzopen("test.gz", "rb");
  if(file == NULL) {
    fprintf(stderr, "gzopen error\n");
  }

  char buffer[1000];
  int uncomprLen = gzread(file, buffer, 1000);
  buffer[uncomprLen]=0;
  printf("data: %s\n",buffer);  

  int pos = gzseek(file, 5, SEEK_SET);
  
  uncomprLen = gzread(file, buffer, 5);
  buffer[6]=0;
  printf("data: %s\n",buffer);  

  gzclose(file);
}