簡體   English   中英

如何使用 python 腳本提取多部分的 zip 文件以在 Google Cloud Function 中運行

[英]How to extract zip files with multi parts using python script to run in Google Cloud Function

再會,

我是 python 語言的新手,我被指派構建一個 python 腳本,該腳本將從電子郵件中獲取所有附件文件。 附件文件有一個多磁盤部分(.zip、.z01、.z02 等)。 使用 zipfile 模塊可以輕松提取沒有任何多磁盤部分的普通 zip 文件。 此代碼有效:

password = get_data()
    files = [f for f in os.listdir(source_file_name) if isfile(join(source_file_name, f))]
    for file in files:
        if file.endswith('.zip'):
            file_name_split = file.split("_")
            resource_name = file_name_split[0]
            pswd = password[resource_name]
            if resource_name not in listdir('extracted/'):
                zip_ref = zipfile.ZipFile('attachments/' +file)
                zip_ref.setpassword(pwd = bytes(pswd, 'utf-8'))
                zip_ref.extractall('extracted')
                zip_ref.close()
                os.unlink(source_file_name + file)  

但是我不能在多磁盤文件中使用該代碼,因為 zip 文件模塊不支持帶有附加注釋的 ZIP 文件,或者 Zip 文件模塊上的文檔所說的多磁盤 ZIP 文件。 我在谷歌中搜索替代方案,然后通過使用 python 子進程模塊來使用 7z 應用程序。 這是我的代碼。

zip_exe = '"C:/Program Files/7-Zip/7z.exe"' + ' x ' '"' + tmp_folder + 'sample.zip' '"'
subprocess.Popen(zip_exe, shell = True, cwd = tmp_extracted_folder)

這在我的本地使用我的桌面 Windows 10 沒有任何問題。 但我想知道,我怎樣才能把這個腳本放在谷歌雲 function 上? m not sure that this will work easily by just putting on this script. Do I need to install 7z application on cloud function? I m not sure that this will work easily by just putting on this script. Do I need to install 7z application on cloud function? I m not sure that this will work easily by just putting on this script. Do I need to install 7z application on cloud function? I不確定。 你們知道這是否可行嗎? 我需要幫助,我卡在這個應用程序上了。

如果有另一種解決方法,請告訴我。 先感謝您。

您不能在 Cloud Functions 上安裝軟件,因為它們不允許您在服務器級別進行交互,因為它是一個無服務器平台,僅執行由事件觸發的單一目的功能。

如果您選擇 go 使用 7zip 應用程序的方法,您將必須使用計算引擎為您的應用程序創建一個服務器實例,但是,這會為您的項目產生服務器成本,我認為這不是您的最佳解決方案問題。

你可以做的是使用不同於 ZipFile 的 Python 庫以編程方式解壓縮它們,這將在雲 Function 中運行。我個人建議你使用 pyunpack,因為它是我為 Python 找到的最完整的解壓縮庫。你可以采取在此鏈接中查看它的文檔,其中包含有關如何實現它的說明和示例,您還可以嘗試libarchivelmza (取決於您的 python 版本)。

注意:對於 pyunpack,您還需要導入patool ,這是它用來解壓文件的“引擎”,否則它只會依賴 ZipFile 而無法工作。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM