3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – KaareZ Aug 1 '15 at 13:24
  • \$\begingroup\$ Yes, it is. I don't want others to modify easily the resources. @KaareZ \$\endgroup\$ – user3742637 Aug 1 '15 at 13:44
  • \$\begingroup\$ Then I don't know much about it. You could also try to put your resources inside the jar? \$\endgroup\$ – KaareZ Aug 1 '15 at 13:57
  • \$\begingroup\$ I've tried it, but you can decompile it and get all the files. \$\endgroup\$ – user3742637 Aug 1 '15 at 14:00
  • \$\begingroup\$ @KaareZ What would stop you from opening the .jar with win rar and viewing the images then ? \$\endgroup\$ – dimitris93 Aug 1 '15 at 14:12
2
\$\begingroup\$

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());
\$\endgroup\$
  • \$\begingroup\$ Perfect solution to my problem. I raccomand it! \$\endgroup\$ – user3742637 Oct 2 '15 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.