Last active
February 1, 2020 11:59
-
-
Save zhangzhhz/7f39db7ef7f26a67d228747225072154 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def pack_file(file_name, outfile_name, no_of_bytes_to_prefix=100): | |
file_name_encoded = bytes(file_name, encoding='utf-8') | |
file_name_length = len(file_name_encoded) | |
if file_name_length > no_of_bytes_to_prefix: | |
raise Exception('file name after utf-8 encoding is too long') | |
prefixed_bytes = bytearray(no_of_bytes_to_prefix) | |
prefixed_bytes[0:file_name_length] = file_name_encoded | |
chunk_buffer = 4096 | |
with open(file_name, 'rb') as infile, open(outfile_name, 'wb') as outfile: | |
outfile.write(prefixed_bytes) | |
while True: | |
chunk = infile.read(chunk_buffer) | |
if not chunk: | |
break | |
outfile.write(chunk) | |
def unpack_file(file_name, no_of_bytes_prefixed=100): | |
import os.path | |
chunk_buffer = 4096 | |
with open(file_name, 'rb') as infile: | |
prefixed_bytes = infile.read(no_of_bytes_prefixed) | |
idx = prefixed_bytes.find(0) | |
if idx == -1: | |
idx = len(prefixed_bytes) | |
actual_file_name = prefixed_bytes[:idx].decode('utf-8') | |
if os.path.isfile(actual_file_name): | |
return 1, Exception(f'Output file [{actual_file_name}] already exists') | |
if os.path.isdir(actual_file_name): | |
return 2, Exception(f'A directory with name [{actual_file_name}] exists') | |
else: | |
with open(actual_file_name, 'wb') as outfile: | |
while True: | |
chunk = infile.read(chunk_buffer) | |
if not chunk: | |
break | |
outfile.write(chunk) | |
return 0, actual_file_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment