Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0
    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? :confused:

    Thanks!
  2. #17
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    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
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Smile


    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!
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo