memtar.py 649 B

12345678910111213141516171819202122232425262728
  1. import os
  2. import io
  3. import tarfile
  4. def create_tar(path):
  5. fileobj = io.BytesIO()
  6. tar = tarfile.open(mode='w:gz', fileobj=fileobj)
  7. for root, dirs, files in os.walk(path):
  8. for fn in files:
  9. p = os.path.join(root, fn)
  10. # remove one more character to remove trailing slash
  11. arcname = p[p.find(path)+len(path)+1:]
  12. if not arcname.startswith('.nova/config'):
  13. tar.add(p, arcname=arcname)
  14. tar.close()
  15. return fileobj
  16. def extract_tar(fileobj, path):
  17. fileobj.seek(0)
  18. tar = tarfile.open(mode='r:gz', fileobj=fileobj)
  19. tar.extractall(path)
  20. tar.close()