How to read a JAR or ZIP file in Java

Java provides built-in support for reading ZIP files using classes in java.util.zip package.

We can extract a zip file in java using either java.util.zip.ZipFile or java.util.zip.ZipInputStream.

Since a JAR file is a special type of ZIP file, we can use the same approach with JAR files too.
 

Reading a ZIP file using ZipFile

Here we create ZipFile object using the Zip file name and then get all the Zip file’s entries into an enumeration by calling the entries() method of the ZipFile object.

Once we have the Zip file entries as an enumeration, we can step through the entries and instantiate a ZipEntry object for each entry as shown below.

package com.topjavatutorial;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ReadZipFileUsingZipFile {

  final static String OUTPUT_DIR = "C:\\blogs\\readingzipfile\\output\\";

  public static void main(String[] args){
    ZipFile file = null;
    try {
      file = new ZipFile("C:\\blogs\\readingzipfile\\test.zip");

      Enumeration<? extends ZipEntry> entries = file.entries();
      while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        InputStream stream = file.getInputStream(entry);
        System.out.println("Extracting " + entry.getName());
        readEntry(entry, stream);
      }
      System.out.println("Contents of " + file.getName()
          + " extracted at path : " + OUTPUT_DIR);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if(file != null)
        try {
          file.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    }

  }

  private static void readEntry(ZipEntry entry, InputStream stream)
      throws IOException {
    final byte[] buf = new byte[1024];
    int length;
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(OUTPUT_DIR + entry.getName());

      while ((length = stream.read(buf, 0, buf.length)) >= 0) {
        fos.write(buf, 0, length);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      fos.close();
    }
  }
}

Output :

Extracting temp.txt
Contents of C:\blogs\readingzipfile\test.zip extracted at path : C:\blogs\readingzipfile\output\

 

Reading a ZIP file using ZipInputStream

package com.topjavatutorial;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ReadZipFileUsingZipInputStream {

  final static String OUTPUT_DIR = "C:\\blogs\\readingzipfile\\output\\";
  final static String ZIP_FILE = "C:\\blogs\\readingzipfile\\test.zip";

  public static void main(String[] args) {

    BufferedInputStream bis;
    ZipInputStream zis = null;
    try {
      bis = new BufferedInputStream(new FileInputStream(ZIP_FILE));
      zis = new ZipInputStream(bis);
      ZipEntry entry;
      while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Extracting " + entry.getName());
        readEntry(entry, zis);
      }
      System.out.println("Contents of " + ZIP_FILE
          + " extracted at path : " + OUTPUT_DIR);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        zis.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

  }

  private static void readEntry(ZipEntry entry, InputStream stream)
      throws IOException {
    final byte[] buf = new byte[1024];
    int length;
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(OUTPUT_DIR + entry.getName());

      while ((length = stream.read(buf, 0, buf.length)) >= 0) {
        fos.write(buf, 0, length);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      fos.close();
    }
  }
}

Output :

Extracting temp.txt
Contents of C:\blogs\readingzipfile\test.zip extracted at path : C:\blogs\readingzipfile\output\

© 2017, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags