C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #16  
Old November 27th, 2012, 11:15 AM
asdk77 asdk77 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 17 asdk77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #17  
Old November 27th, 2012, 12:30 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 1 m 4 sec
Reputation Power: 1774
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #18  
Old November 27th, 2012, 12:50 PM
asdk77 asdk77 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 17 asdk77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 19 m 31 sec
Reputation Power: 0
Smile

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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Use zlib to compress and decompress

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap