Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › gzip compression level in Java?
New Posts  All Forums:Forum Nav:

gzip compression level in Java?

post #1 of 7
Thread Starter 
When I used zlib in C++, I'm pretty sure there was an option to set the compression level for gzipping, but I have no clue how to set a such option in Java... someone clarify?

And thanks in advance.
post #2 of 7
Set it up through the Deflater.
http://java.sun.com/j2se/1.5.0/docs/.../Deflater.html
Ever Changing
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 GO 400x9 1.392v p5q pro turbo MSI Twin Frozr III 1G/OC 6950 Kingston HX (4x1gb 800mhz) 
Hard DriveOptical DriveOSMonitor
WD 80gb 16mb cache DVd rw win 7 24" ctl 
PowerCase
Rx 630SS CM 690 II Advanced 
  hide details  
Reply
Ever Changing
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 GO 400x9 1.392v p5q pro turbo MSI Twin Frozr III 1G/OC 6950 Kingston HX (4x1gb 800mhz) 
Hard DriveOptical DriveOSMonitor
WD 80gb 16mb cache DVd rw win 7 24" ctl 
PowerCase
Rx 630SS CM 690 II Advanced 
  hide details  
Reply
post #3 of 7
Thread Starter 
Alright, that seems to be pointing in the right direction... now how would I go about using that with file streams?

EDIT: I'm currently reading into DeflaterOutputStream, nudge me if I'm on the wrong track.
Edited by bomfunk - 6/11/10 at 5:33am
post #4 of 7
I wrote this forever and a day ago but you might find it useful.
http://www.overclock.net/application...rams-java.html

Scroll down to the ZipJarUtil.

Under the src directory ZipCreate.java is the one you want to look at.
Edited by onnetz - 6/11/10 at 2:31pm
Ever Changing
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 GO 400x9 1.392v p5q pro turbo MSI Twin Frozr III 1G/OC 6950 Kingston HX (4x1gb 800mhz) 
Hard DriveOptical DriveOSMonitor
WD 80gb 16mb cache DVd rw win 7 24" ctl 
PowerCase
Rx 630SS CM 690 II Advanced 
  hide details  
Reply
Ever Changing
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 GO 400x9 1.392v p5q pro turbo MSI Twin Frozr III 1G/OC 6950 Kingston HX (4x1gb 800mhz) 
Hard DriveOptical DriveOSMonitor
WD 80gb 16mb cache DVd rw win 7 24" ctl 
PowerCase
Rx 630SS CM 690 II Advanced 
  hide details  
Reply
post #5 of 7
You set the compression level in the Deflater constructor and pass that Deflater to the DeflaterOutputStream constructor.

This should about sort you:

Code:
DeflaterOutputStream dos = new DeflaterOutputStream(s, new Deflater(Deflater.BEST_COMPRESSION));
where s is the OutputStream that you are already using (probably a FileOutputStream judging by your question)


Note that this is for DEFLATE, which is for the raw compression format used inside zip, gzip and a few other containers. Gzip adds extra headers and footers so you will not be able to write a .gz with this. GZIPOutputStream does exist but does not seem to offer any way of setting the compression level (my Java is a little rusty but I'm pretty sure it does not inherit the constructor from DeflaterOutputStream).

If you actually wanted to write a .gz that other applications (such as winrar or "gunzip") could open, off the top of my head, you would need to add the headers and footers yourself. They aren't too hard, mostly just things like unix style permission masks in the headers and a crc32 at the end.

You may be able to pass a DeflaterOutputStream to the constructor of the GZIPOutputStream but this will probably compress it twice and when you extract the .gz file you will just get the original deflated file back anyway.

gzip is specified by RFC 1952 and is pretty simple.


Alternatively, of course, you could just package an operating system specific gzip binary with your application and execute it.


If you only want your own application to be able to read the files, DEFLATE should be fine.
Edited by ghell - 6/12/10 at 5:42am
Serene Power
(13 items)
 
  
CPUMotherboardGraphicsRAM
Dual Xeon E5620 Supermicro X8DAL-i EVGA 570HD SC 8GiB Corsair DDR3 1333 ECC-R 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 120GB SSD (+16TB NAS) LG 10x Blu-Ray Windows 7 x64 / Debian x64 Dell 2408WFP, P2411H 
PowerCase
850W CoolerMaster SilentPro M CoolerMaster Cosmos Pure 
  hide details  
Reply
Serene Power
(13 items)
 
  
CPUMotherboardGraphicsRAM
Dual Xeon E5620 Supermicro X8DAL-i EVGA 570HD SC 8GiB Corsair DDR3 1333 ECC-R 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 120GB SSD (+16TB NAS) LG 10x Blu-Ray Windows 7 x64 / Debian x64 Dell 2408WFP, P2411H 
PowerCase
850W CoolerMaster SilentPro M CoolerMaster Cosmos Pure 
  hide details  
Reply
post #6 of 7
Thread Starter 
Thanks for that, some really great stuff... however:

Quote:
If you only want your own application to be able to read the files, DEFLATE should be fine.
That's the problem... this actually is for an application that gzips its files... though it uses zlib as well, so I don't see why it wouldn't open even if the format's different from what it normally uses to save the files. I'll have to try it, though.
post #7 of 7
If it is expecting the gzip format, the DEFLATE format will be missing headers and footers that it expects.

gzip is a format that builds on top of DEFLATE. All of the deflate headers and the Adler32 checksum remain inside it, but it adds its own additional headers in front and uses a CRC32 checksum at the end (better but slower).

If you just want to create the gzipped files for the other program to be able to use, compression level should not make any difference. Zlib does not care what level was used to compress it when it comes to decompressing it. In this case just use the GZIPOutputStream to do it for you. Remember to "finish" the stream at the end of writing though.

Alternatively, as I said, you could get a port of gzip for windows or the actual Windows binary or the binary for whatever operating system you use and just execute it from java. Write the file out uncompressed, such as "foobar.txt" then run "gzip -9 foobar.txt" to create "foobar.txt.gz". You can use Java to detect the OS and determine which executable to use. It's not ideal but it should work.
Serene Power
(13 items)
 
  
CPUMotherboardGraphicsRAM
Dual Xeon E5620 Supermicro X8DAL-i EVGA 570HD SC 8GiB Corsair DDR3 1333 ECC-R 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 120GB SSD (+16TB NAS) LG 10x Blu-Ray Windows 7 x64 / Debian x64 Dell 2408WFP, P2411H 
PowerCase
850W CoolerMaster SilentPro M CoolerMaster Cosmos Pure 
  hide details  
Reply
Serene Power
(13 items)
 
  
CPUMotherboardGraphicsRAM
Dual Xeon E5620 Supermicro X8DAL-i EVGA 570HD SC 8GiB Corsair DDR3 1333 ECC-R 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 120GB SSD (+16TB NAS) LG 10x Blu-Ray Windows 7 x64 / Debian x64 Dell 2408WFP, P2411H 
PowerCase
850W CoolerMaster SilentPro M CoolerMaster Cosmos Pure 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › gzip compression level in Java?