java自带了java.util.zip工具可以实现在不解压zip压缩包的情况下读取包内文件的文件名:(注:只能是ZIP格式的,rar我试了不行)代码如下:
public static String readZipFile(String path, String str) throws IOException {
ZipEntry zipEntry = null;
File file = new File(path);
if(file.exists()){ //判断文件是否存在
ZipInputStream zipInputStream = new ZipInputStream( new FileInputStream(path), Charset.forName("GBK")); //解决包内文件存在中文时的中文乱码问题
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if(zipEntry.isDirectory()){ //遇到文件夹就跳过
continue;
}else{ str+=";"+zipEntry.getName().substring(zipEntry.getName().lastIndexOf("/")+1);
// System.out.println(zipEntry.getName().substring(zipEntry.getName().lastIndexOf("/")+1));//通过getName()可以得到文件名称 } }
} return str; }