Skip to content

Instantly share code, notes, and snippets.

@r1walz
Last active February 14, 2019 21:29
Show Gist options
  • Save r1walz/6980b6ef11b8705e89c1df091fd1e0f1 to your computer and use it in GitHub Desktop.
Save r1walz/6980b6ef11b8705e89c1df091fd1e0f1 to your computer and use it in GitHub Desktop.
file that can compress any string using zlib compression library
#include "zlib.h"
#include <stdio.h>
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# ifdef UNDER_CE
# include <stdlib.h>
# endif
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
# define SET_BINARY_MODE(file)
#endif
static char *prog;
static void error(const char *msg);
static void gz_compress(const char *in, gzFile out);
int main(int argc, char *argv[]);
static void error(msg)
const char *msg;
{
fprintf(stderr, "%s: %s\n", prog, msg);
exit(1);
}
static void gz_compress(in, out)
const char *in;
gzFile out;
{
int len = strlen(in);
int err;
if(gzwrite(out, in, (unsigned) len) != len) error(gzerror(out, &err));
}
int main(argc, argv)
int argc;
char *argv[];
{
gzFile outfile;
char outmode[] = "wb1";
prog = argv[0];
outfile = gzdopen(fileno(stdout), outmode);
SET_BINARY_MODE(stdout);
gz_compress("Hello ", outfile);
gz_compress("World!", outfile);
if(gzclose(outfile) != Z_OK) error("failed gzclose");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment