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; }
