Ivan Bilan
Ivan Bilan

Reputation: 2439

Read xml files directly from a zip file using Python

I have a following zip file structure:

some_file.zip/folder/folder/files.xml

So I have a lot of xml files within a subfolder of the zip file.

So far I have managed to unpack the zip file using the following code:

import os.path
import zipfile

with zipfile.ZipFile('some_file.zip') as zf:
    for member in zf.infolist():
        # Path traversal defense copied from
        # http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
        words = member.filename.split('/')
        path = "output"
        for word in words[:-1]:
            drive, word = os.path.splitdrive(word)
            head, word = os.path.split(word)
            if word in (os.curdir, os.pardir, ''): continue
            path = os.path.join(path, word)

        zf.extract(member, path)

But I do not need to extract the files but to read them directly from the zip file. So either read each file within a for loop and process it or to save each file in some kind of data structure in Python. Is it possible?

Upvotes: 4

Views: 13675

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210882

as Robin Davis has written zf.open() will do the trick. Here is a small example:

import zipfile

zf = zipfile.ZipFile('some_file.zip', 'r')

for name in zf.namelist():
    if name.endswith('/'): continue

    if 'folder2/' in name:
        f = zf.open(name)
        # here you do your magic with [f] : parsing, etc.
        # this will print out file contents
        print(f.read()) 

As OP wished in comment only files from the "folder2" will be processed...

Upvotes: 9

Robin Davis
Robin Davis

Reputation: 632

zf.open() will return a file like object without extracting it.

Upvotes: 5

Related Questions