C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-04-2009, 04:53 PM   #1
Trying to Learn C
 
nathanpc's Avatar
 
Join Date: Jul 2009
Location: Brazil
Posts: 32
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
__________________
Eee PC 904HD White | Windows XP Home Edition | My Site

Google Talk: eeepc904@gmail.com
ICQ: 424738586

Add me and let's talk about C and C++!
nathanpc is offline   Reply With Quote
Old 09-04-2009, 06:36 PM   #2
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 730
Actually, that doesn't look like it's your code at all, it looks like it's this guy's code.
rags_to_riches is offline   Reply With Quote
Old 09-04-2009, 07:04 PM   #3
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 09-04-2009, 08:16 PM   #4
Trying to Learn C
 
nathanpc's Avatar
 
Join Date: Jul 2009
Location: Brazil
Posts: 32
The code is from that guy, but then how i can do this code function!

Thanks!
__________________
Eee PC 904HD White | Windows XP Home Edition | My Site

Google Talk: eeepc904@gmail.com
ICQ: 424738586

Add me and let's talk about C and C++!
nathanpc is offline   Reply With Quote
Old 09-04-2009, 08:25 PM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
So did you look at the zlib manual? Even a little bit?
tabstop is offline   Reply With Quote
Old 09-04-2009, 08:33 PM   #6
Trying to Learn C
 
nathanpc's Avatar
 
Join Date: Jul 2009
Location: Brazil
Posts: 32
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!
__________________
Eee PC 904HD White | Windows XP Home Edition | My Site

Google Talk: eeepc904@gmail.com
ICQ: 424738586

Add me and let's talk about C and C++!
nathanpc is offline   Reply With Quote
Old 09-04-2009, 08:51 PM   #7
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 09-04-2009, 08:59 PM   #8
Trying to Learn C
 
nathanpc's Avatar
 
Join Date: Jul 2009
Location: Brazil
Posts: 32
I didn't understand!
__________________
Eee PC 904HD White | Windows XP Home Edition | My Site

Google Talk: eeepc904@gmail.com
ICQ: 424738586

Add me and let's talk about C and C++!
nathanpc is offline   Reply With Quote
Old 09-05-2009, 05:52 AM   #9
Trying to Learn C
 
nathanpc's Avatar
 
Join Date: Jul 2009
Location: Brazil
Posts: 32
Please, someone can help me!
The only thing that i want is to start, then i will continue by myself!

Thanks!
__________________
Eee PC 904HD White | Windows XP Home Edition | My Site

Google Talk: eeepc904@gmail.com
ICQ: 424738586

Add me and let's talk about C and C++!
nathanpc is offline   Reply With Quote
Old 09-05-2009, 06:27 AM   #10
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
if you think this code is confusing - write a code that just calls external pkzip.exe with correct parameters, it will be much simplier
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 09-05-2009, 06:32 AM   #11
Trying to Learn C
 
nathanpc's Avatar
 
Join Date: Jul 2009
Location: Brazil
Posts: 32
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
__________________
Eee PC 904HD White | Windows XP Home Edition | My Site

Google Talk: eeepc904@gmail.com
ICQ: 424738586

Add me and let's talk about C and C++!
nathanpc is offline   Reply With Quote
Old 09-05-2009, 09:31 AM   #12
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,470
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.
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 09-06-2009, 11:09 PM   #13
Professional Chef
 
leeor_net's Avatar
 
Join Date: Apr 2004
Location: Scotch Plains, NJ
Posts: 129
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.
__________________
- Leeor
leeor_net is offline   Reply With Quote
Reply

Tags
c++, error, zlib

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:27 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22