# How can I compress resources into a single file in Java?

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 1 '15 at 13:24
• Yes, it is. I don't want others to modify easily the resources. @KaareZ – user3742637 Aug 1 '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 1 '15 at 13:57
• I've tried it, but you can decompile it and get all the files. – user3742637 Aug 1 '15 at 14:00
• @KaareZ What would stop you from opening the .jar with win rar and viewing the images then ? – dimitris93 Aug 1 '15 at 14:12

Put all of the resources in a ZIP archive.

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

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());

• Perfect solution to my problem. I raccomand it! – user3742637 Oct 2 '15 at 13:59