Created
December 20, 2017 14:06
-
-
Save whatalnk/ba4d9c0f39ff436ce1be977cf4f5a6c3 to your computer and use it in GitHub Desktop.
Weathernews Programming Competition
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
import os.path | |
import struct | |
from ctypes import * | |
import io | |
class fileInfo(Structure): | |
_fields_ = ( | |
('filename', c_char * 100), | |
('filesize', c_uint32) | |
) | |
def compress(): | |
UserEncodingFileName = input() | |
DirectoryName = input() | |
N = int(input()) | |
fileinfo = [] | |
for i in range(N): | |
filename, filesize = input().split(" ") | |
fileinfo.append(fileInfo(filename.encode(), int(filesize))) | |
with open(UserEncodingFileName, 'wb') as outputfile: | |
outputfile.write(struct.pack('i', N)) | |
for x in fileinfo: | |
outputfile.write(x) | |
with open(os.path.join(DirectoryName, x.filename.decode('utf-8')), 'rb') as inputfile: | |
data = inputfile.read() | |
outputfile.write(data) | |
def extract(): | |
UserEncodingFileName = input() | |
UnzipDirectoryName = input() | |
with open(UserEncodingFileName, "rb") as inputfile: | |
N = struct.unpack('i', inputfile.read(sizeof(c_int)))[0] | |
for i in range(N): | |
x = fileInfo() | |
inputfile.readinto(x) | |
data = inputfile.read(x.filesize) | |
with open(os.path.join(UnzipDirectoryName, x.filename.decode('utf-8')), 'wb') as outputfile: | |
outputfile.write(data) | |
if __name__ == '__main__': | |
command = input() | |
if command == "encode": | |
compress() | |
elif command == "decode": | |
extract() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment