Java listFiles in directory in jar - java

Is there any way to use listFiles() on a directory that's been packaged into a jar?
Let's say I have a directory in my resource directory with some text files: texts/text1.txt and texts/text2.txt.
And within this Java program I have a class that needs to use listFiles() to get a list of those files. I'll get something like jar:file:/home/soupkitchen.jar/!text. I'd expect that to be a directory. Is there any way to be able to treat it as a java.io.File directory containing files? Right now it seems to only be listed as neither a file nor directory.

No. java.io.File can only be used to list real directories.
However, you can treat it as a java.nio.file.Path.
Overall, you have three options:
Open the .jar as a Zip File System and use Files.newDirectoryStream or Files.list.
Iterate through all entries in the .jar file, looking for names that match.
Put a text file in your .jar that contains the names of all the entries in the directory, so you don't have to try to list them.

Related

Adding empty directories to .tar file in Java using JTar

I'm trying to package a directory, which includes an empty sub-directory, into a .tar file using Java's JTar. Folders that contain files are automatically included in the .tar file, but the empty folders are not. I want to preserve the directory structure below, is this possible?
MyTarFile.tar--|
|--ChildFolder1--|
|--MyFile.txt
|--ChildFolder2--|
ChildFolder1 is created, ChildFolder2 is not.

How to iterate through each xml file in root of a jar

during runtime of my app I need to iterate through each xml file which lies in the root of my jar.
I know that I can access a concrete file like this
InputStream in = this.getClass().getClassLoader().getResourceAsStream( "filename.xml" );
But how can I receive the list of files which are in the root of the jar?
Thanks
Try this.
Java: Listing the contents of a resource directory
The ClassLoader.getResource() function can be a really handy way to load up your files in Java. The files can be loaded from any folder or JAR file on your classpath. However, the API disappointingly lacks a way to list all the files in the directory. (No, getResources() does not do it.) This utility function comes to the rescue!

Get contents of a directory in a ZIP file in Java

I have some default configuration files inside my application jar that I would like to save to the file system if they don't already exist. I would like it to keep the directory structure too. Example:
Jar file
-configs/
-main-config.cfg
-another-file.txt
-stuff/
-another-file.cfg
-com/
-META-INF/
I would like the contents of configs/ to be mirrored to the file system, including the subfolder.
Use JarFile.entries to get an enumeration of all of the entries in your Jar file.

Unzipping without first directory in java

I would like a zip file Test.zip containing 2 folders, say, A and B, to be unzipped outside of Test folder.
For now A and B are unzipped within Test folder i.e Test->A and Test->B, whereas I want it in a different folder like Test2->A and Test2->B. right now I am getting an output like Test2->Test->A.
How can i achieve this? Please help.
It sounds to me like your Test.zip file simply contains a folder named "Test" that in turn contains A and B. Could you verify if this is the case?
If that's so, maybe you could detect if the zip file contains a single directory with the same name as the file. If that is so, extract from that subdirectory into your target. If not, extract directly from the zip root.

Get path of a file from a subdirectory of a directory in CLASSPATH

Given the following directory structure:
working-directory
subfolder1
file1.wav
file2.wav
file3.wav
subfolder2
file4.wav
file5.wav
file6.wav
subfolder3
file7.wav
file8.wav
file9.wav
jar-that-im-running.jar
I need to get the path to each wav file. I figured that since the folder is in the working directory, and is thus part of the classpath (if my assumption is wrong, I'd just add the working directory to the classpath), I could just run:
String path = ClassLoader.getSystemResource("file1.wav");
or
String path = ClassLoader.getSystemResource("/file1.wav");
but it wouldn't work, unless I specified the folder the wave file was in. This would be fine, but I wouldn't know what folder each wave file is in; I only know their names. I'm going to process all the files one way or another, but the order that I do depends on a config file. Also, I am not going to edit these files directly. Instead, I'm going to be passing them off as arguments to a ProcessBuilder. Since some of the directories in the path may have spaces, which get converted to %20 in URLs, I figured I could convert them with path.replaceAll("%20", " "). Will I be better off using files, or is there a way to get a specific wav file, without knowing its parent folder.
Did you try getting from the class loader as a system resource? Here's a snippet of code to illustrate:
String path = ClassLoader.getSystemResource("subfolder1/file1.wav");

Categories