How can I compress an NBT with gzip?

Viewed 3571

I want to get a NBT file, decompress it, modify it, and then re-compress it. The only problem is re-compressing. Unlike just using GZipStream and calling it a day, Minecraft (and standalone NBT editors) can't read it.

public static void PatchNBT()
{
    var data = File.ReadAllBytes(@"level.temp"); // Reads all bytes of the uncompressed NBT
    var GetDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "level.dat");
    Console.WriteLine(GetDirectory);
    Console.ReadKey();
    if (File.Exists("level.dat"))
    {
        File.Move("level.dat", "level.bak");
    }
    Task.WaitAll();

    File.WriteAllBytes(GetDirectory, CompressGZip(data));
}


static byte[] CompressGZip(byte[] raw)
{
    using (MemoryStream memory = new MemoryStream())
    {
        using (GZipStream gzip = new GZipStream(memory, CompressionLevel.Optimal))
        {
            gzip.Write(raw, 0, raw.Length);
        }
        return memory.ToArray();
    }
}
0 Answers
Related