I can't understand why there are two blocks here (check full file).  One for if the suffix is kmz and another if it is not...  shouldn't both just write to the requested file (provided it is an os-valid filename)