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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
09-13-2012, 01:39 PM
|
#1
|
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 101
Rep: 
|
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
|
|
|
|
09-13-2012, 01:46 PM
|
#2
|
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 101
Original Poster
Rep: 
|
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.
|
|
|
|
09-13-2012, 09:55 PM
|
#3
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,022
|
Quote:
Originally Posted by Brandon9000
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().
|
|
|
|
09-14-2012, 07:31 AM
|
#4
|
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 101
Original Poster
Rep: 
|
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.
|
|
|
|
09-14-2012, 07:53 AM
|
#5
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,804
|
|
|
|
|
09-14-2012, 07:54 AM
|
#6
|
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 101
Original Poster
Rep: 
|
Thanks, Pan. I guess my question would be whether it's possible to create it with gzip headers instead of zlib headers.
|
|
|
|
09-14-2012, 08:38 AM
|
#7
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,045
|
gzip doesn't have headers, the program interface of gzip is zlib
|
|
|
|
09-14-2012, 08:40 AM
|
#8
|
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 101
Original Poster
Rep: 
|
Zlib and gzip both produce headers in the encoded object and they aren't the same.
|
|
|
|
09-14-2012, 09:08 AM
|
#9
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,045
|
Oh, sorry, you meant file headers...
|
|
|
|
09-14-2012, 09:09 AM
|
#10
|
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 101
Original Poster
Rep: 
|
Or if the encoding is in memory, of the encoded structure in memory. I Googled this thing for several hours yesterday.
|
|
|
|
09-14-2012, 09:22 AM
|
#11
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,045
|
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
|
|
|
|
09-14-2012, 09:24 AM
|
#12
|
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 101
Original Poster
Rep: 
|
So does minigzip do compression on a memory variable and, if so, what functions does it call to do it?
|
|
|
|
09-14-2012, 09:39 AM
|
#13
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,045
|
no it uses gzopen/gzwrite/etc
BTW: have you read this: http://www.zlib.net/zlib_faq.html#faq20
|
|
|
|
09-14-2012, 09:50 AM
|
#14
|
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 101
Original Poster
Rep: 
|
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.
|
|
|
|
09-14-2012, 09:59 AM
|
#15
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,045
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:38 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|