0

(Python 2.7)

我有一个程序可以从服务器下载一个 .zip 文件,其中包含一个我想运行的 .app 文件。.zip 从服务器下载很好,尝试在 Python 之外提取它可以正常工作。但是,当我尝试从 Python 中提取 zip 文件时,.app 没有运行 - 它没有说文件已损坏或损坏,它根本不会启动。我已经用其他 .app 文件尝试过这个,我遇到了同样的问题,想知道是否有其他人以前遇到过这个问题以及解决它的方法?

我正在使用的代码:

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()

我也尝试过使用 zip_file.extractall(),但我遇到了同样的问题。

4

2 回答 2

0

在我的 macbook pro 上进行测试,问题似乎与 Python 提取文件的方式有关。

如果你跑

diff -r python_extracted_zip normal_extracted_zip

你会收到这样的消息:

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

所以很明显,问题在于它在提取文件时遇到的文件名。在提取文件名时,您需要对文件名进行一些检查。

编辑:这似乎是 python 2.7.* 中的一个错误,如在此处找到-源自此处发布的另一个问题。

于 2013-07-29T16:55:13.813 回答
0

设法自己解决了这个问题 - 问题与没有正确提取目录无关,但实际上与上面提到的 eri 权限有关。

当使用 Python 提取文件时,权限并没有保留在 .zip 中,因此所有可执行文件都设置为不可执行。通过对我提取的所有文件调用以下命令解决了这个问题,其中“路径”是文件的路径:

os.chmod(path, 0755)
于 2013-09-09T12:30:18.310 回答