Skip to content

Instantly share code, notes, and snippets.

@koooge
Created June 21, 2015 16:03
Show Gist options
  • Save koooge/3bf42b464d928db781f4 to your computer and use it in GitHub Desktop.
Save koooge/3bf42b464d928db781f4 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>
#define BUF_SIZE 1024
int main(int argc, char **argv)
{
int ret = Z_OK;
z_stream strm;
Byte *ibuf, *obuf;
FILE *ifp = fopen("input.txt", "r");
FILE *ofp = fopen("output.gz", "w");
ibuf = (Byte *)calloc(BUF_SIZE, sizeof(Byte));
obuf = (Byte *)calloc(BUF_SIZE, sizeof(Byte));
fread(ibuf, BUF_SIZE, 1, ifp);
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit2(&strm, 6, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
strm.next_in = ibuf;
strm.avail_in = BUF_SIZE;
strm.next_out = obuf;
strm.avail_out = BUF_SIZE;
ret = deflate(&strm, Z_FINISH);
fwrite(obuf, strm.total_out, 1, ofp);
END:
deflateEnd(&strm);
fclose(ofp);
fclose(ifp);
return 0;
}
~
@koooge
Copy link
Author

koooge commented Jun 21, 2015

gcc gzip.c -lz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment