How to Unzip a Zip File in Java

The Java programming language was developed for easy cross-platform programming. With Java, a programmer can write a single application and run it, with little to no modification, on almost any operating system. One of the features built directly into Java is the ability to read and write ZIP archive files using the ZipEntry and ZipFile classes. The ZipFile class provides a collection of ZipEntries, and each ZipEntry provides a standard stream that can be read with any of Java's built-in Stream classes, including the easy-to-use Scanner class.

Instructions

    • 1

      Open Netbeans. These instructions will assume you use the NetBeans Integrated Development Environment (IDE) that comes free with Sun Microsystems' version of Java, but they will work with any plain text editor with a minimum of modification.

    • 2

      Create a new project named "ZipExample" by clicking "File," "New Project." This will automatically set up a default class file for you named Main that already has a valid 'main' method and open it.

    • 3

      Paste the following at the top of the file, above the line that reads "public class Main" and below the line "package zipexample."

      import java.io.File;

      import java.io.FileOutputStream;

      import java.io.IOException;

      import java.io.InputStream;

      import java.util.Enumeration;

      import java.util.Scanner;

      import java.util.zip.ZipEntry;

      import java.util.zip.ZipFile;

    • 4

      Paste the following within the main method:

      public static void main(String[] args) {

      // Get the file name of a zip file from the command line.

      // Alternatively, write your own file name here.

      String filename = args[0];

      try {

      // Open the zip file.

      ZipFile archive = new ZipFile(filename);

      Enumeration<? extends ZipEntry> fileList = archive.entries();

      // Go through each file in the ZIP archive.

      for (ZipEntry e = fileList.nextElement();

      fileList.hasMoreElements();

      e = fileList.nextElement()) {

      // Print some info to let the user know what is happening.

      System.out.println("Expanding " + e.getName());

      // If the zip entry is a directory, make the directory.

      if (e.isDirectory()) new File(e.getName()).mkdir();

      else {

      // If it's not a directory, read the data from

      // the zip archive and write it to the disk.

      InputStream in = archive.getInputStream(e);

      Scanner scan = new Scanner(in);

      FileOutputStream fout = new FileOutputStream(e.getName());

      while (scan.hasNextByte()) {

      fout.write(scan.nextByte());

      }

      // Close the file.

      fout.close();

      }

      }

      }catch (IOException e) {

      // If there is any sort of error reading or writing, print

      // an error message to the console.

      System.out.println(e.getMessage());

      }

      }

    • 5

      Click the green arrow to run the program.

Related Searches:

References

Resources

Comments

You May Also Like

  • How to Unzip an Eclipse File

    Eclipse is a software development tool supporting multiple languages. Eclipse uses plug-ins to extend its capabilities, including adding additional languages. Java is...

  • How to Download Java as a Zip File

    Java is a website utility that allows your computer to understand, process and display certain visual elements of Internet pages, such as...

  • How to Unzip WAR Files in Windows

    Web Archive Files (WAR) are just like any other .zip files in the sense that they are auto-compressed in a directory and...

  • Problems With Unzipping Files

    Pkware introduced the concept of zipped files with their pkzip/pkunzip utility. Zip files became the most popular method for storing files on...

  • How to Extract Zip Files in Java

    Extracting zip files is accomplished using an internal Java library of classes that makes it easy for programmers. Java uses a zip...

  • How to Unzip Zip Files on a Mac

    Zip files are archive files compressed with the Zip compression algorithm. Decompressing a Zip file is called "unzipping." The Mac OS X...

  • How to Unzip a JAR File

    JAR is an archive format. These file types store other files inside themselves, making the stored files smaller and so easier to...

  • How to Unzip an AIX

    AIX files are created by the Hewlett-Packard NewWave Cardfile application. They contain data related to the software and can be zipped up...

  • How to Read From a File With JAVA

    As JAVA has become a very popular tool for both application and web programming, it pays to have an understanding of the...

  • How to Unzip Files That Are Blocked

    If your computer's firewall or antivirus software detects (either correctly or incorrectly) a threat with one of the files contained in a...

  • How to Read Zip Files With Java

    You may not realize it, but Java already has all the tools it needs to handle the common file compression and packaging...

  • How to Unzip an Archive Containing JavaScript Files

    JavaScript, a script language created and produced by Sun Microsystems, gives designers the ability to craft programs and applications for online use....

  • How Can I Download Java As a Zip File?

    Java is an Internet-based application that gives your browser the ability to display videos and web page backgrounds with heavy graphics. Although...

  • How to Use a Packaging API to Unzip & Zip

    The Windows application programming interface (API) comes with the capability to zip and unzip files. You do not need a third-party application...

  • How to Unzip a WinRAR Zip Archive File

    ZIP archive files are created using "Zip" compression, which compresses one or more files individually and bundles them together inside a single...

  • How to Unzip a RAR File for Free

    RAR files are a special kind of format called an archive. Archives store other files within themselves. The files compressed inside of...

Related Ads

Featured