SunQuest
 
       Java
  Home arrow Java arrow Page 4 - 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 
 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

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    GZIPping with Java - And the Other Way Around: Uncompressing Your GZIP Files (Programmatically)


    (Page 4 of 5 )

    Decompressing a gzip file with the java.util.zip API is pretty straightforward too.  The class below decompresses the file we created in the previous section. The class begins by creating a new GZIPInputStream stream object. We pass the GZIPInputStream constructor the filename of the gzip file we want to decompress (i.e., c:\articles\examplegzip.gz). Next, we use the same byte buffer technique to read from the GZIPInputStream object and write to a FileOutputStream object. To clean things up, we close our GZIPInputStream and our OutputStream. Our sample program will create a file named statebirdsclone.txt, which is exactly the same as its original, statebirds.txt that we compressed in the previous section.
     


    // Open the gzip file
    String inFilename = "c:\articles\examplegzip.gz";
    GZIPInputStream gzipInputStream =
    new GZIPInputStream(new FileInputStream(inFilename));
    // Open the output file
    String outFilename = "c:\articles\statebirdsclone.txt";
    OutputStream out = new FileOutputStream(outFilename);
     

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

    // Close the file and stream
    gzipInputStream.close();
    out.close();


     
    After reading the entire file using the GZIPInputStream, the read method will compare the checksum of the decompressed data with the checksum that is stored in the trailer of the GZIP file.  If the values do not match, the read method will throw an exception.

    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 4 hosted by Hostway