如何相应地检查输出是 zip 还是 tar.gz 和 unzip/untar?

How to check if the output is zip or tar.gz and unzip/untar accordingly?

我试图在使用 if else 语句从输出中读取后解压缩或解压缩文件,但它对我不起作用。

这是我的代码:

import tarfile
import zipfile

download_url = https:github.com/sergi/go-diff/archive/refs/tags/v1.2.0.zip

target_path = primresult['download_url'].split("/")[-1]
#will give me output as v1.2.0.tar.gz or v1.2.0.zip depending on the download_url
print(target_path) 

#Prim query
primQueryURL = requests.get('http://example.com?query={"username":"xxxxx","token":"xxxxxx","facility":"COMPONENT_QUERY", "prim":"'+ primNumber +'"}', timeout = 10,  headers={"Content-Type":"application/json"})
primresult = json.loads(primQueryURL.text)
print(primresult['source_code_url'])

#download the software
url = primresult['source_code_url']
download_url = primresult['download_url']
print(primresult['download_url'].split("/")[-1])
resp = requests.get(url, stream=True, auth=HTTPBasicAuth('xxxx', 'xxxxx'))

if resp.status_code == 200:
    with open(target_path, 'wb') as f:
        f.write(resp.raw.read())

target_path = primresult['download_url'].split("/")[-1]

def process_archive(download_url):
    if download_url.endswith('.zip'):
        # process zip
        target_path = download_url.split("/")[-1]
        zip_ref = zipfile.ZipFile(target_path)
        zip_ref.extractall()
        zip_ref.close()
    elif download_url.endswith('.tar.gz'):
        # process tar.gz
        target_path = download_url.split("/")[-1]
        tar = tarfile.open(target_path)
        tar.extractall()
        tar.close()
process_archive('download_url')

任何人都可以帮助阅读 target_path 如果它是 zip,解压它,否则检查它是否是 tar.gz 并解压它。

import tarfile

def process_archive(download_url):
    if download_url.endswith('.zip'):
        # process zip
        target_path = download_url.split("/")[-1]
        print('download url: ' + download_url)
        print('filename: ' + target_path)
    elif download_url.endswith('.tar.gz'):
        # process tar.gz
        target_path = download_url.split("/")[-1]
        tar = tarfile.open(target_path)
        tar.extractall()
        tar.close()


process_archive('https://github.com/sergi/go-diff/archive/refs/tags/v1.2.0.zip')  

输出:

download url: https://github.com/sergi/go-diff/archive/refs/tags/v1.2.0.zip
filename: v1.2.0.zip

请尝试以下方法。 必须将文件下载到临时 location.Then 检查文件扩展名并将文件解压缩到服务器上的某个位置。

import tarfile
import zipfile
import urllib.request
download_url = "https://github.com/sergi/go-diff/archive/refs/tags/v1.2.0.zip"
target_path = download_url.split("/")[-1]
print(target_path)
staging="D:\Learning\staging\"
try:
    urllib.request.urlretrieve(download_url, staging+target_path)
    if '.zip' in target_path:
        # process zip
        zip_ref = zipfile.ZipFile(staging+target_path)
        zip_ref.extractall(staging)
        zip_ref.close()
    elif 'tar.gz' in target_path:
        print('yo1')
        # process tar.gz
        tar = tarfile.open(staging+target_path)
        tar.extractall(staging)
        tar.close()
except Exception as e: print(e)