redirect output to compressed format (C)
-
23-04-2021 - |
Frage
Is it possible to use fprintf
in such a way that write data to a compressed file?
For example:
fopen ("myfile.txt","w");
will write to a plain text file. So the file size grows very large.
Lösung
You can use zlib
to write data to a compressed stream.
gzFile fp;
fp = gzopen(NAME, "wb");
gzprintf(fp, "Hello, %s!\n", "world");
gzclose(fp);
Compile it like this:
gcc -Wall -Wextra -o zprog zprog.c -lz
Use zcat
to print the contents of the file.
Andere Tipps
The minimally-invasive solution if you're on a system that has pipes would be to open a pipe to an external gzip
process. That way you can use all the normal stdio output functions without having to replace everything with zlib calls.
On Linux, you could use the zlib library (and link it as -lz
) and use its compressed streams
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow