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:
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
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.