Extract Certain Files from .Zip

Extract certain files from .zip

Thanks to comment from @user20650.

Use two calls to unzip. First with list=TRUE just to get the $Name for the files. Second with files= to extract only the files whose names match the pattern.

  zipped_csv_names <- grep('\\.csv$', unzip('some_archive.zip', list=TRUE)$Name, 
ignore.case=TRUE, value=TRUE)
unzip('some_archive.zip', files=zipped_csv_names)
comb_tbl <- rbindlist(lapply(zipped_csv_names,
function(x) cbind(fread(x, sep=',', header=TRUE,
stringsAsFactors=FALSE),
file_nm=x)), fill=TRUE )

extract certain file from zip but not complete directory

import os
import shutil
import zipfile

zip_filepath='/home/sundeep/Desktop/SCHEMA AUTOMATION/SOURCE/DSP8010_2017.1.zip'
target_dir='/home/sundeep/Desktop/SCHEMA AUTOMATION/SCHEMA'

with zipfile.ZipFile(zip_filepath) as z:
with z.open('DSP8010_2017.1/json-schema/AccountService.json') as zf, open(os.path.join(target_dir, os.path.basename('AccountService.json')), 'wb') as f:
shutil.copyfileobj(zf, f)

How to extract specific set of files in a zip file using python

You can iterate over all files in zip archive and check filename before extracting:

import zipfile

with zipfile.ZipFile('test.zip', 'r') as zp:
files = zipfile.ZipFile.infolist(zp)
for file in files:
if file.filename.startswith('first'):
with open(file.filename, 'wb') as f:
f.write(zp.read(file.filename))


Related Topics



Leave a reply



Submit