java zipfile用法_Java ZipFile getEntry()用法及代码示例

getEntry()函数是java.util.zip软件包的一部分。该函数返回string参数指定的zip文件中存在的文件的ZipEntry。

函数签名:

public ZipEntry getEntry(String name)

用法:

zip_file.getEntry();

参数:该函数带有一个字符串参数,即文件名。

返回值:该函数返回由string参数指定的zip文件的ZipEntry。

Exceptions:如果zip文件已关闭,则该函数将引发IllegalStateException。

以下示例程序旨在说明getEntry()函数的使用

范例1:创建一个名为zip_file的文件,并使用getEntry()函数获取zip文件条目。 “file.zip”是f:目录中的一个zip文件。

// Java program to demonstrate the

// use of getEntry() function

import java.util.zip.*;

import java.util.Enumeration;

public class solution {

public static void main(String args[])

{

try {

// Create a Zip File

ZipFile zip_file

= new ZipFile("f:\\file.zip");

// get the Zip Entry using

// the getEntry() function

ZipEntry entry

= zip_file.getEntry("file1.cpp");

// display the Zip Entry

System.out.println("Name:"

+ entry.getName());

}

catch (Exception e) {

System.out.println(e.getMessage());

}

}

}

输出:

Name:file1.cpp

范例2:创建一个名为zip_file的文件,并使用getEntry()函数获取zip文件条目。 “file.zip”是f:目录中的一个zip文件,而该压缩文件中不存在“file4.cpp”。

// Java program to demonstrate the

// use of getEntry() function

import java.util.zip.*;

import java.util.Enumeration;

public class solution {

public static void main(String args[])

{

try {

// Create a Zip File

ZipFile zip_file

= new ZipFile("f:\\file.zip");

// get the Zip Entry using

// the getEntry() function

ZipEntry entry

= zip_file.getEntry("file4.cpp");

// display the Zip Entry

System.out.println("Name:"

+ entry.getName());

}

catch (Exception e) {

System.out.println(e.getMessage());

}

}

}

输出:

null


版权声明:本文为weixin_29323551原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。