docs: def write_blockdata(self, x, z, data, compression=COMPRESSION_ZLIB):
          """
          Compress the data, write it to file, and add pointers in the header so it 
          can be found as chunk(x,z).
          """
          if compression == COMPRESSION_GZIP:
              # Python 3.1 and earlier do not yet support `data = gzip.compress(data)`.
              compressed_file = BytesIO()
              f = gzip.GzipFile(fileobj=compressed_file)
              f.write(data)
              f.close()
              compressed_file.seek(0)
              data = compressed_file.read()
              del compressed_file
          elif compression == COMPRESSION_ZLIB:
              data = zlib.compress(data) # use zlib compression, rather than Gzip
          elif compression != COMPRESSION_NONE:
              raise ValueError("Unknown compression type %d" % compression)
          length = len(data)
  
          # 5 extra bytes are required for the chunk block header
          nsectors = self._bytes_to_sector(length + 5)
  
          if nsectors >= 256:
              raise ChunkDataError("Chunk is too large (%d sectors exceeds 255 maximum)" % (nsectors))
  
          # Ensure file has a header
          if self.size < 2*SECTOR_LENGTH:
              self._init_file()
  
          # search for a place where to write the chunk:
          current = self.metadata[x, z]
          free_sectors = self._locate_free_sectors(ignore_chunk=current)
          sector = self._find_free_location(free_sectors, nsectors, preferred=current.blockstart)
  
          # If file is smaller than sector*SECTOR_LENGTH (it was truncated), pad it with zeroes.
          if self.size < sector*SECTOR_LENGTH:
              # jump to end of file
              self.file.seek(0, SEEK_END)
              self.file.write((sector*SECTOR_LENGTH - self.size) * b"\x00")
              assert self.file.tell() == sector*SECTOR_LENGTH
  
          # write out chunk to region
          self.file.seek(sector*SECTOR_LENGTH)
          self.file.write(pack(">I", length + 1)) #length field
          self.file.write(pack(">B", compression)) #compression field
          self.file.write(data) #compressed data
  
          # Write zeros up to the end of the chunk
          remaining_length = SECTOR_LENGTH * nsectors - length - 5
          self.file.write(remaining_length * b"\x00")
  
          #seek to header record and write offset and length records
          self.file.seek(4 * (x + 32*z))
          self.file.write(pack(">IB", sector, nsectors)[1:])
  
          #write timestamp
          self.file.seek(SECTOR_LENGTH + 4 * (x + 32*z))
          timestamp = int(time.time())
          self.file.write(pack(">I", timestamp))
  
          # Update free_sectors with newly written block
          # This is required for calculating file truncation and zeroing freed blocks.
          free_sectors.extend((sector + nsectors - len(free_sectors)) * [True])
          for s in range(sector, sector + nsectors):
              free_sectors[s] = False
          
          # Check if file should be truncated:
          truncate_count = list(reversed(free_sectors)).index(False)
          if truncate_count > 0:
              self.size = SECTOR_LENGTH * (len(free_sectors) - truncate_count)
              self.file.truncate(self.size)
              free_sectors = free_sectors[:-truncate_count]
          
          # Calculate freed sectors
          for s in range(current.blockstart, min(current.blockstart + current.blocklength, len(free_sectors))):
              if free_sectors[s]:
                  # zero sector s
                  self.file.seek(SECTOR_LENGTH*s)
                  self.file.write(SECTOR_LENGTH*b'\x00')
          
          # update file size and header information
          self.size = max((sector + nsectors)*SECTOR_LENGTH, self.size)
          assert self.get_size() == self.size
          current.blockstart = sector
          current.blocklength = nsectors
          current.status = STATUS_CHUNK_OK
          current.timestamp = timestamp
          current.length = length + 1
          current.compression = COMPRESSION_ZLIB