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.
Related
I'm programming a simple HTTP server in C. So far, my server only supports text/html. I'm trying to add more functionalities to it by supporting additional MIME types (more precisely: text /css, text/javascript, image/jpg, image/png, font/woff2). For html files, I simply used fseek() and ftell() to determine the size of the file and read(), write() to read the file into a buffer and send it to the client. Now, I have the following questions:
1.Can I treat js, css, and woff2 files the exact same as html files (figuring size, reading, and sending)?
2.For binary files (images), what differences am I expected to make? Can I still use fseek() and ftell() to determine the size? Let's say I used fread(), can I use the return value as the file length? Is fwrite() really better than write() for binary files? Do I have to encode the image file before sending (I checked the RFC but I can't find any definite answer)? Should I include the "Content-Transfer-Encoding", or is it optional?
Do I have to encode the image file before sending (I checked the RFC but I can't find any definite answer)? Should I include the "Content-Transfer-Encoding", or is it optional?
No you don't need any encoding, and, FWIW, there is no Content-Transfer-Encoding field in HTTP.
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.
Is there any function for retrieving the pcap file header or is it done manually(type casting)?
What information are you trying to retrieve from the file header?
You can get the major and minor version with pcap_major_version() and pcap_minor_version(), the snapshot length with pcap_snapshot(), and the link-layer type with pcap_datalink(). There is no guarantee that the time zone offset and time stamp accuracy are valid (libpcap sets both to 0). Note also that libpcap 1.1.0 and later can read pcap-ng files, which don't have a pcap file header.
Libpcap provides no routines to directly hand you the file header, and does not supply anything that you could typecast to be a file header, so you can't do this with libpcap. If you want to read the file header, you will have to write your own code to replace libpcap, and that code will not be able to handle, for example, pcap-ng files (which will be the default file type in the next release of Wireshark), unlike code that uses libpcap (which can read pcap-ng files, as long as all the network interfaces in the pcap-ng file have the same link-layer header type and snapshot length, when they're using libpcap 1.1.0 or later).
The pcap file header is handled by libpcap internally. You shouldn't have to manipulate it manually.
You can open a pcap file for writing using pcap_dump_open.
You can open a pcap file for reading using pcap_open_offline.
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
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?