Bunch of fixes to export.py. increase reliability.

Before, we'd occasionally have null bytes in the output stream, causing
XML parsing to die, ultimately losing pages and lots of versions.