Zip 文件基本上是一种存档文件,用于将所有文件压缩到一个位置。这样做可以减少占用的内存空间并使文件包的传输变得容易。 ZipInputStream用于读取 zip 中存在的 zip 文件中的文件条目。在Java,有两个类,即ZipFile和ZipInputStream ,它们用于读取 zip 文件中存在的文件条目。这两个类都可以在Java.util.zip 类中找到,并且两个类都实现了 closeable 接口,因为它们在读取和提取 zip 文件时都非常有用。
ZipFile 类 用于读取压缩成 zip 文件的文件。此类提供了多种方法来访问 zip 文件中的条目。此外,该类中还存在其他几种方法,但现在不是我们关心的问题。下面列出了一些:
- ZipEntry getEntry(String name) : getEntry()函数是Java.util.zip 包的一部分。该函数返回由字符串参数指定的 zip 文件中存在的文件的 ZipEntry。此方法用于获取在字符串参数中指定名称的文件的条目。
- InputStream getInputStream(ZipEntry entry) :该方法用于创建一个输入流来读取 entry(file) 的数据
- 枚举 extends ZipEntry> entries() :这是这个类实现的非常重要的方法。此方法用于生成所有条目的枚举,然后可以按任何顺序单独访问这些条目。因此,此方法可确保文件的非顺序访问。我们也可以只访问那些需要的文件,我们不需要提取所有文件。
实现:这是我们访问 zip File 类的方式:
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 rememember 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 begining
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();
}
}
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 particulat 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();
}
}
输出:
ZipInputStream 类 还用于获取 zip 文件中存在的文件的条目和元数据。该类还实现了与上述类相同的方法,除了两个方法如下:
- getInputStream() :这个类本身就是一个输入流,所以不需要实现这个方法。
- 枚举 extends ZipEntry> entries() :由于上述方法未在此类中实现。因此,我们将无法随机访问 zip 文件中的任何文件或条目。所以我们基本上需要搜索整个 zip 文件来访问特定文件。此类提供对 zip 文件中文件的顺序访问。
实现:这是访问 ZipInputStream 类的方法
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 particulat 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();
}
}
输出:
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.