java getentry_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值