# Minecraft chunk file decompresssion

First of all, hello everyone,

I'm currently making a program to read Minecraft region files (r.#.#.mca) for a server.
The problem I have is that I can't decompress them, I tried GZip and ZLib decompression but none of them worked. I already searched for solutions but there is little to no information online. It's really important that I find a solution to that issue.

• The docs say Zlib is what's used, so it sounds like you're on the right track. Can you give more details of what exactly you've tried (step by step) and in what particular way these approaches failed to work? – DMGregory Aug 30 '16 at 11:26
• Well, I first used the jnbt library as it provides methods to read NBT files but it expects gzipped files. When I tried to read region files it crashed and told me that the files weren't in GZip format. So, I then tried to use the Inflater class from the Java zip API because it uses the zlib algorithm. But this time it tells me "unknown compression method". That's where I am right now. – Darmo Aug 30 '16 at 14:28
• If I'm reading the docs correctly, it sounds like each block of "chunk" data in the file is Zlib compressed individually, not the .mca file as a whole. So you're going to need to start by parsing the chunk information tables to slice out each block of the file you want to decompress. Have you done that yet? – DMGregory Aug 30 '16 at 14:37
• Well, I didn't see that bit, I'm gonna try and see what I can do. – Darmo Aug 30 '16 at 14:39
• So, I tried my best but still not working. Out of all the files I've tested, none seemed to follow the format from the wiki page (MCRegion format). They say it has not changed since, but it seems it did anyway. I didn't find any information about the precise structure of .mca files. – Darmo Aug 30 '16 at 15:33

So I found the AnvilChunkLoader class, copied its loadChunk__Async method and it worked.
This class can be found at net.minecraft.world.chunk.storage.AnvilChunkLoader for Minecraft 1.8.9
You have to find the method loadChunk__Async(World worldIn, int x, int z).
The part I needed was:

DataInputStream datainputstream = RegionFileCache.getChunkInputStream(chunkSaveLocation, x, z);
if (datainputstream == null) {
return null;
}

To make it work, you need to specify the path to the region folder that contains the file, then the chunk coordinates x and z. The tag variable should contain the chunk data in NBT format.