题目描述
现有一个zip格式的压缩文件,文件名为dict.dic。它里边压缩了若干个文件(少于20个),没有目录。请写一段程序将这些文件的文件名读出来。
输入
为一个整数n(0<n<20),代表要输出压缩文件中第n个条目的文件名。比如压缩文件共压缩了3个文件,依次为a.txt,b.txt,c.txt。那么当输入为1时输出a.txt,输入为2时输出b.txt,输入为3时输出c.txt。测试用例保证输入合法。
输出
只有一行字符串,为对应的文件名。
样例输入 Copy
1
样例输出 Copy
a.txt
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int fileIndex = sc.nextInt();
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream("dict.dic"))) {
ZipEntry entry;
int currentIndex = 0;
while ((entry = zipInputStream.getNextEntry()) != null) {
currentIndex++;
if (currentIndex == fileIndex) {
String fileName = entry.getName();
System.out.println(fileName);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java程序读取zip压缩文件内文件名
该程序使用Java的ZipInputStream从dict.dic压缩文件中读取指定索引的文件名。通过输入整数n,程序可以输出第n个条目的文件名。在示例中,当输入为1时,输出a.txt。
386

被折叠的 条评论
为什么被折叠?



