unzipper.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import zipfile,os.path
  2. import subprocess
  3. def unar(source,dest_dir):
  4. if source.lower().endswith("zip"):
  5. return unzip(source,dest_dir)
  6. if source.lower().endswith("rar"):
  7. return unrar(source,dest_dir)
  8. print(" KQHJKWQHRKJQHEWRKJHEWK?????")
  9. return []
  10. def unzip(source_filename, dest_dir):
  11. out=[]
  12. with zipfile.ZipFile(source_filename) as zf:
  13. for member in zf.infolist():
  14. # Path traversal defense copied from
  15. # http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
  16. words = member.filename.split('/')
  17. path = dest_dir
  18. for word in words[:-1]:
  19. drive, word = os.path.splitdrive(word)
  20. head, word = os.path.split(word)
  21. if word in (os.curdir, os.pardir, ''): continue
  22. path = os.path.join(path, word)
  23. out.append(os.path.join(path,member.filename.split('/')[-1]))
  24. zf.extract(member, path)
  25. return out
  26. def unrar(source, dest_dir):
  27. out=[]
  28. with subprocess.Popen(["lsar",source], stdout=subprocess.PIPE) as proc:
  29. out=proc.stdout.read().decode('utf-8').split("\n")
  30. if out[0].endswith(": RAR"):
  31. del out[0] #header
  32. if len(out[-1])==0:
  33. del out[-1]
  34. #print(out)
  35. subprocess.run(["unar","-f","-o",dest_dir,source],stdout=subprocess.DEVNULL)
  36. return [os.path.join(dest_dir,file) for file in out ]