Compress Files Example
Compress Files Example
#1
Posted 03 September 2009 - 09:57 AM
Hello,
I'm learning C++ and trying to build my own file zipper, because i like very much to work with zip files, but now i want to build my own zipper and it's so hard to find good resources to start. As you can see in my previous posts i was trying to correct the only code snippet that i've founded in Google, but it's getting so much errors.
Then the only thing that i want is a very good snippet to start my zipper, remember that i want to build a zipper, not an unzipper.
Thanks,
Nathan Paulino Campos
I'm learning C++ and trying to build my own file zipper, because i like very much to work with zip files, but now i want to build my own zipper and it's so hard to find good resources to start. As you can see in my previous posts i was trying to correct the only code snippet that i've founded in Google, but it's getting so much errors.
Then the only thing that i want is a very good snippet to start my zipper, remember that i want to build a zipper, not an unzipper.
Thanks,
Nathan Paulino Campos
#4
Posted 03 September 2009 - 11:29 AM
Is this basically a repeat of your zlib thread? You couldn't link to the library properly, etc..
There are plenty of code samples out there. The zlib library provides a couple of small samples with it too. No doubt you've seen them.
It's not really clear what you are asking us (unless you want us to Google for you...)
There are plenty of code samples out there. The zlib library provides a couple of small samples with it too. No doubt you've seen them.
It's not really clear what you are asking us (unless you want us to Google for you...)
#5
Posted 03 September 2009 - 01:29 PM
You can't get much simpler than it already is. I'll write something quick that compresses the source file.
Compiled on SuSE Linux with:
#include <stdio.h>
#include <zlib.h>
#include <limits.h> /* for PATH_MAX */
int compressFile(FILE *in, const char * const outFileName)
{
/* Buffer to hold data read */
char buf[BUFSIZ] = { 0 };
size_t bytes_read = 0;
gzFile *out = gzopen(outFileName, "wb");
if (!out)
{
/* Handle error */
fprintf(stderr, "Unable to open %s for writing\n", outFileName);
return -1;
}
bytes_read = fread(buf, 1, BUFSIZ, in);
while (bytes_read > 0)
{
int bytes_written = gzwrite(out, buf, bytes_read);
if (bytes_written == 0)
{
int err_no = 0;
fprintf(stderr, "Error during compression: %s", gzerror(out, &err_no))\
;
gzclose(out);
return -1;
}
bytes_read = fread(buf, 1, BUFSIZ, in);
}
gzclose(out);
return 0;
}
int main()
{
char outFileName[PATH_MAX] = { 0 };
FILE *fp = fopen(__FILE__, "r");
if (fp)
{
sprintf(outFileName, "%s.gz", __FILE__);
if (compressFile(fp, outFileName) != 0)
{
fclose(fp);
return -1;
}
fclose(fp);
}
return 0;
}
Compiled on SuSE Linux with:
gcc -Wall -pedantic -o gztest gztest.c -lz
#7
Posted 03 September 2009 - 01:49 PM
Thanks Jack, now i'm compiling all right, but i have this code:
It compiles, but when i execute it, the program shows me nothing and do nothing too, see the log, remember that the program never ends until i stop it by pressing Ctrl+Z:
Thanks,
Nathan Paulino Campos
#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;
}
It compiles, but when i execute it, the program shows me nothing and do nothing too, see the log, remember that the program never ends until i stop it by pressing Ctrl+Z:
ubuntu@eeepc:~$ ./gz test.txt [1]+ Stopped ./gz test.txt ubuntu@eeepc:~$
Thanks,
Nathan Paulino Campos
#8
Posted 03 September 2009 - 01:57 PM
Do you really want to operate on that low-level, given your apparent skill level? Why don't you work off what I've provided?
The first time through that loop, what's in outbuffer when zlib deflates it???
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);
The first time through that loop, what's in outbuffer when zlib deflates it???
#10
Posted 03 September 2009 - 02:28 PM
Actually, I misread that...the compression function is fine.
The actual problem is ... you're copying and pasting code from the Internet without putting in any significant effort to *understand* it. There's NO problem with this code; it's working EXACTLY it's been written!
It reads from stdin until end of file (which would be Ctrl-D), then compresses the data provided and outputs it to stdout.
Here's one way to use it properly (to do what I did in my sample -- compress the source file). Assuming the source file is called gz.c,
will read the contents of gz.c to stdin and pass it to the compression program, outputting the compressed data to gz.c.gz.
The actual problem is ... you're copying and pasting code from the Internet without putting in any significant effort to *understand* it. There's NO problem with this code; it's working EXACTLY it's been written!
It reads from stdin until end of file (which would be Ctrl-D), then compresses the data provided and outputs it to stdout.
Here's one way to use it properly (to do what I did in my sample -- compress the source file). Assuming the source file is called gz.c,
./gz < gz.c > gz.c.gz
will read the contents of gz.c to stdin and pass it to the compression program, outputting the compressed data to gz.c.gz.
#14
Posted 04 September 2009 - 12:31 PM
Hello Jack,
Sorry about that, i think that those tags are only to denominate that i have to put the file name there, so much sorry, but now i've copied the command, but when i try to open it, i got this errors:
If i try to use it as ZIP:
What is Wrong?
Thanks,
Nathan Paulino Campos
Sorry about that, i think that those tags are only to denominate that i have to put the file name there, so much sorry, but now i've copied the command, but when i try to open it, i got this errors:
gzip: /home/ubuntu/test.txt.gz: not in gzip format
If i try to use it as ZIP:
[/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.
What is Wrong?
Thanks,
Nathan Paulino Campos
#17
Posted 05 September 2009 - 04:39 AM
As near as I can tell, dude, this is not a programming error. It's an error by someone who doesn't get I/O redirection at the command line. Which is a necessity if you really want to be using Linux. So get Googling, instead of spamming a bunch of message boards.
#19
Posted 05 September 2009 - 04:56 AM
I was reading about I/O Redirection with this link, now i understand much more about some programs and about my own program, your tip was very nice!, but the command line that i'm using to compress is this:
For GZ Compress
For ZIP Compress
And i'm getting that errors when trying to open the archive.
Sorry,
Nathan Paulino Campos
For GZ Compress
./gz < test.txt > test.txt.gz
For ZIP Compress
./gz < test.txt > test.zip
And i'm getting that errors when trying to open the archive.
Sorry,
Nathan Paulino Campos
#20
Posted 05 September 2009 - 05:14 AM
It's because you're using deflate instead of gzip. From the FAQ:
The only way to extract the data you've created with deflate is with a call to the inflate function from code.
Which is why I used the standard gzip function calls in my sample (which works!).
Quote
# Why does gzip give an error on a file I make with compress/deflate?
The compress and deflate functions produce data in the zlib format, which is different and incompatible with the gzip format. The gz* functions in zlib on the other hand use the gzip format. Both the zlib and gzip formats use the same compressed data format internally, but have different headers and trailers around the compressed data.
The compress and deflate functions produce data in the zlib format, which is different and incompatible with the gzip format. The gz* functions in zlib on the other hand use the gzip format. Both the zlib and gzip formats use the same compressed data format internally, but have different headers and trailers around the compressed data.
The only way to extract the data you've created with deflate is with a call to the inflate function from code.
Which is why I used the standard gzip function calls in my sample (which works!).

Start a new topic
Add Reply




MultiQuote



| 


