Last active
December 28, 2016 09:19
-
-
Save clichedmoog/dcc9dd229137a635b81fb6dd90ca4b8e to your computer and use it in GitHub Desktop.
merge two partial files in binary mode like uncompleted torrent download.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys,getopt | |
def str_or(s1,s2): | |
# convert strings to a list of character pair tuples | |
# go through each tuple, converting them to ASCII code (ord) | |
# perform or on the ASCII code | |
# then convert the result back to ASCII (chr) | |
# merge the resulting array of characters as a string | |
return ''.join(chr(ord(a) | ord(b)) for a,b in zip(s1,s2)) | |
filename1 = None | |
filename2 = None | |
filename_out = None | |
blocksize = 1024 | |
opts,args = getopt.getopt(sys.argv[1:],'a:b:o:') | |
for o,a in opts: | |
if o == '-a': | |
filename1 = a | |
if o == '-b': | |
filename2 = a | |
if o == '-o': | |
filename_out = a | |
if not filename1 or not filename2: | |
print '-a file -b file -o outfile' | |
exit() | |
offset = 0 | |
f1 = open(filename1, 'rb') | |
f2 = open(filename2, 'rb') | |
fout = None | |
if filename_out: | |
fout = open(filename_out, 'wb') | |
while True: | |
block1 = f1.read(blocksize) | |
block2 = f2.read(blocksize) | |
if block1 == '' and block2 == '': | |
break | |
if block1 != block2: | |
print block1.encode('hex') | |
print '-'*80 | |
print block2.encode('hex') | |
block = str_or(block1, block2) | |
else: | |
block = block1 | |
if fout: | |
fout.write(block) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment