How to count the number of files in a folder in a JAR?

I spent a little time trying to find a way to count the number of files in a folder in a JAR. I have compiled some code examples that served various purposes for this work. It works great when I run the code through Eclipse, but after exporting to the JAR, it fails and returns 0. In this case, my path to the folder I use is just the "rules /". I would appreciate any advice or samples. Thanks.

public static int countFiles(String folderPath) throws IOException { //Counts the number of files in a specified folder ClassLoader loader = ToolSet.class.getClassLoader(); InputStream is = loader.getResourceAsStream(folderPath); try { byte[] c = new byte[1024]; int count = 0; int readChars = 0; boolean empty = true; while ((readChars = is.read(c)) != -1) { empty = false; for (int i = 0; i < readChars; ++i) { if (c[i] == '\n') { ++count; } } } return (count == 0 && !empty) ? 1 : count; } finally { is.close(); } } 

EDIT: The following does not fit my original question, but thanks to MadProgrammer I was able to reduce my code and get rid of the need to even count files. Hitting the code looks for every file in my JAR that searches for those that end with ".rules", opens the file, looks for the file for a line that matches "searchBox.getText ()", adds the results and moves on to the next ".rules" .

  StringBuilder results = new StringBuilder(); int count = 0; JarFile jf = null; try { String path = ToolSet.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String decodedPath = URLDecoder.decode(path, "UTF-8"); jf = new JarFile(new File(decodedPath)); Enumeration<JarEntry> entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.getName().endsWith(".rules")) { String name = entry.getName(); InputStream in = ToolSet.class.getResourceAsStream(name); InputStreamReader isr = new InputStreamReader(in); BufferedReader bf = new BufferedReader(isr); String line; while ((line = bf.readLine()) != null) { String lowerText = line.toLowerCase(); if(lowerText.indexOf(searchBox.getText().toLowerCase()) > 0) { results.append(line + "\n"); count++; } } bf.close(); } } } catch (IOException ex) { try { jf.close(); } catch (Exception e2) { } } if(count>0) { logBox.setText(results.toString()); } else { logBox.setText("No matches could be found"); } 
+4
source share
2 answers

A Jar file is essentially a Zip file with a manifest.

Jar / Zip files do not actually have the concept of directories such as disks. They just have a list of entries with names. These names may contain some path separator, and some entries can be marked as directories (and usually do not have bytes associated with them, just acting as markers)

If you want to find all the resources in the given path, you will have to open the Jar file and check it yourself, for example ...

 JarFile jf = null; try { String path = "resources"; jf = new JarFile(new File("dist/ResourceFolderCounter.jar")); Enumeration<JarEntry> entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (!entry.isDirectory()) { String name = entry.getName(); name = name.replace(path + "/", ""); if (!name.contains("/")) { System.out.println(name); } } } } catch (IOException ex) { try { jf.close(); } catch (Exception e) { } } 

Now you need to know the name of the Jar file that you want to use, this can be problematic, since you can list resources from several different banners ...

The best solution would be to create some kind of โ€œresource search fileโ€ during the build, in which there would be all the resource names that you might need, perhaps even tied to specific names ...

That way you can just use ...

 BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsInputStream("/resources/MasterResourceList.txt"))); String name = null; while ((name = br.readLine()) != null) { URL url = getClass().getResource(name); } } finally { try { br.close(); } catch (Exception exp) { } } 

For instance...

You can even sow a file with the amount of resources;)

+4
source

this is a simple solution:

 InputStream is = loader.getResourceAsStream(folderPath); //open zip ZipInputStream zip = new ZipInputStream(is); //count number of files while ((zip.getNextEntry()) != null ) { UnzipCounter++; } 
0
source

Source: https://habr.com/ru/post/1501782/


All Articles