Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

G-ZIP

Thread view: 
Chase Preuninger - 29 Mar 2008 20:43 GMT
How do you set the compression level of a G-ZIP output stream (0-9)?
Roedy Green - 29 Mar 2008 22:02 GMT
On Sat, 29 Mar 2008 12:43:10 -0700 (PDT), Chase Preuninger
<chasepreuninger@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>How do you set the compression level of a G-ZIP output stream (0-9)?

Think that is a feature of ZIP, not GZIP.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Arne Vajhøj - 30 Mar 2008 00:49 GMT
> How do you set the compression level of a G-ZIP output stream (0-9)?

It is a bit complicated, because they have protected the API
for that.

But see at the code below for how to workaround it. It should
also be noted that the default level seems to be 6 and that
it will be fine for most usage.

Arne

================================================

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

public class GZipFun {
    public static void gzip(String infnm, String outfnm) throws
IOException {
        FileInputStream is = new FileInputStream(infnm);
        GZIPOutputStream os = new GZIPOutputStream(new
FileOutputStream(outfnm));
        byte[] b = new byte[100000];
        int n;
        while((n = is.read(b)) > 0) {
            os.write(b, 0, n);
        }
        os.close();
        is.close();
    }
    public static void xgzip(String infnm, String outfnm, int level)
throws IOException {
        FileInputStream is = new FileInputStream(infnm);
        XGZIPOutputStream os = new XGZIPOutputStream(new
FileOutputStream(outfnm));
        os.setLevel(level);
        byte[] b = new byte[100000];
        int n;
        while((n = is.read(b)) > 0) {
            os.write(b, 0, n);
        }
        os.close();
        is.close();
    }
    public static void main(String[] args) throws Exception {
        gzip("C:\\z.txt", "C:\\z.txt.gz");
        for(int i = 0; i < 10; i++) {
            xgzip("C:\\z.txt", "C:\\z.txt.gz" + i, i);
        }
    }
}

class XGZIPOutputStream extends GZIPOutputStream {
    public XGZIPOutputStream(OutputStream out) throws IOException {
        super(out);
    }
    public void setLevel(int level) {
        def.setLevel(level);
    }
}
Chase Preuninger - 30 Mar 2008 19:38 GMT
Thanks, the reason I want the level is that I'm writing a compression
program where the user gets to choose the level.  I ended up however
just using a deflateroutputstream and passing it a custom deflater.

http://groups.google.com/group/java-software-develoupment?hl=en


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.