Advertisement
Shahar_Goldenberg

unzip files program

Oct 7th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. import struct
  2.  
  3. def unzip(zipFile):
  4.     #קורא חתימה ובודק שהיא מתאימה ל-zip
  5.     signature = zipFile.read(4)
  6.     if (signature != b"\x50\x4B\x03\x04"):
  7.         print("Error! not a zip")
  8.         zipFile.close()
  9.         exit()
  10.  
  11.     #בודק שהקובץ לא מקובץ
  12.     compact = zipFile.read(4)
  13.     compact = zipFile.read(2)
  14.     if (compact != b"\x00\x00"):
  15.         print("Error! the zip is compact")
  16.         zipFile.close()
  17.         exit()
  18.  
  19.     #מוציא את אורך התוכן והופך אותו למספר עשרוני
  20.     ContentLen = zipFile.read(8)
  21.     ContentLen = zipFile.read(4)
  22.     contentLen = struct.unpack("<I", ContentLen)[0]
  23.  
  24.     #מוציא את אורך הדרך (המיקום שבוא הקובץ נמצא) והופך אותו למספר
  25.     nameLen = zipFile.read(4)
  26.     nameLen = zipFile.read(2)
  27.     nameLen = struct.unpack("<H", nameLen)[0]
  28.  
  29.     #קורא את הדרך באורך שזיהה לפני
  30.     fileName = zipFile.read(2)
  31.     fileName = zipFile.read(nameLen)
  32.     #מחלק את הדרך לפי / ומוציא את השם שנמצא בסוף
  33.     fileNameStr = fileName.decode().split('/')
  34.     fileNameStr = fileNameStr[1:]
  35.     fileNameStr = '/'.join(fileNameStr)
  36.     print(fileNameStr)
  37.     #יוצר את הקובץ
  38.     newFile = open(fileNameStr, 'wb')
  39.  
  40.     #מוציא את התוכן באורך שזיהה לפני וממלא את הקובץ החדש
  41.     fileContent = zipFile.read(contentLen)
  42.     #if (fileContent.find(b"\x50\x4B\x03\x04") == 1):
  43.      #   fileContent = fileContent.split(b"\x50\x4B\x03\x04")
  44.      #   newFile.write(fileContent)[0]
  45.     #    unzip(fileZip)
  46.      #   newFile.close()
  47.     #    return
  48.     newFile.write(fileContent)
  49.     print(fileNameStr, ":", fileContent)
  50.     print("done with", fileNameStr)
  51.     newFile.close()
  52.  
  53.  
  54. fileZip = open('zip_example.zip', 'rb')
  55. while(True):
  56.     unzip(fileZip)
  57.  
  58. #להוסיף יצירת תיקיות
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement