Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import struct
- def unzip(zipFile):
- #קורא חתימה ובודק שהיא מתאימה ל-zip
- signature = zipFile.read(4)
- if (signature != b"\x50\x4B\x03\x04"):
- print("Error! not a zip")
- zipFile.close()
- exit()
- #בודק שהקובץ לא מקובץ
- compact = zipFile.read(4)
- compact = zipFile.read(2)
- if (compact != b"\x00\x00"):
- print("Error! the zip is compact")
- zipFile.close()
- exit()
- #מוציא את אורך התוכן והופך אותו למספר עשרוני
- ContentLen = zipFile.read(8)
- ContentLen = zipFile.read(4)
- contentLen = struct.unpack("<I", ContentLen)[0]
- #מוציא את אורך הדרך (המיקום שבוא הקובץ נמצא) והופך אותו למספר
- nameLen = zipFile.read(4)
- nameLen = zipFile.read(2)
- nameLen = struct.unpack("<H", nameLen)[0]
- #קורא את הדרך באורך שזיהה לפני
- fileName = zipFile.read(2)
- fileName = zipFile.read(nameLen)
- #מחלק את הדרך לפי / ומוציא את השם שנמצא בסוף
- fileNameStr = fileName.decode().split('/')
- fileNameStr = fileNameStr[1:]
- fileNameStr = '/'.join(fileNameStr)
- print(fileNameStr)
- #יוצר את הקובץ
- newFile = open(fileNameStr, 'wb')
- #מוציא את התוכן באורך שזיהה לפני וממלא את הקובץ החדש
- fileContent = zipFile.read(contentLen)
- #if (fileContent.find(b"\x50\x4B\x03\x04") == 1):
- # fileContent = fileContent.split(b"\x50\x4B\x03\x04")
- # newFile.write(fileContent)[0]
- # unzip(fileZip)
- # newFile.close()
- # return
- newFile.write(fileContent)
- print(fileNameStr, ":", fileContent)
- print("done with", fileNameStr)
- newFile.close()
- fileZip = open('zip_example.zip', 'rb')
- while(True):
- unzip(fileZip)
- #להוסיף יצירת תיקיות
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement