School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,144 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,812 people online right now. Registration is fast and FREE... Join Now!



Compress Files Example

  • (2 Pages)
  • +
  • 1
  • 2

Compress Files Example Rate Topic: -----

#1 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

Post icon  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
Was This Post Helpful? 0
  • +
  • -


#2 Oler1s  Icon User is offline

  • D.I.C Addict
  • PipPipPipPip
  • Group: Members
  • Posts: 927
  • Joined: 04-June 09


Dream Kudos: 0

Posted 03 September 2009 - 11:12 AM

Your own zipping/unzipping library? Well, a good first start would be to read the actual specifications for a zip file.
Was This Post Helpful? 0
  • +
  • -

#3 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

Posted 03 September 2009 - 11:19 AM

Oler1s, i'm not trying to build my own zip library, but use an existing to build my own PROGRAM to zip files.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Addict
  • PipPipPipPip
  • Group: Members
  • Posts: 927
  • Joined: 04-June 09


Dream Kudos: 0

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...)
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is online

  • Mayor of Simpleton
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 7,129
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

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.
#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

Was This Post Helpful? 0
  • +
  • -

#6 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

Posted 03 September 2009 - 01:32 PM

I'm going to try your code Jack.

Thanks!

This post has been edited by nathanpc: 03 September 2009 - 01:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

Posted 03 September 2009 - 01:49 PM

Thanks Jack, now i'm compiling all right, but i have this 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;
}

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
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is online

  • Mayor of Simpleton
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 7,129
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

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?

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???
Was This Post Helpful? 0
  • +
  • -

#9 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

Posted 03 September 2009 - 02:08 PM

I don't know!
If i understood, the thing is: There are no files to input in the zip file.
I'm correct?

This post has been edited by nathanpc: 03 September 2009 - 02:08 PM

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is online

  • Mayor of Simpleton
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 7,129
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

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,
./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.
Was This Post Helpful? 0
  • +
  • -

#11 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

Posted 03 September 2009 - 02:56 PM

If i do this is the same:
ubuntu@eeepc:~$ ./gz test.txt test.gz

[1]+  Stopped				 ./gz test.txt test.gz
ubuntu@eeepc:~$

Was This Post Helpful? 0
  • +
  • -

#12 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

Posted 04 September 2009 - 11:12 AM

Help me please!

Thanks!
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is online

  • Mayor of Simpleton
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 7,129
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

Posted 04 September 2009 - 12:11 PM

Sorry, your reading comprehension skills suck. I understand that English is not your native language, but the answer has already been posted. You can't even seem to copy and paste a shell command correctly.
Was This Post Helpful? 0
  • +
  • -

#14 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

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:
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
Was This Post Helpful? 0
  • +
  • -

#15 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

Posted 04 September 2009 - 06:37 PM

Needing Help!

Thanks!
Was This Post Helpful? 0
  • +
  • -

#16 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

Posted 05 September 2009 - 03:50 AM

Please, someone help me!
This is the only project that i'm liking so much to develop, the only thing that i want is to have a simple code to do this, then i will study it better, then i will improve it.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#17 JackOfAllTrades  Icon User is online

  • Mayor of Simpleton
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 7,129
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

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.
Was This Post Helpful? 0
  • +
  • -

#18 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

Posted 05 September 2009 - 04:48 AM

Sorry, the only thing that i want is to know what is wrong in my code/

Thanks!
Was This Post Helpful? 0
  • +
  • -

#19 nathanpc  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 469
  • Joined: 31-July 09


Dream Kudos: 175

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
./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
Was This Post Helpful? 0
  • +
  • -

#20 JackOfAllTrades  Icon User is online

  • Mayor of Simpleton
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 7,129
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

Posted 05 September 2009 - 05:14 AM

It's because you're using deflate instead of gzip. From the FAQ:

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 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!).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month