SunQuest
 
       Java
  Home arrow Java arrow Page 3 - GZIPping with Java
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

GZIPping with Java
By: Kulvir Singh Bhogal and Javid Jamae
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 26
    2004-02-18

    Table of Contents:
  • GZIPping with Java
  • GZIP vs. ZIP
  • Enough… Let’s Write Some GZIP Code
  • And the Other Way Around: Uncompressing Your GZIP Files (Programmatically)
  • Leveraging GZIP in the Web World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    GZIPping with Java - Enough… Let’s Write Some GZIP Code


    (Page 3 of 5 )

    Programmatically creating a file in the gzip format is rather straightforward.  In the code below, we do just that.  We start off by creating a new GZIPOutputStream object. The constructor of GZIPOutputStream takes a FileOutputStream object. We instantiate the FileOutputStream object using a String that specifies the location where our gzip file should be created.
     
    Next, we create an InputFileStream that points to the (uncompressed) file that we wish to compress. We create a byte[] to use as a buffer to write to the GZIPOutputStream.  Then we use a while loop to read our input file and write it to our GZIPOutputStream using our buffer. The process of creating the GZIP file is completed by using the finish and close methods on the GZIPOutputStream. Our sample code will create a file named examplegzip.gz in a directory named c:\articles, using the file statebirds.txt from the same directory as the source file to be zipped.
     


    System.out.println("Creating gzip file.");
    // Specify gzip file name
    String gzipFileName = "c:\articles\examplegzip.gz";
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzipFileName));
     

    // Specify the input file to be compressed
    String inFilename = “c:\articles\statebirds.txt";
    FileInputStream in = new FileInputStream(inFilename);
     

    // Transfer bytes from the input file 
    // to the gzip output stream
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
    }
    in.close();
     

    // Finish creation of gzip file
    out.finish();
    out.close();


     
    After you are finished writing the file using the GZIPOutputStream, a checksum value representing the original (uncompressed) data is stored in the trailer of the GZIP file.
     
    In the example above, we are reading from a file. Of course, because we are using an InputStream we could just as easily read from another data source, such as a socket.

    More Java Articles
    More By Kulvir Singh Bhogal


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway