Skip to content

Instantly share code, notes, and snippets.

@zyr17
Created September 27, 2020 08:06
Show Gist options
  • Save zyr17/de76fb458c723cbf564af5c961400aab to your computer and use it in GitHub Desktop.
Save zyr17/de76fb458c723cbf564af5c961400aab to your computer and use it in GitHub Desktop.
check rar add recovery
import os, shutil
rarpath = 'C:\\Progra~1\\WinRAR\\Rar.exe '
def checkbackup(filepath):
assert filepath[-4:] == '.rar'
res = os.popen(rarpath + 'lta "' + filepath + '"').read()
#res = res.split('\n')
#for l in res:
if '恢复记录' in res:
return True
return False
def listallfile(nowfolder):
if nowfolder[-1] != '/':
nowfolder = nowfolder + '/'
res = []
for file in os.listdir(nowfolder):
if os.path.isfile(nowfolder + file):
res.append(nowfolder + file)
else:
for deepf in listallfile(nowfolder + file):
res.append(deepf)
return res
def bakcheck(allfiles):
for file in allfiles:
if file[-8:] == '.rar.bak':
print('check', file)
assert file[:-4] in allfiles
bakres = os.popen('%st "%s"' % (rarpath, file)).readlines()[6:]
nowres = os.popen('%st "%s"' % (rarpath, file[:-4])).readlines()[6:]
bakres.sort()
nowres.sort()
#print(bakres)
#print(nowres)
nowres = nowres[:-1]
assert len(bakres) == len(nowres)
for a, b in zip(bakres, nowres):
a = a.split('\x08')[0]
b = b.split('\x08')[0]
#print([a], a)
#print([b], b)
assert a == b
print('bak check passed')
os.remove(file)
def repack(filename):
tmpfolder = filename + '.tmp/'
tmpfile = filename.split('/')[-1]
cmd = '%sx "%s" "%s"' % (rarpath, filename, tmpfolder)
print(cmd)
os.system(cmd)
cmd = 'cd "%s" && %sa -rr3 "%s" ' % (tmpfolder, rarpath, tmpfile)
for file in os.listdir(tmpfolder):
cmd += '"' + file + '" '
print(cmd)
os.system(cmd)
os.system(('copy "%s" "%s.bak"' % (filename, filename)).replace('/', '\\'))
os.system(('copy "%s" "%s"' % (tmpfolder + tmpfile, filename)).replace('/', '\\'))
shutil.rmtree(tmpfolder)
allfiles = listallfile('./')
#print('\n'.join(allfiles))
for file in allfiles:
if file[-4:] == '.rar':
if not checkbackup(file):
print(file)
repack(file)
allfiles = listallfile('./')
bakcheck(allfiles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment