Pipe Zlib example

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

int main () {
	setmode(0, _O_BINARY);
	setmode(1, _O_BINARY);

	uLongf compressedBytes   = 0;
	uLongf unCompressedBytes = 0;

	// read data size
	fread(&compressedBytes  , sizeof(uLongf), 1, stdin);
	fread(&unCompressedBytes, sizeof(uLongf), 1, stdin);

	// allocate data container
	Bytef *compressedData   = (Bytef*) malloc(compressedBytes);
	Bytef *unCompressedData = (Bytef*) malloc(unCompressedBytes);

	// read compressed data
	fread(compressedData, compressedBytes, 1, stdin);

	// decompress
	uncompress(unCompressedData, &unCompressedBytes, compressedData, compressedBytes);

	// write un-compressed data
	fwrite(unCompressedData, unCompressedBytes, 1, stdout);
	fflush(stdout);

	free(compressedData);
	free(unCompressedData);
	return 0;
}

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *