【问题标题】:Extracting .app from zip file in Python在 Python 中从 zip 文件中提取 .app
【发布时间】:2013-07-29 13:44:55
【问题描述】:

(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(),但我遇到了同样的问题。

【问题讨论】:

    标签: python macos zip extract .app


    【解决方案1】:

    在我的 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.* 中的一个错误,如发现 here - 来自另一个发布的问题 here

    【讨论】:

    • 认为这可能是一个错误......我想除了更新到 Python 3 之外绝对没有解决方法?
    • 不一定,我相信 osx 版本是 2.7.2,但最新的 2.7 系列是 2.7.5,所以你可以更新到那个版本。哪个最适合您。顺便说一句,请随时将我的回复标记为答案,因为它会在遇到类似问题的其他人中脱颖而出。
    【解决方案2】:

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

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

    os.chmod(path, 0755)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      相关资源
      最近更新 更多