Search code examples
javapackagingassets

How to Handle Assets in Single File within a Virtual File System in Java


I am developing a small game with Java and all my files are bundled within a folder called data and my application reads them. However, I want to pack these files into a single file, .pak, .zip, etc and use it when distrubiting. My game is planned to work for different packages, so having a single file instead of a folder containing many files are more preferabble. I have looked into Zip compressing/decompressing, but I just want to create a virtual file system to read them, no compression or protection is needed.

What do you propose? Is there any virtualization library available? It is also important for my packages to be created easily.


Solution

  • I would put your data files in the same directories as your classes and then you can read them using the classpath (i.e., Class.getResourceAsStream()), this way you can package up your application including all of the data into a single Jar file.

    If you are using Maven, you would put it in the resources section.

    If you wish to distribute them separately then you can do essentially the same thing, just distribute your application in two Jar files, one for the code and one for the data. Put them both on the classpath and you can access your data through the means I just described. You can then release and build the data portion of your application separately.