Thursday, July 7, 2011

Unzip and Gzip with Java

Download the zipped file.  Get the files within the zipped file and gzip each of the file.


ZipInputStream zipStream = null;

try {
    final int BUFFER = 2048;

    GZIPOutputStream dest = null
;
    zipStream = new ZipInputStream(new BufferedInputStream(
                                   new FileInputStream(file)));

    ZipEntry zipEntry;
    while ((zipEntry = zipStream.getNextEntry()) != null) {
       int count;
       byte data[] = new byte[BUFFER];

       String reportName = zipEntry.getName();
       File report = File.createTempFile("rpt", "gz");
       dest = new GZIPOutputStream(

              new FileOutputStream(report), BUFFER);

       while ((count = zipStream.read(data, 0, BUFFER)) != -1) {
         dest.write(data, 0, count);
       }
       dest.flush();
       dest.close();
    } finally {
       if(zipStream != null) {
          zipStream.close();
 
       }   
    }

No comments:

Post a Comment