Reduce memory use

Flush buffers and periodically close and reopen the zipfile to keep memory use under control.