tags:

views:

184

answers:

3

How can I extract a data from a zipped file uising JAVA libraries ? Is there a library that does unzipping and the I get the file and manipulate it ?

+2  A: 

http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/package-summary.html http://www.roseindia.net/java/beginners/JavaUncompress.shtml

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

public class JavaUncompress{
public static void main(String args[]){
try{
  //To Uncompress GZip File Contents we need to open the gzip file.....
  if(args.length<=0){
    System.out.println("Please enter the valid file name");
  }
  else{
    String inFilename = args[0];
    System.out.println("Opening the gzip file.......................... :  opened");


    ZipInputStream zipInputStream = null;
    FileInputStream fileInputStream = null;
    zipInputStream = new ZipInputStream(new 

FileInputStream(inFilename));
    System.out.println("Opening the output file............ : opened");
    String outFilename = inFilename +".pdf";
    OutputStream out = new FileOutputStream(outFilename);
    System.out.println("Transferring bytes from the 

compressed file to the output file........:
     Transfer successful");
    byte[] buf = new byte[1024];  //size can be 

//changed according to programmer's need.
    int len;
    while ((len = zipInputStream.read(buf)) > 0) {
      out.write(buf, 0, len);
    }
    System.out.println("The file and stream is ......closing.......... : closed"); 
    zipInputStream.close();
    out.close();
      }
    }
    catch(IOException e){
  System.out.println("Exception has been thrown" + e);
    }
  }
}
sadboy
The original question is asking about zip, not gzip. The procedure for extracting zipfiles is different; see the article referenced in NomNom's answer.
rob
The link to the package I referenced applies to zip and gzip, sorry about the example being gzip.
sadboy
I'm somewhat amused that all your messages print "opened" "transferred" and "closed" in the same line as the "preparing to" message... when you actually haven't done any of it until later ^_^
Steven Schlansker
@Steven Schlansker It was a quick copy and paste from here. http://www.roseindia.net/java/beginners/JavaUncompress.shtml guess I should have checked it.
sadboy
+4  A: 

You could use the "java.util.zip" package.

See this article by Sun.

NomNom
Other powerful options are TrueZip and 7-Zip-JBinding
Carlos Tasada
A: 

Commons Compress http://commons.apache.org

Raymond