📜  从 Zip 文件读取和打印所有文件的Java程序

📅  最后修改于: 2022-05-13 01:54:32.850000             🧑  作者: Mango

从 Zip 文件读取和打印所有文件的Java程序

zip 文件是一个或多个文件被压缩在一起的文件,通常,zip 文件是存储大文件的理想选择。这里的 zip 文件将首先读取并同时使用Java程序使用Java.util.zip.ZipEntry 类来打印 zip 文件的内容来标记 zip 文件,读取后将打印其中的内容.如果万一,没有找到 zip 文件,代码将抛出一个输入输出异常 FileNotFoundException

插图:

考虑一个存储名为 zip 文件的系统

  • windows操作系统D盘的geekforgeeks.zip
  • Windows 操作系统中的 D:/geeks.zip
  • macOS 操作系统中的 Archive.zip

案例一: geekforgeeks.zip

Input  : D:/geekforgeeks.zip
Output : The files in the Zip are as follows:
         Java program to print a number.java
         Java program to print your name.java
         Java program to calculate.java
         Java program to print a pattern.java

现在,如果系统不存储用户输入的特定 zip 文件,则代码将抛出异常并打印一条消息,提示尚未找到该文件。

案例 2: D:/geeks.zip

Input  :  D:/geeks.zip
Output : java.io.FileNotFoundException: D:/geeks.zip (The system cannot find the file specified)

案例 3: Archive.zip

它与案例 1 相同,因此仅在不同的操作系统上为实现部分保留它,如更深入的示例所示。

方法

  • 将 zip 文件的位置作为 main 方法中的输入。
  • zip 文件的位置现在发送到该方法。
  • 在该方法中,读取文件并打印其内容。
  • 如果万一文件未找到,代码将抛出异常。

例子

Java
// Java program to read and print all files
// from a zip file
 
// Importing input output classes
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
// Importing zip classes and Scanner class
// from java.util package
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
 
// Class to Read and print the Zip Files
public class GFG {
 
    // Function to read and print the file names.
    public void printFileContent(String filePath)
    {
 
        // Creating objects for the classes and
        // initializing them to null
        FileInputStream fs = null;
        ZipInputStream Zs = null;
        ZipEntry ze = null;
 
        // Try block to handle if exception occurs
        try {
 
            // Display message when program compiles
            // successfully
            System.out.println(
                "Files in the zip are as follows: ");
 
            fs = new FileInputStream(filePath);
            Zs = new ZipInputStream(
                new BufferedInputStream(fs));
 
            // Loop to read and print the zip file name till
            // the end
            while ((ze = Zs.getNextEntry()) != null) {
                System.out.println(ze.getName());
            }
 
            // Closing the file connection
            Zs.close();
        }
 
        // Catch block to handle if any exception related
        // to file handling occurs
        catch (FileNotFoundException fe) {
 
            // Print the line line and exception
            // of the program where it occurred
            fe.printStackTrace();
        }
 
        // Catch block to handle generic exceptions
        catch (IOException ie) {
 
            // Print the line line and exception
            // of the program where it occurred
            ie.printStackTrace();
        }
    }
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of the file
        GFG zf = new GFG();
 
        // Taking input of the zip file from local directory
        // Name of the zip file to be read should be entered
        Scanner sc = new Scanner(System.in);
 
        // Display message asking user to enter
        // zip file local directory
        System.out.println(
            "Enter the location of the zip file: ");
        String str = sc.nextLine();
 
        // Print the zip files(compressed files)
        zf.printFileContent(str);
    }
}


输出:

如上所示,以下 zip 文件打印出其中的 5 个文件。 (.png 格式的所有图像)