Thread: zlib Problems

  1. #1
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72

    Exclamation zlib Problems

    Hello,
    I'm learning C++, and i'm developing a simple zipper application, as my own, here is my code:
    Code:
    #include <string>
    #include <stdexcept>
    #include <iostream>
    #include <iomanip>
    #include <sstream>
    #include "zlib.h"
    using namespace std;
    
    string compress_string(const string& str,
                                int compressionlevel = Z_BEST_COMPRESSION)
    {
        z_stream zs;                        // z_stream is zlib's control structure
        memset(&zs, 0, sizeof(zs));
    
        if (deflateInit(&zs, compressionlevel) != Z_OK)
            throw(runtime_error("deflateInit failed while compressing."));
    
        zs.next_in = (Bytef*)str.data();
        zs.avail_in = str.size();           // set the z_stream's input
    
        int ret;
        char outbuffer[32768];
        string outstring;
    
        // retrieve the compressed bytes blockwise
        do {
            zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
            zs.avail_out = sizeof(outbuffer);
    
            ret = deflate(&zs, Z_FINISH);
    
            if (outstring.size() < zs.total_out) {
                // append the block to the output string
                outstring.append(outbuffer,
                                 zs.total_out - outstring.size());
            }
        } while (ret == Z_OK);
    
        deflateEnd(&zs);
    
        if (ret != Z_STREAM_END) {          // an error occurred that was not EOF
            ostringstream oss;
            oss << "Exception during zlib compression: (" << ret << ") " << zs.msg;
            throw(runtime_error(oss.str()));
        }
    
        return outstring;
    }
    
    int main(int argc, char* argv[])
    {
        string allinput;
    
        while (cin.good())     // read all input from cin
        {
            char inbuffer[32768];
            cin.read(inbuffer, sizeof(inbuffer));
            allinput.append(inbuffer, cin.gcount());
        }
            string cstr = compress_string( allinput );
    
            cerr << "Deflated data: "
                      << allinput.size() << " -> " << cstr.size()
                      << " (" << setprecision(1) << fixed
                      << ( (1.0 - (float)cstr.size() / (float)allinput.size()) * 100.0)
                      << "% saved).\n";
    
            cout << cstr;
     return 0;
    }
    Then i executed it normally, without errors, but when i try to open the archive i have some errors:
    *.gz
    Code:
    gzip: /home/ubuntu/test.txt.gz: not in gzip format
    *.zip
    Code:
    [/home/ubuntu/test.zip]
      End-of-central-directory signature not found.  Either this file is not
      a zipfile, or it constitutes one disk of a multi-part archive.  In the
      latter case the central directory and zipfile comment will be found on
      the last disk(s) of this archive.
    zipinfo:  cannot find zipfile directory in one of /home/ubuntu/test.zip or
              /home/ubuntu/test.zip.zip, and cannot find /home/ubuntu/test.zip.ZIP, period.
    If you want to see the full discussion about this application, See Here

    Thanks,
    Nathan Paulino Campos
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: [email protected]
    ICQ: 424738586
    AIM: nathanjava

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Actually, that doesn't look like it's your code at all, it looks like it's this guy's code.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I also don't see where you write anything to a file. If you redirected output, you'll have to get rid of the informational stuff at the top.

  4. #4
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72
    The code is from that guy, but then how i can do this code function!

    Thanks!
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: [email protected]
    ICQ: 424738586
    AIM: nathanjava

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So did you look at the zlib manual? Even a little bit?

  6. #6
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72
    I took a look on the Usage Example, but that code is so much confusing, the thing that i want is the most simple code, only to compress a test file and study that code to start the improvement of it.
    If you can help me i'll be very happy

    Thanks!
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: [email protected]
    ICQ: 424738586
    AIM: nathanjava

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The sample code you linked to is 36 lines to compress a file, with pages of comments to keep it from being confusing. If you're that kind of person and take out all the error checking, you're left with deflateInit, deflate+fwrite, deflateEnd.

  8. #8
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72
    I didn't understand!
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: [email protected]
    ICQ: 424738586
    AIM: nathanjava

  9. #9
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72
    Please, someone can help me!
    The only thing that i want is to start, then i will continue by myself!

    Thanks!
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: [email protected]
    ICQ: 424738586
    AIM: nathanjava

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you think this code is confusing - write a code that just calls external pkzip.exe with correct parameters, it will be much simplier
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72
    Hello vart,
    First thing, i use Linux. Second thing, i want to build my own zipper, your suggestion was good, but i want to build my own project.

    Thanks,
    Nathan Paulino Campos
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: [email protected]
    ICQ: 424738586
    AIM: nathanjava

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Zlib is very simple to use and the docs are pretty clear about how to use it. Perhaps a bit more reading and less asking before reading is in order.

  13. #13
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    I think what you're missing is the point that if you can't understand basic code that is generally well documented than you need to work with C++ more. ZLib is a very simple library and has a well documented API not to mention dozens and dozens of examples. Clearly you're not understanding them which means that you're more likely having difficulty understand C++ itself, not ZLib.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Zlib dll for windows
    By vin_pll in forum C++ Programming
    Replies: 8
    Last Post: 02-02-2008, 04:33 AM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  4. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

Tags for this Thread