Zip文件基本上是一种存档文件,用于将所有文件压缩在一个地方。这样做可以减少占用的内存空间,使文件捆绑运输变得容易。ZipInputStream用于读取Zip文件中存在的文件条目。在Java中,有两个类,即ZipFileZipInputStream,用于读取Zip文件中的文件条目。这两个类都在java.util.zip类中找到,这两个类都实现了closeable接口,因为它们在读取和提取zip文件时都非常有用。

ZipFile类用于读取被压缩成zip文件的文件。这个类提供了几个方法来访问压缩文件中的条目。此外,在这个类中还有其他一些方法,但现在不是我们关心的问题。下面列出了一些方法,如下:

  • ZipEntry getEntry(String name): getEntry()函数是java.util.zip包的一部分。该函数返回由字符串参数指定的zip文件中存在的ZipEntry文件。该方法用于获取名称由字符串参数指定的文件的条目。
  • InputStream getInputStream(ZipEntry entry) : 该方法用于创建一个输入流来读取条目(文件)的数据。
  • Enumeration<? extends ZipEntry> entries(): 这是该类实现的非常重要的方法。该方法用于生成所有条目的枚举,然后可以按任何顺序单独访问。因此,这个方法确保了文件的非顺序访问。也可以只访问那些需要的文件,而不需要提取所有的文件。

实现: 下面是访问 zip File 类的方式:

// 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 YiibaiDemo.java

// Class
public class YiibaiDemo {

    // 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\\Yiibai\\Desktop\\yiibai.com.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();
    }
}

ZipInputStream类 也被用来获取压缩文件中的条目和文件的元数据。这个类也实现了与上述类相同的方法,只是有两个方法即如下所示:

  • getInputStream(): 这个类本身就是一个输入流,所以不需要实现这个方法。
  • Enumeration<? extends ZipEntry> entries(): 由于上述方法没有在这个类中实现。所以我们将无法随机访问压缩文件中的任何文件或条目。所以我们基本上需要搜索整个压缩文件来访问一个特定的文件。这个类提供了对压缩文件中的文件的顺序访问。

实现:下面如何访问ZipInputStream类的方法

// 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 YiibaiDemo.java

// CLass
public class YiibaiDemo {

    // file name with path of the zip file.
    public static String file_path
        = "C:\\Users\\yiibai\\Desktop\\vsdiffer.com.zip";

    // name of the file present in the zip file to be
    // accessed.
    public static String file_name = "yiibai/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();
    }
}

| ——- | ——- | ——- |

Java中ZipFile和ZipInputStream的区别

欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果。
转载请注明:文章转载自 有区别网 [http://www.vsdiffer.com]
本文标题:Java中ZipFile和ZipInputStream的区别
本文链接:https://www.vsdiffer.com/vs/difference-between-zipfile-and-zipinputstream-in-java.html
免责声明:以上内容仅代表 个人看法、理解、学习笔记、总结和研究收藏。不保证其正确性,因使用而带来的风险与本站无关!如本网站内容冒犯了您的权益,请联系站长,邮箱: ,我们核实并会尽快处理。