code: def zip_dir(directory):
      
      result = io.BytesIO()
      dlen = len(directory)
      with ZipFile(result, "w") as zf:
          for root, dirs, files in os.walk(directory):
              for name in files:
                  full = os.path.join(root, name)
                  rel = root[dlen:]
                  dest = os.path.join(rel, name)
                  zf.write(full, dest)
      return result
docs: zip a directory tree into a BytesIO object