LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2012, 01:39 PM   #1
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Rep: Reputation: Disabled
Gzip a String


I would like to use gzip from C++ (or C) to gzip a string. If possible, I would like to use zlib.

When I learned that I would have to use zlib to compress and uncompress, I Googled it for a few minutes and then quickly wrote a program to gzip a file and then ungzip it. However, I don't actually have any need to do that. I need to use gzip to compress and uncompress a string, not a file. I couldn't find much good documentation for using gzip on strings. Every example I find works with files.

Could someone show me a simple example?

Thanks in advance.

Brandon
 
Old 09-13-2012, 01:46 PM   #2
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
I should add that when I say a "string," I mean a char[], although if I see an example that applies to C++ strings, one can easily convert back and forth.
 
Old 09-13-2012, 09:55 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by Brandon9000 View Post
When I learned that I would have to use zlib to compress and uncompress, I Googled it for a few minutes and then quickly wrote a program to gzip a file and then ungzip it. However, I don't actually have any need to do that. I need to use gzip to compress and uncompress a string, not a file.
Well you could take your program and remove the file IO parts

You can use zlib's utility functions compress() and uncompress().
 
Old 09-14-2012, 07:31 AM   #4
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Thanks. I was thinking that I might have to figure out deflate() and inflate(). Could you give an example? Can it be made to produce a gzip header?

Last edited by Brandon9000; 09-14-2012 at 07:33 AM.
 
Old 09-14-2012, 07:53 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
probably this helps: http://stackoverflow.com/questions/4...ffer-with-zlib
 
Old 09-14-2012, 07:54 AM   #6
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Thanks, Pan. I guess my question would be whether it's possible to create it with gzip headers instead of zlib headers.
 
Old 09-14-2012, 08:38 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
gzip doesn't have headers, the program interface of gzip is zlib
 
Old 09-14-2012, 08:40 AM   #8
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Zlib and gzip both produce headers in the encoded object and they aren't the same.
 
Old 09-14-2012, 09:08 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Oh, sorry, you meant file headers...
 
Old 09-14-2012, 09:09 AM   #10
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Or if the encoding is in memory, of the encoded structure in memory. I Googled this thing for several hours yesterday.
 
Old 09-14-2012, 09:22 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
For a test I have compared the output of zlib.minigzip and gzip; the first three bytes of the file-headers are the same (1f 8b 08), but gzip also stores the original file name/attributes:

Code:
$ file *.gz
test.x.gz: gzip compressed data, from Unix
test.y.gz: gzip compressed data, was "test.y", from Unix, last modified: Sat May 28 08:40:50 2005
 
Old 09-14-2012, 09:24 AM   #12
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
So does minigzip do compression on a memory variable and, if so, what functions does it call to do it?
 
Old 09-14-2012, 09:39 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
no it uses gzopen/gzwrite/etc

BTW: have you read this: http://www.zlib.net/zlib_faq.html#faq20
 
Old 09-14-2012, 09:50 AM   #14
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Since gzopen()/gzwrite() work with files, I am not surprised that minigzip creates the same file headers as gzip. The issue is getting zlib calls to do it.

After posting the first message here yesterday, I did come across that link and read the relevant portion. According to that, I would have to use deflate/deflateInit2. If that is what I have to do, I must say that the proper settings of the parameters to deflateInit2() to produce a gzip type header are not well documented. Something about adding 16 to windowsBits. I would really like to see an explicit example of compressing and uncompressing using zlib programmed to duplicate gzip.
 
Old 09-14-2012, 09:59 AM   #15
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Why, it is in gzio.c in zlib source, check deflateInit2 in function gz_open, and writing crc in gzclose.

PS: Or, if you want to handle files in memory, fmemopen(3) might be useful.
 
  


Reply

Tags
compression, gzip, zip, zlib



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
how to open gzip file? I have gzip installed mhg Linux - Newbie 3 10-24-2008 02:18 AM
[gzip] appears in process table after gzip command completes redmanDBA Linux - General 0 02-26-2008 06:12 AM
apt-get update error: "gzip: stdin: not in gzip format" haertig Debian 0 08-02-2006 05:21 PM
Solaris 8 gzip issues- cannot gzip -d lynx sixosix Solaris / OpenSolaris 4 03-13-2005 03:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration