How to extract file from zip without maintaining directory structure in Python?

后端 未结 1 853
攒了一身酷
攒了一身酷 2020-12-01 14:17

I\'m trying to extract a specific file from a zip archive using python.

In this case, extract an apk\'s icon from the apk itself.

I am currently using

<
相关标签:
1条回答
  • 2020-12-01 14:49

    You can use zipfile.ZipFile.open:

    import shutil
    import zipfile
    
    with zipfile.ZipFile('/path/to/my_file.apk') as z:
        with z.open('/res/drawable/icon.png') as zf, open('temp/icon.png', 'wb') as f:
            shutil.copyfileobj(zf, f)
    

    Or use zipfile.ZipFile.read:

    import zipfile
    
    with zipfile.ZipFile('/path/to/my_file.apk') as z:
        with open('temp/icon.png', 'wb') as f:
            f.write(z.read('/res/drawable/icon.png'))
    
    0 讨论(0)
提交回复
热议问题