![]() |
| | #1 |
| Trying to Learn C Join Date: Jul 2009 Location: Brazil
Posts: 32
| 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;
}
*.gz Code: gzip: /home/ubuntu/test.txt.gz: not in gzip format 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.
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 | |
| | #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 | |
| | #3 |
| and the Hat of Guessing 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 | |
| | #4 |
| Trying to Learn C 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 | |
| | #5 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| So did you look at the zlib manual? Even a little bit? |
| tabstop is offline | |
| | #6 |
| Trying to Learn C 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 | |
| | #7 |
| and the Hat of Guessing 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 | |
| | #8 |
| Trying to Learn C 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 | |
| | #9 |
| Trying to Learn C 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 | |
| | #10 |
| CSharpener 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 | |
| | #11 |
| Trying to Learn C 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 | |
| | #12 |
| Super Moderator 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 | |
| | #13 |
| Professional Chef 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 | |
![]() |
| Tags |
| c++, error, zlib |
| Thread Tools | |
| Display Modes | |
|
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 |