I am currently learning C, and am having some issues with trying to make a small program that utilizes zlib.
I have managed to compile my application (using Codeblocks/MinGW) with the zlib libraries, and compilation works fine. I have used an example based upon the zpipe.c example found over at the official zlib site (zlib.net).
On execution, the output zip file is created, but it seems malformed and/or empty. I am unable to open it using 7zip.
Here is the code that I have modified. I have simply replaced the main() function within zpipe.c.
int main() {
printf("Compression test...");
int ret;
FILE *fpsource;
FILE *fpdest;
fpsource = fopen("test.txt", "rb");
fpdest = fopen("output.zip", "wb");
ret = def(fpsource, fpdest, Z_DEFAULT_COMPRESSION);
if (ret != Z_OK) {
printf("failure\n");
zerr(ret);
}
else {
printf("success..\n");
}
fclose(fpsource);
fclose(fpdest);
return EXIT_SUCCESS;
}
I receive no errors, and my 'success' message is printed. It's just the output file is corrupt.
zpipe.c as-is will generate the zlib format, which is raw deflate data wrapped in a zlib header and trailer. 7zip won't recognize that. It will recognize the gzip or zip format, which are entirely different wrappers on the same raw deflate data.
You can modify zpipe.c to use deflateInit2 (and inflateInit2) instead of the versions without the "2" to select the gzip format instead of the zlib format. You can read zlib.h for how to do this.
The code discussed simply compresses the file using the DEFLATE algorithm. The appropriate structures that make it a zip or gzip file are missing.
Related
I'm trying to write a generic function that will write both an uncompressed and compressed file (depending on user input). According to zlib, you just have to set the gzopen mode to "w0" (no compression), but I still get the ZLIB header!
In the ZLIB manual it mentions it is possible to write raw data (no header/trailer) but it doesn't say how. How can I write a plain (raw encoded) file with zlib?
thanks,
Open the file using the transparent mode "T":
#include <zlib.h>
int main()
{
gzFile file = gzopen("/tmp/a.dat", "wT");
(void) gzwrite(file, "test", 4);
(void) gzclose(file);
}
I'm using zlib to write a program that compress data in several threads. so I can't use gzwrite. I'm using compress2().
*dest_len = compressBound(LOG_BUFF_SZ);
err = compress2((Bytef*)compressed_buff->buff, dest_len, (Bytef*)b->buff, size, GZ_INT_COMPRESSION_LEVEL);
write(fd, compressed_buff->buff, compressed_buff->full);
But when I try to decompress file via gzip -d I see the next output: "not in gzip format". what am I doing wrong? Thank you for your answers
compress() and compress2() compress to the zlib format, not the gzip format. You need to use the lower-level functions to be able to select the gzip format. Those are deflateInit2(), deflate() and deflateEnd(). Read the documentation in zlib.h for those functions. After that, you should also look at the heavily documented example of their use.
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.
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.
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
It looks like this question is pretty simple but I can't find the clear solution for copying files in C without platform dependency.
I used a system() call in my open source project for creating a directory, copying files and run external programs. It works very well in Mac OS X and other Unix-ish systems, but it fails on Windows. The problem was:
system( "cp a.txt destination/b.txt" );
Windows uses backslashes for path separator. (vs slashes in Unix-ish)
Windows uses 'copy' for the internal copy command. (vs cp in Unix-ish)
How can I write a copying code without dependency?
( Actually, I wrote macros to solve this problems, but it's not cool. http://code.google.com/p/npk/source/browse/trunk/npk/cli/tests/testutil.h, L22-56 )
The system() function is a lot more trouble than it's worth; it invokes the shell in a seperate proccess, and should usually be avoided.
Instead fopen() a.txt and dest/b.text, and use getc()/putc() to do the copying (because the standard library is more likely to do page-aligned buffering than you)
FILE *src = fopen("a.txt", "rb");
FILE *dst = fopen("dest/b.txt", "wb");
int i;
for (i = getc(src); i != EOF; i = getc(src))
{
putc(i, dst);
}
fclose(dst);
fclose(src);
You need to use the C standard library functions in stdio.h.
In particular, fopen, fread, fwrite, and fclose will be sufficient.
Be sure to include the b ("binary") option in the flags to fopen.
[edit]
Unfortunately, the file names themselves (forward-slashes vs. back-slashes) are still platform dependent. So you will need some sort of #ifdef or similar to deal with that.
Or you can use a cross-platform toolkit.
Use the standard C library stdio.h. First open input file for reading using fopen(inputFilename, "rb") and open output file for writing using fopen(outputFilename, "wb"), copy the content using fread and fwrire. Then close both files using fclose.
There are some functions to decompress in zlib library (zlib version 1.2.3)
I want to decompress my source zip (.gz) file using uncompress function.
It is not working (error code -3) but gzopen is. It is still not working when I input payload pointer (passing gzip header) to uncompress.
So the question is:
What's the valid arguments for uncompress function?
If it needs different format, how can I make it?
You have to use some poorly documented features of the zlib library. See my answer to this question for more information: How can I decompress a gzip stream with zlib?