Shortcuts

ZipArchiveLoader

class torchdata.datapipes.iter.ZipArchiveLoader(datapipe: Iterable[Tuple[str, BufferedIOBase]], length: int = -1)

Opens/decompresses zip binary streams from an Iterable DataPipe which contains a tuple of path name and zip binary stream, and yields a tuple of path name and extracted binary stream (functional name: load_from_zip).

Parameters:
  • datapipe – Iterable DataPipe that provides tuples of path name and zip binary stream

  • length – Nominal length of the DataPipe

Note

The opened file handles will be closed automatically if the default DecoderDataPipe is attached. Otherwise, user should be responsible to close file handles explicitly or let Python’s GC close them periodically. Due to how zipfiles implements its open() method, the data_stream variable below cannot be closed within the scope of this function.

Example

>>> from torchdata.datapipes.iter import FileLister, FileOpener
>>> datapipe1 = FileLister(".", "*.zip")
>>> datapipe2 = FileOpener(datapipe1, mode="b")
>>> zip_loader_dp = datapipe2.load_from_zip()
>>> for _, stream in zip_loader_dp:
>>>     print(stream.read())
b'0123456789abcdef'

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources