Some fixes to dump_write now that the basics are working.

* Detect the final write and flush zlib's output buffer before returning
  in this case.
* Do bounds checking before writing to media.