'File permissions lost after unzipping with zipfile

I have extracted a zip file (hisat2-2.2.0-Linux_x86_64.zip from https://cloud.biohpc.swmed.edu/index.php/s/hisat2-220-Linux_x86_64/download) in Ubuntu (by right-clicking the file and choose extract here) and this gives me the following file permissions:

total 91M
-rw-r--r-- 1 user user  584 Jul  9  2019 NEWS
-rw-r--r-- 1 user user  78K Jul  9  2019 MANUAL.markdown
-rw-r--r-- 1 user user  35K Jul  9  2019 LICENSE
-rwxr-xr-x 1 user user 2.8K Jul  9  2019 hisat2-build
-rw-r--r-- 1 user user 1.3K Jul  9  2019 AUTHORS
-rw-r--r-- 1 user user  202 Jan 23  2020 TUTORIAL
-rw-r--r-- 1 user user  64K Jan 23  2020 MANUAL
-rw-r--r-- 1 user user    6 Dec  2  2020 VERSION
-rwxr-xr-x 1 user user  35K Dec  2  2020 hisat2_simulate_reads.py
-rwxr-xr-x 1 user user 5.6K Dec  2  2020 hisat2_read_statistics.py
-rwxr-xr-x 1 user user 2.7K Dec  2  2020 hisat2-inspect
-rwxr-xr-x 1 user user 5.0K Dec  2  2020 hisat2_extract_splice_sites.py
-rwxr-xr-x 1 user user  35K Dec  2  2020 hisat2_extract_snps_haplotypes_VCF.py
-rwxr-xr-x 1 user user  19K Dec  2  2020 hisat2_extract_snps_haplotypes_UCSC.py
-rwxr-xr-x 1 user user 5.5K Dec  2  2020 hisat2_extract_exons.py
-rwxr-xr-x 1 user user  20K Dec  2  2020 hisat2
-rwxr-xr-x 1 user user 5.0K Dec  2  2020 extract_splice_sites.py
-rwxr-xr-x 1 user user 5.5K Dec  2  2020 extract_exons.py
-rwxr-xr-x 1 user user 5.3M Dec  2  2020 hisat2-build-s
-rwxr-xr-x 1 user user 5.3M Dec  2  2020 hisat2-build-l
-rwxr-xr-x 1 user user  15M Dec  2  2020 hisat2-align-s
-rwxr-xr-x 1 user user  15M Dec  2  2020 hisat2-align-l
-rwxr-xr-x 1 user user 3.5M Dec  2  2020 hisat2-inspect-s
-rwxr-xr-x 1 user user 3.5M Dec  2  2020 hisat2-inspect-l
-rwxr-xr-x 1 user user 6.7M Dec  2  2020 hisat2-repeat
-rwxr-xr-x 1 user user 3.7M Dec  2  2020 hisat2-build-s-debug
-rwxr-xr-x 1 user user 3.6M Dec  2  2020 hisat2-build-l-debug
-rwxr-xr-x 1 user user  11M Dec  2  2020 hisat2-align-s-debug
-rwxr-xr-x 1 user user  11M Dec  2  2020 hisat2-align-l-debug
-rwxr-xr-x 1 user user 1.8M Dec  2  2020 hisat2-inspect-s-debug
-rwxr-xr-x 1 user user 1.7M Dec  2  2020 hisat2-inspect-l-debug
-rwxr-xr-x 1 user user 5.2M Dec  2  2020 hisat2-repeat-debug
drwxr-xr-x 2 user user 4.0K Dec  2  2020 scripts
drwxr-xr-x 5 user user 4.0K Dec  2  2020 example

When I unzip this file with Python:

from zipfile import ZipFile

download_file = "hisat2-2.2.0-Linux_x86_64.zip"

with ZipFile(download_file, 'r') as zip_ref:
                zip_ref.extractall(script_dir)

The file permissions are different (I have lost the right to execute the file) when extracted from within Ubuntu:

total 91M
drwxrwxr-x 2 user user 4.0K Aug 18 23:15 scripts
drwxrwxr-x 2 user user 4.0K Aug 18 23:15 hisatgenotype_scripts
drwxrwxr-x 2 user user 4.0K Aug 18 23:15 hisatgenotype_modules
drwxrwxr-x 5 user user 4.0K Aug 18 23:15 example
-rw-rw-r-- 1 user user 5.0K Aug 18 23:25 hisat2_extract_splice_sites.py
-rw-rw-r-- 1 user user  35K Aug 18 23:25 hisat2_extract_snps_haplotypes_VCF.py
-rw-rw-r-- 1 user user 122K Aug 18 23:25 hisatgenotype_locus.py
-rw-rw-r-- 1 user user  21K Aug 18 23:25 hisatgenotype_build_genome.py
-rw-rw-r-- 1 user user 4.8K Aug 18 23:25 hisat2_read_statistics.py
-rw-rw-r-- 1 user user  19K Aug 18 23:25 hisat2_extract_snps_haplotypes_UCSC.py
-rw-rw-r-- 1 user user 5.0K Aug 18 23:25 extract_splice_sites.py
-rw-rw-r-- 1 user user  18K Aug 18 23:25 hisatgenotype.py
-rw-rw-r-- 1 user user  73K Aug 18 23:25 hisatgenotype_hla_cyp.py
-rw-rw-r-- 1 user user  52K Aug 18 23:25 hisatgenotype_extract_vars.py
-rw-rw-r-- 1 user user  23K Aug 18 23:25 hisatgenotype_extract_reads.py
-rw-rw-r-- 1 user user  34K Aug 18 23:25 hisat2_simulate_reads.py
-rw-rw-r-- 1 user user 5.5K Aug 18 23:25 hisat2_extract_exons.py
-rw-rw-r-- 1 user user 5.5K Aug 18 23:25 extract_exons.py
-rw-rw-r-- 1 user user    6 Aug 18 23:25 VERSION
-rw-rw-r-- 1 user user  202 Aug 18 23:25 TUTORIAL
-rw-rw-r-- 1 user user  584 Aug 18 23:25 NEWS
-rw-rw-r-- 1 user user  78K Aug 18 23:25 MANUAL.markdown
-rw-rw-r-- 1 user user  64K Aug 18 23:25 MANUAL
-rw-rw-r-- 1 user user  35K Aug 18 23:25 LICENSE
-rw-rw-r-- 1 user user 2.7K Aug 18 23:25 hisat2-inspect
-rw-rw-r-- 1 user user 2.8K Aug 18 23:25 hisat2-build
-rwxrwxr-x 1 user user  20K Aug 18 23:25 hisat2
-rw-rw-r-- 1 user user 1.3K Aug 18 23:25 AUTHORS
-rw-rw-r-- 1 user user 5.5M Aug 18 23:25 hisat2-build-s
-rw-rw-r-- 1 user user 5.5M Aug 18 23:25 hisat2-build-l
-rw-rw-r-- 1 user user  15M Aug 18 23:25 hisat2-align-s
-rw-rw-r-- 1 user user  15M Aug 18 23:25 hisat2-align-l
-rw-rw-r-- 1 user user 3.4M Aug 18 23:25 hisat2-inspect-s
-rw-rw-r-- 1 user user 3.4M Aug 18 23:25 hisat2-inspect-l
-rw-rw-r-- 1 user user 6.8M Aug 18 23:25 hisat2-repeat
-rw-rw-r-- 1 user user 3.5M Aug 18 23:25 hisat2-build-s-debug
-rw-rw-r-- 1 user user 3.4M Aug 18 23:25 hisat2-build-l-debug
-rw-rw-r-- 1 user user  11M Aug 18 23:25 hisat2-align-s-debug
-rw-rw-r-- 1 user user  11M Aug 18 23:25 hisat2-align-l-debug
-rw-rw-r-- 1 user user 1.7M Aug 18 23:25 hisat2-inspect-s-debug
-rw-rw-r-- 1 user user 1.7M Aug 18 23:25 hisat2-inspect-l-debug
-rw-rw-r-- 1 user user 5.1M Aug 18 23:25 hisat2-repeat-debug

How can I maintain the original file permissions with the Python command?



Solution 1:[1]

Python's zipfile does not support Unix-style permissions inside Zip files, since they are non standard. You can just call the unzip command via the subprocess module:

import subprocess

subprocess.run(["unzip", download_file, "-d", script_dir])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MegaIng