java 读rar文件_Java读取压缩文件信息

该博客展示了如何使用Java的ZipFile类来读取RAR文件的内容,包括统计文件和目录的数量。此外,它还演示了如何检查文件名中是否存在非法字符串,并列出可能的非法文件及其大小。
摘要由CSDN通过智能技术生成

import java.util.zip.ZipEntry;

import java.util.zip.ZipException;

import java.util.zip.ZipFile;

private static voidreadZip(String fileName) {

ZipFile zf= null;try{

zf= newZipFile(fileName);

HashSet set = new HashSet();int fileCount = 0;int dirCount = 0;for (Enumeration entries = (Enumeration) zf.entries(); entries.hasMoreElements();) {

ZipEntry ze=entries.nextElement();if(ze.isDirectory()) {

dirCount++;

}else{

fileCount++;

String name=ze.getName();for(String s : illegalString) {if(name.toUpperCase().contains(s)) {

set.add(ze);break;

}

}

}

}

System.out.println("The package contains " + fileCount + " files and " + dirCount + " directories.\n");if (set.size() <= 0) {

System.out.println("PASS: ILLEGAL FILE NOT FOUND.");

}else{

System.out.println("Possible illegal files: \n");int index = 1;

System.out.format(" %-10s%-80s%-20s %n%n", "NO.", "FileName", "Bytes");for(ZipEntry e : set) {

System.out.format(" %-10s%-80s%-20s %n%n", index, e.getName(), e.getSize());

index++;

}

}

}catch(ZipException e) {

System.out.println("INFO: The file format is not a common zip.");

}catch(IOException e) {

System.out.println("WARNING: IOException occured.");

}catch(SecurityException e) {

System.out.println("WARNING: The file is not accessible.");

}catch(NullPointerException e) {

System.out.println("WARNING: NullPointerException. May due to ZipEntry has no Size.");

}finally{if (zf != null) {try{

zf.close();

}catch(IOException e) {

System.out.println("WARNING: IOException occured when close file.");

}

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值