0

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.

Mustafa
  • 10,013
  • 10
  • 70
  • 116
  • you can turn off zip's compression .. http://docs.oracle.com/javase/1.4.2/docs/api/java/util/zip/Deflater.html#NO_COMPRESSION – wowest Dec 30 '11 at 20:45

2 Answers2

2

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.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57
  • 1
    But I do not want to package my whole application to single Jar file, I want Game.jar and data.pak files independently, so the data.pak files can be changed without any need to build. – Mustafa Dec 31 '11 at 00:32
  • @Mustafa: read my answer. You can hold the jar (or zip) with assets in any location as long as you have it on your classpath. Any IDE will generate the necessary MANIFEST.MF. – fdreger Jan 01 '12 at 01:33
  • 1
    Thanks but I am asking how to make the zip or other file to be directly readable via a virtual system; I do not want to extract my files each time – Mustafa Jan 01 '12 at 10:50
  • @Mustafa - see my revised answer. – Francis Upton IV Jan 01 '12 at 14:13
2

The usual way is to keep your assets along with .class files (in 99% of cases this means they end up inside the executable jar that you build); this way they can be read by a classloader. This ensures that when you change the way your game is packaged or run from exploded directory, the resources will be found:

package pl.alx.example;

class Whatever {
   public Whatever(){
      // I assume that you keep your image in the same folder as Whatever
      InputStream is = this.getClass().getResourceAsStream("/pl/alx/example/file.png");
      // read the png

   }
}

If for some reason you insist on keeping compiled classes and resources in separate files, you can still use the technique - just add the jar with resources to your classpath (your IDE will do it for you).

fdreger
  • 12,264
  • 1
  • 36
  • 42