Open In App

Difference Between ZipFile and ZipInputStream in Java

Last Updated : 07 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Zip files are basically an archive file that is used to compress all the files in one place. Doing this reduces memory space occupied and makes transport of files bundle easy. ZipInputStream which are used to read the file entries in zip files present in the zip. In Java, there are two classes namely ZipFile and ZipInputStream which are used to read the file entries present in zip files. Both the classes are found inside the java.util.zip class, and both classes implement the closeable interface as they are both very useful in reading and extracting zip files.

ZipFile class is used to read the files which are compressed into a zip file. This class provides several methods to access the entries in the zip file. Also, there are several other methods also present in this class but it is not a matter of our concern now. Some are listed below as follows:

  • ZipEntry getEntry(String name): The getEntry() function is a part of java.util.zip package. The function returns the ZipEntry of file present in the zip file specified by the string parameter. This method is used to get the entry of the file whose name is specified in the string parameter.
  •  InputStream getInputStream(ZipEntry entry) : This method is used for creating an input stream to read the data of the entry(file)
  • Enumeration<? extends ZipEntry> entries(): This is the very important method implemented by this class. This method is used to generate an enum of all the entries which can then be accessed individually in any order. Hence, this method ensures the non-sequential access of the files. We can also access only those files which are required, and we are not required to extract all the files.

Implementation: This is how we access the zip File class:

Java




// Java Program to illustrate extraction of
// Zip file
 
// Importing classes and modules
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.Enumeration;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
 
// save as file named GFG.java
 
// Class
public class GFG {
 
    // Step 1: Specify the file name
    // with path of the zip file
 
    // Custom directory from local directory
    // is passed as an argument
    public static String file_path
        = "C:\\Users\\Dipak\\Desktop\\j.zip";
 
    // Step 2: Specify the name of the file
    // present in the zip file to be accessed
    public static String file_name = "j/ritu.txt";
 
    // Also do remember that one can take input
    // for the file path and name
    // Using Zipinputstream method
    public static BufferedInputStream b;
    public static ZipInputStream z;
 
    // Zipinputsream()  method for implementation
    public static void zipinputstream() throws IOException
    {
        z = new ZipInputStream(b);
        ZipEntry e;
 
        // If condition holds true
        while (true) {
 
            // Read the next ZIP file entry positioning
            // the stream at beginning
            e = z.getNextEntry();
 
            if (e == null)
                break;
            if (e.getName().equals(file_name)) {
                // Display message
                System.out.println("file size is "
                                   + e.getSize()
                                   + " bytes");
            }
        }
    }
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
 
        b = new BufferedInputStream(
            new FileInputStream(file_path));
 
        // calling static method
        zipinputstream();
    }
}


Output:

ZipInputStream class also used to get the entries and the metadata of the files present in the zip file. This class also implements the same methods as the above class does, except the two methods namely as follows:

  • getInputStream(): This class itself is an input stream so there is no need for this method to be implemented.
  • Enumeration<? extends ZipEntry> entries(): Since the above method is not implemented in this class. So we will not be able to get random access to any file or entry in the zip file. So we basically need to search the whole zip file to access a particular file. This class gives sequential access to the files in the zip file.

Implementation: This is how to access ZipInputStream class

Java




// Java Program to illustrate extraction of
// ZipInputStream
 
// Importing classes and modules
import java.io.BufferedInputStream;
import java.io.FileInputStream;
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;
import java.util.zip.ZipInputStream;
// save as file named GFG.java
 
// CLass
public class GFG {
 
    // file name with path of the zip file.
    public static String file_path
        = "C:\\Users\\Dipak\\Desktop\\j.zip";
   
    // name of the file present in the zip file to be
    // accessed.
    public static String file_name = "j/gfg.txt";
   
    // we can also take input for the file path and name
    // Using ZipFile method;
    public static void zipfile() throws IOException
    {
        // creating an object of zip file class
        ZipFile f = new ZipFile(file_path);
       
        // getting all its entries
        Enumeration<? extends ZipEntry> entry = f.entries();
       
        // checking for the particular file we require and
        // printing its size
        while (entry.hasMoreElements()) {
            final ZipEntry e = entry.nextElement();
            if (e.getName().equals(file_name))
                System.out.println(
                    "size of file specified is "
                    + e.getSize() + " bytes");
        }
    }
 
    public static void main(String[] args) throws Exception
    {
        // calling static method
        zipfile();
    }
}


Output: 

Hence, the main difference between the two classes is that one allows free movement and access of files (by creating enumeration) while other not. This leads to a large complexity issues while handling zip files with big data entries. Hence, we can see that accessing file using ZipFile class is much easier than ZipinputStream.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads