1

(Python 2.7)

I have a program that will download a .zip file from a server, containing a .app file which I'd like to run. The .zip downloads fine from the server, and trying to extract it outside of Python works fine. However, when I try to extract the zip from Python, the .app doesn't run - it does not say the file is corrupted or damaged, it simply won't launch. I've tried this with other .app files, and I get the same problem, and was wondering if anyone else has had this problem before and a way to fix it?

The code I'm using:

for a in gArchives:
if (a['fname'].endswith(".build.zip") or a['fname'].endswith(".patch.zip")): 
    #try to extract: if not, delete corrupted zip
    try :
        zip_file = zipfile.ZipFile(a['fname'], 'r')
    except:
        os.remove(a['fname'])
    for files in zip_file.namelist() :
        #deletes local files in the zip that already exist
        if os.path.exists(files) :
            try :
                os.remove(files)
            except:
                print("Cannot remove file")
            try :
                shutil.rmtree(files)
            except:
                print("Cannot remove directory")
        try :
            zip_file.extract(files)
        except:
            print("Extract failed")                        
    zip_file.close()

I've also tried using zip_file.extractall(), and I get the same problem.

Jon Bailey
  • 93
  • 3
  • 9

2 Answers2

0

Testing on my macbook pro, the problem appears to be with the way Python extracts the files.

If you run

diff -r python_extracted_zip normal_extracted_zip

You will come into messages like this:

File Seashore.app/Contents/Frameworks/TIFF.framework/Resources is a directory while file here/Seashore.app/Contents/Frameworks/TIFF.framework/Resources is a regular file

So obviously the issue is with the filenames it's coming across as it's extracting them. You will need to implement some checking of the filenames as you extract them.

EDIT: It appears to be a bug within python 2.7.* as found here - Sourced from another question posted here.

Community
  • 1
  • 1
basica
  • 67
  • 1
  • 2
  • 11
  • Thought it may be a bug...I guess there's absolutely no workaround except updating to Python 3? – Jon Bailey Jul 30 '13 at 07:53
  • not necessarily, the osx version is 2.7.2 I believe but the latest 2.7 series is 2.7.5 so you can update to that instead. Whichever works best for you. BTW, feel free to mark my reply as the answer as it will stand out to others who encounter similar issues. – basica Jul 30 '13 at 11:23
0

Managed to resolve this myself - the problem was not to do with directories not being extracted correctly, but in fact with permissions as eri mentioned above.

When the files were being extracted with Python, the permissions were not being kept as they were inside the .zip, so all executable files were set to be not executable. This problem was resolved with a call to the following on all files I extracted, where 'path' is the path of the file:

os.chmod(path, 0755)
Jon Bailey
  • 93
  • 3
  • 9
  • This did not work for me, but this: https://stackoverflow.com/questions/39296101/python-zipfile-removes-execute-permissions-from-binaries did. – AudioDroid Dec 09 '22 at 10:06