datalad_next.archive_operations.ZipArchiveOperations

class datalad_next.archive_operations.ZipArchiveOperations(location: Path, *, cfg: ConfigManager | None = None, **kwargs)[source]

Bases: ArchiveOperations

Handler for a ZIP archive on a local file system

close() None[source]

Calls .close() on the underlying zipfile.ZipFile instance

open(item: str | PurePosixPath | ZipInfo, **kwargs) Generator[IO | None, None, None][source]

Context manager, returning an open file for a member of the archive.

The file-like object will be closed when the context-handler exits.

This method can be used in conjunction with __iter__ to read any file from an archive:

with ZipArchiveOperations(archive_path) as zf:
    for item in zf:
        if item.type != FileSystemItemType.file:
            continue
        with zf.open(item.name) as fp:
            ...
Parameters:
  • item (str | PurePosixPath | zipfile.ZipInfo) -- Name, path, or ZipInfo-instance that identifies an item in the zipfile

  • kwargs (dict) -- Keyword arguments that will be used for ZipFile.open()

Returns:

A file-like object to read bytes from the item or to write bytes to the item.

Return type:

IO

property zipfile: ZipFile

Access to the wrapped ZIP archive as a zipfile.ZipFile