/*
* 案例 1-9: 解压缩 Zip 文件
* 目标: 掌握使用流的方式解压缩 zip 文件
*/
import java.io.*;
import java.util.zip.*;
public class ScanZipFile {
public int scan(String filename) {
ZipEntry entry = null;
int filecount = 0;
try {
ZipInputStream zin = new ZipInputStream(new FileInputStream(
filename));
System.out.println("开始扫描 " + filename + ":");
System.out.println("-------------------------");
while ((entry = zin.getNextEntry()) != null) {
if (entry.isDirectory() || entry.getName().equals("..//")) {
continue;
}
System.out.println(entry.getName());
filecount++;
zin.closeEntry();
}
zin.close();
return filecount;
} catch (IOException e) {
System.out.println(e);
return -1;
}
}
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("用法: java ScanZipFile filename");
} else {
ScanZipFile zip = new ScanZipFile();
int i = zip.scan(args[0]);
if (i >= 0) {
System.out.println("扫描到了" + i + " 个文件");
} else {
System.out.println("对不起, 不能打开压缩文件");
}
}
}
/*
* 这个程序建立了一个 ZipInputStream 对象, 然后, 用这个对象的 getNextEntry() 方法来 一个个得到
* ZipEntry, 如果 Zipentry 是目录, 则不将它记录文件中, 如果 ZipEntry 是文件, 则将文件计数器 filecount
* 加上1, 并且将此文件名打印到控制台上 利用下面命令, 扫描压缩文件 test.zip 中的文件 java ScanZipFile
* test.zip 它将开始扫描 test.zip 中的内容, 并且将 test.zip 中的文件列出来, 最后将统计的文件数输出
*/
}