Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- import subprocess as sp
- import os
- import re
- import time
- import tempfile
- import sys
- class unrar:
- def __init__(self, rar, pwd):
- self.rar = rar
- self.pwd = pwd and "-p%s" % pwd or ""
- #self.tmpdir = tempfile.mkdtemp()
- self.tmpdir = "./tmp"
- def mkdir(self, file):
- dir = os.path.dirname(file)
- if dir and not os.path.isdir(dir):
- os.makedirs(dir)
- def merge(self, fnin, fnout):
- self.mkdir(fnout)
- size = os.path.exists(fnout) and os.path.getsize(fnout)
- with open(fnin) as fin:
- with open(fnout, 'a') as fout:
- fin.seek(size)
- buf = fin.read(512*1024)
- tot = 0
- while buf:
- fout.write(buf)
- tot += len(buf)
- buf = fin.read(512*1024)
- print("Merged %d kB to %s (current size %d kB)" % (tot/1024, fnout, (size+tot)/1024))
- def list(self):
- return sp.Popen(['nice', 'unrar', 'vb', self.pwd, self.rar], stdout=sp.PIPE).communicate()[0].splitlines()
- def extract(self, f):
- fn = os.path.join(self.tmpdir, f)
- while sp.Popen(['nice', 'unrar', 'x', '-kb', '-o+', self.pwd, self.rar, f, self.tmpdir], stderr=sp.PIPE, stdout=sp.PIPE).communicate()[1]:
- self.merge(fn, f)
- time.sleep(60)
- self.merge(fn, f)
- os.unlink(fn)
- def extract_all(self):
- for f in self.list(): self.extract(f)
- def main(params):
- if len(params)<2:
- print("usage: %s <archive> [<password>]" % params[0])
- return 1
- archive = params[1]
- try:
- pwd = params[2]
- except IndexError:
- pwd = ""
- unrar(archive, pwd).extract_all()
- return 0
- if __name__ == "__main__":
- sys.exit(main(sys.argv))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement