当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ZipFile和ZipInputStream的区别用法及代码示例


压缩文件本质上是一个存档文件,用于将所有文件压缩到一个位置。这样做可以减少占用的内存空间并使文件包的传输变得容易。Java.util.zip.ZipInputStream用于读取 zip 中存在的 zip 文件中的文件条目。在Java中,有两个类,即ZipFileJava.util.zip.ZipInputStream用于读取 zip 文件中存在的文件条目。这两个类都可以在 java.util.zip 类中找到,并且这两个类都实现 closeable 接口,因为它们在读取和提取 zip 文件时都非常有用。

ZipFile entries() 用于读取压缩为 zip 文件的文件。此类提供了多种方法来访问 zip 文件中的条目。此外,该类中还存在其他几种方法,但现在不是我们关心的问题。部分列举如下:

  • ZipFile getEntry() getEntry()函数是java.util.zip包的一部分。该函数返回Java.util.zip.ZipEntry由字符串参数指定的 zip 文件中存在的文件的数量。该方法用于获取字符串参数中指定名称的文件的条目。
  • ZipFile getInputStream()该方法用于创建输入流来读取条目(文件)的数据
  • ZipFile 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 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();
    }
}

输出:

Java.util.zip.ZipInputStream 还用于获取 zip 文件中存在的文件的条目和元数据。该类还实现了与上述类相同的方法,除了以下两个方法:

  • ZipFile getInputStream()这个类本身就是一个输入流,所以不需要实现这个方法。
  • ZipFile 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 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();
    }
}

输出:

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.



相关用法


注:本文由纯净天空筛选整理自harshkumarchoudhary144大神的英文原创作品 Difference Between ZipFile and ZipInputStream in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。