The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Page 2 -
Use zlib to compress and decompress
Page 2 - Discuss Use zlib to compress and decompress in the C Programming forum on Dev Shed. Use zlib to compress and decompress C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 27th, 2012, 11:15 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 17
Time spent in forums: 5 h 19 m 31 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem So what's wrong with just using strcat() into some large buffer to accumulate ALL your strings, then you use compress() to compress the whole lot in one hit.
You then use decompress() in one hit to recover a large buffer of all the strings.
Apparently (from reading the manual - you've read it of course, so you would know this), the low level inflate functions can be used to decompress the kind of files generated by your post #1.
Study how Z_STREAM_END is returned, and updates to the strm structure to tell you how much data was used from the buffer. |
I used the example program(inflate function) from zlib.net to decompress the compressed file generated by my post #1, but it still only decompress the first sentence. I have check that the compressed file are all put into the buffer, but only the first one is decompressed. Do I need to rewrite the compression program by using deflate function to replace compress function? How can I decompress such compressed file?
Thanks!
|

November 27th, 2012, 12:30 PM
|
 |
Contributed User
|
|
|
|
Well at vast expense (I don't normally do this when it seems this is your paid employment to solve these issues), I present:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include"zlib.h"
unsigned long count = 0;
unsigned long file_len = 0;
unsigned long cfile_len = 0;
unsigned char *compression(const char *s) {
unsigned char *buf;
unsigned long buflen;
unsigned long slen = strlen(s) + 1;
buflen = compressBound(slen);
buf = (unsigned char *) malloc(sizeof(unsigned char) * buflen);
compress(buf, &buflen, (const Bytef *) s, slen);
count = buflen;
file_len = file_len + slen;
cfile_len = cfile_len + count;
return buf;
}
void f1() {
FILE *pf;
char *p1 = "this document is distributed in the hope that it will be useful, but without any warranty;";
char *p2 = "without even the implied warranty of merchantability or fittness for a particular purpose;";
char *p3 = "You should have received a copy of the GNU General Public License along with this document;";
pf = fopen("filess.txt", "wb");
unsigned char *temp;
temp = compression(p1);
fwrite(temp, count, 1, pf);
free(temp);
printf("%lu\n", count);
temp = compression(p2);
fwrite(temp, count, 1, pf);
free(temp);
printf("%lu\n", count);
temp = compression(p3);
fwrite(temp, count, 1, pf);
free(temp);
printf("%lu\n", count);
printf("%lu\n", file_len); //274
printf("%lu\n", cfile_len); //251
fclose(pf);
}
#define LINE 1024
void f2() {
char *pf = "filess.txt";
int p = open(pf, O_RDONLY);
FILE *nf = fopen("dfile.txt", "w");
z_stream z = { 0 };
unsigned char buf[LINE];
unsigned char dec_buf[LINE * 10];
unsigned long buf_len;
unsigned long dec_len = LINE * 10;
buf_len = read(p, buf, LINE);
z.next_in = buf;
z.avail_in = buf_len;
for ( int i = 0 ; i < 3 ; i++ ) {
z.next_out = dec_buf;
z.avail_out = sizeof(dec_buf);
memset(dec_buf,0,sizeof(dec_buf));
int res = inflateInit(&z);
int mode = Z_FINISH;
res = inflate(&z,mode);
fprintf(nf,"%s\n",dec_buf);
}
close(p);
fclose(nf);
}
int main ( ) {
f1();
f2();
return 0;
}
$ gcc -std=c99 -g bar.c -lz
$ ./a.out
82
80
89
274
251
$ cat dfile.txt
this document is distributed in the hope that it will be useful, but without any warranty;
without even the implied warranty of merchantability or fittness for a particular purpose;
You should have received a copy of the GNU General Public License along with this document;
This is proof of concept.
General tidying up, error checking and so forth are up to you.
|

November 27th, 2012, 12:50 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 17
Time spent in forums: 5 h 19 m 31 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem Well at vast expense (I don't normally do this when it seems this is your paid employment to solve these issues), I present:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include"zlib.h"
unsigned long count = 0;
unsigned long file_len = 0;
unsigned long cfile_len = 0;
unsigned char *compression(const char *s) {
unsigned char *buf;
unsigned long buflen;
unsigned long slen = strlen(s) + 1;
buflen = compressBound(slen);
buf = (unsigned char *) malloc(sizeof(unsigned char) * buflen);
compress(buf, &buflen, (const Bytef *) s, slen);
count = buflen;
file_len = file_len + slen;
cfile_len = cfile_len + count;
return buf;
}
void f1() {
FILE *pf;
char *p1 = "this document is distributed in the hope that it will be useful, but without any warranty;";
char *p2 = "without even the implied warranty of merchantability or fittness for a particular purpose;";
char *p3 = "You should have received a copy of the GNU General Public License along with this document;";
pf = fopen("filess.txt", "wb");
unsigned char *temp;
temp = compression(p1);
fwrite(temp, count, 1, pf);
free(temp);
printf("%lu\n", count);
temp = compression(p2);
fwrite(temp, count, 1, pf);
free(temp);
printf("%lu\n", count);
temp = compression(p3);
fwrite(temp, count, 1, pf);
free(temp);
printf("%lu\n", count);
printf("%lu\n", file_len); //274
printf("%lu\n", cfile_len); //251
fclose(pf);
}
#define LINE 1024
void f2() {
char *pf = "filess.txt";
int p = open(pf, O_RDONLY);
FILE *nf = fopen("dfile.txt", "w");
z_stream z = { 0 };
unsigned char buf[LINE];
unsigned char dec_buf[LINE * 10];
unsigned long buf_len;
unsigned long dec_len = LINE * 10;
buf_len = read(p, buf, LINE);
z.next_in = buf;
z.avail_in = buf_len;
for ( int i = 0 ; i < 3 ; i++ ) {
z.next_out = dec_buf;
z.avail_out = sizeof(dec_buf);
memset(dec_buf,0,sizeof(dec_buf));
int res = inflateInit(&z);
int mode = Z_FINISH;
res = inflate(&z,mode);
fprintf(nf,"%s\n",dec_buf);
}
close(p);
fclose(nf);
}
int main ( ) {
f1();
f2();
return 0;
}
$ gcc -std=c99 -g bar.c -lz
$ ./a.out
82
80
89
274
251
$ cat dfile.txt
this document is distributed in the hope that it will be useful, but without any warranty;
without even the implied warranty of merchantability or fittness for a particular purpose;
You should have received a copy of the GNU General Public License along with this document;
This is proof of concept.
General tidying up, error checking and so forth are up to you. |
Thank you for your help!!! But I am a graduate student in university, and I don't get paid.
Thanks again!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|