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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值