In this Java File IO tutorial, you will learn how to write Java code to read content of a ZIP file without extracting it.

You know, reading content of a ZIP file means that you list all entries contained in the file without extracting them. In the java.util.zip package, the ZipFile class can be used to open and read a ZIP file like this:

ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
Each entry is of type ZipEntry which provides the following methods for reading information of an individual entry (to name a few):

  • getName(): returns the name of the entry in form of a relative path
  • getComment(): returns comment of the entry
  • getCompressedSize(): returns the compressed size of the entry in bytes.
  • getSize(): returns the normal size (uncompressed) of the entry in bytes.
  • isDirectory(): tells whether the entry is a directory or not.
For example, the following program prints the content of a ZIP file whose path is passed from the command line:

import java.io.*;
import java.util.*;
import java.util.zip.*;

/**
 * This program reads contents of a ZIP file.
 *
 * @author www.codejava.net
 */
public class ReadZipFile {

	private static void read(String zipFilePath) {
		try {
			ZipFile zipFile = new ZipFile(zipFilePath);

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

			while (entries.hasMoreElements()) {
				ZipEntry entry = entries.nextElement();
				String name = entry.getName();
				long compressedSize = entry.getCompressedSize();
				long normalSize = entry.getSize();
				String type = entry.isDirectory() ? "DIR" : "FILE";

				System.out.println(name);
				System.out.format("\t %s - %d - %d\n", type, compressedSize, normalSize);
			}

			zipFile.close();
		} catch (IOException ex) {
			System.err.println(ex);
		}
	}


	public static void main(String[] args) {
		String zipFilePath = args[0];
		read(zipFilePath);
	}
}
 Run this program like this:

java ReadZipFile <zip_file_path>

 

For example:

java ReadZipFile exercises_week12.zip

 

This would print the following output:

exercise1/

         DIR - 0 - 0

exercise1/Book.class

         FILE - 868 - 1906

exercise1/Book.java

         FILE - 442 - 1689

exercise1/Book.java~

         FILE - 258 - 1066

exercise1/Main.class

         FILE - 1631 - 3159

exercise1/Main.java

         FILE - 707 - 1964

exercise2/

         DIR - 0 - 0

exercise2/Book.class

         FILE - 868 - 1906

exercise2/Book.java

         FILE - 442 - 1689

exercise2/BookStats.class

         FILE - 2558 - 5748

exercise2/BookStats.java

         FILE - 897 - 3292



As you can see in the output above, the entries in a ZIP file is in the form of a list which includes directories and files. And based on this information, you can write code to decompress a ZIP file properly.

 

API References:

 

Related Java Zip File Tutorials:

 

Other Java File IO Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment