Accessing files inside a .zip archive without extracting them

有些话、适合烂在心里 提交于 2019-12-23 02:43:10

问题


I am attempting to read a NIFTI file inside a .zip without having to extract the directory to the root directory. More specifically, I am working with the ADNI database and the files are stored by subjectID in individual .zip files. Inside the .zip file there is all the data pertaining to that subject, I would like to extract the NIFTI file (.nii.gz) from within the .zip without extracting the files.

Currently I have the following code snippet

def openNIFTI(filename):
   return nib.load(filename).get_data()

zip_filename = filepath + str(subject_id) + '_3T_Structural_unproc.zip'
filename = str(subject_id) + '/unprocessed/3T/T1w_MPR1/' + str(subject_id) + '_3T_T1w_MPR1.nii.gz'

file = zf.extract(filename)
data = openNIFTI(file)

The filepath is the path to the collection of .zip files. filename is the path inside the .zip file to the NIFTI file I want to extract.


(Edit)

It seem the error is coming from the nibabel load function. Then function checks

if not op.exists(filename):

Upon testing the os.path.exists(filename) function independently I found that.

os.path.exists(r'C:/Users/eee/workspace_python/Image Reconstruction/data/ADNI/MRI data/100206_3T_Structural_unproc.zip/100206/unprocessed/3T/T1w_MPR1/100206_3T_T1w_MPR1.nii.gz')

False

However, this path is copy/pasted from the file I am attempting to open. It seems to me the error appears due to the .zip in the file path because

os.path.exists(r'C:/Users/eee/workspace_python/Image Reconstruction/data/ADNI/MRI data/100206_3T_Structural_unproc.zip')

True

Is there another way of doing this?


回答1:


Please see the accepted answer to python: Open file from zip without temporary extracting it this shows how to read the data from within a zipfile without extracting the file, (of course you need enough RAM to handle the file contents).



来源:https://stackoverflow.com/questions/48456420/accessing-files-inside-a-zip-archive-without-extracting-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!