📜  Java ZipEntry getMethod()函数及示例

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

Java ZipEntry getMethod()函数及示例


getMethod()函数是Java.util.zip 包的一部分。该函数返回作为参数传递的特定 ZipEntry 的压缩方法,如果未指定,则返回 -1。
函数签名:

public int getMethod()

句法 :

zip_entry.getMethod();

参数:此函数不需要参数。
返回值:函数返回一个 int 值,这个 ZipEntry 的压缩方法,如果没有指定,则返回 -1。
异常:该函数不会抛出任何异常。

下面的程序说明了 getMethod()函数的使用

示例一:我们将创建一个名为 zip_file 的文件,并使用 getEntry()函数获取 zip 文件条目,然后获取指定 ZipEntry 的压缩方式。“file.zip”是存在于 f: 目录中的 zip 文件。我们将使用“.zip”文件作为 ZipEntry

// Java program to demonstrate the
// use of getMethod() function
  
import java.util.zip.*;
import java.util.Enumeration;
import java.util.*;
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
        try {
            // Create a Zip File
            ZipFile zip_file = new ZipFile("f:\\file1.zip");
  
            // get the Zip Entry using
            // the getEntry() function
            ZipEntry entry = zip_file.getEntry("file.zip");
  
            // Get the compression method
            // using the getMethod()
            // function
            int input = entry.getMethod();
  
            // Display the compression method
            System.out.println("compression method : " + input);
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
输出:
compression method : 8

例 2:我们将创建一个名为 zip_file 的文件,并使用 getEntry()函数获取 zip 文件条目,然后获取指定 ZipEntry 的压缩方法。“file.zip”是存在于 f: 目录中的 zip 文件。我们将使用“.cpp”文件作为 ZipEntry

// Java program to demonstrate the
// use of getMethod() function
  
import java.util.zip.*;
import java.util.Enumeration;
import java.util.*;
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
        try {
            // Create a Zip File
            ZipFile zip_file = new ZipFile("f:\\file1.zip");
  
            // get the Zip Entry using
            // the getEntry() function
            ZipEntry entry = zip_file.getEntry("file1.cpp");
  
            // Get the compression method
            // using the getMethod()
            // function
            int input = entry.getMethod();
  
            // Display the compression method
            System.out.println("compression method : " + input);
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
输出:
compression method : 8

参考: https: Java/util/zip/ZipEntry.html#getMethod–