private static byte[] Compress(byte[] zlib) { // Create a GZIP stream with decompression mode. // ... Then create a buffer and write into while reading from the GZIP stream. using (MemoryStream memory = new MemoryStream()) { var def = new Deflater(); def.setInput(gzip); def.finish(); var decompressStream = new MemoryStream(); int i = 0; var buffer = new byte[1024 * 1024]; while ((i = def.deflate(buffer)) > 0) { decompressStream.Write(buffer, 0, i); } return decompressStream.ToArray(); } //using (GZipStream stream = new GZipStream(memory, CompressionMode.Compress)) //{ // stream.Write(gzip, 0, gzip.Length); // stream.Flush(); // stream.Close(); // return memory.ToArray(); //} }
public override void Write() { int o = 16 * 16 * 128; byte[] data = new byte[o * 5 / 2]; int i = 0; Chunk.Types.CopyTo(data, i); i += Chunk.Types.Length; Chunk.Data.Data.CopyTo(data, i); i += Chunk.Data.Data.Length; Chunk.Light.Data.CopyTo(data, i); i += Chunk.Light.Data.Length; Chunk.SkyLight.Data.CopyTo(data, i); byte[] comp = new byte[o * 5 / 2]; int len; Deflater deflater = new Deflater(-1); try { deflater.setInput(data); deflater.finish(); len = deflater.deflate(comp); } finally { deflater.end(); } SetCapacity(18 + len); Writer.Write(Coords.WorldX); Writer.Write((short)Coords.WorldY); Writer.Write(Coords.WorldZ); Writer.Write(SizeX); Writer.Write(SizeY); Writer.Write(SizeZ); Writer.Write(len); Writer.Write(comp, 0, len); }
public override void Write(BigEndianStream stream) { stream.Write(X); stream.Write(Y); stream.Write(Z); stream.Write(SizeX); stream.Write(SizeY); stream.Write(SizeZ); int o = 16 * 16 * 128; byte[] data = new byte[o * 5 / 2]; int i = 0; for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { for (int y = 0; y < 128; y++) { int s = ((i + 1) & 1) * 4; int ofst = i; data[ofst] = Chunk[x, y, z]; ofst = i / 2 + o * 2 / 2; data[ofst] = unchecked((byte)(data[ofst] | (Chunk.GetData(x, y, z) << s))); ofst = i / 2 + o * 3 / 2; data[ofst] = unchecked((byte)(data[ofst] | (Chunk.GetBlockLight(x, y, z) << s))); ofst = i / 2 + o * 4 / 2; data[ofst] = unchecked((byte)(data[ofst] | (Chunk.GetSkyLight(x, y, z) << s))); i++; } } } byte[] comp = new byte[o * 5]; int len; Deflater deflater = new Deflater(0); try { deflater.setInput(data); deflater.finish(); len = deflater.deflate(comp); } finally { deflater.end(); } stream.Write(len); stream.Write(comp, 0, len); }