當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。