3

Today I've finished my game, but there's a problem. There're more than 500+ images in my resources folder and I want to compress them into a single file, like other games do (for example put everything in a .pak file). I've searched on the internet, but I can't find something that tells about how to do it with Java.

There's maybe a library that I can use to avoid that problem?

Thanks.

  • Is this for deployment purposes? Will the files be uncompressed when the game is installed, or will the game load the files directly from the compressed package? – KaareZ Aug 01 '15 at 13:24
  • Yes, it is. I don't want others to modify easily the resources. @KaareZ – user3742637 Aug 01 '15 at 13:44
  • Then I don't know much about it. You could also try to put your resources inside the jar? – KaareZ Aug 01 '15 at 13:57
  • I've tried it, but you can decompile it and get all the files. – user3742637 Aug 01 '15 at 14:00
  • @KaareZ What would stop you from opening the .jar with win rar and viewing the images then ? – dimitris93 Aug 01 '15 at 14:12
  • +@Shiro I didn't thought about it. – KaareZ Aug 01 '15 at 14:21
  • Try having a look at JarProtector (http://www.bfa-it.com/?lang=en&id=products/jarprotector) – Maxx Aug 05 '15 at 07:41
  • see also http://gamedev.stackexchange.com/questions/37648/how-can-you-put-all-images-from-a-game-to-1-file for some ideas – Zavael Aug 08 '15 at 10:54
  • To stop others from modifying the contents of files that you load, you calculate use MD5 or SHA-256 checksums of these files and check that they match values that you have hard coded in. – Lucien Sep 12 '15 at 07:58

1 Answers1

2

Put all of the resources in a ZIP archive.

Rename the file from example.zip to example.pak (or whatever else you want).

Lastly, uncompress the archive and load all of your resources.

ZipFile zipFile = new ZipFile("/external/path/to/test.zip");

Enumeration<? extends ZipEntry> entries = zipFile.entries();

while(entries.hasMoreElements()){
    ZipEntry entry = entries.nextElement();
    InputStream stream = zipFile.getInputStream(entry);
}

You can also read an archive from inside of your JAR file.

URL url = new URL("jar:file:/path/of/file.jar!/resources.pak");

The location of the running JAR can be easily retrieved (as long as the class that this is called from has been loaded from that JAR).

new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
Lucien
  • 1,176
  • 2
  • 10
  • 36