scistag.filestag.memory_zip.MemoryZip

class MemoryZip(source=None, **kwargs)[source]

Bases: ZipFile

Helper file for fast and easy zip data creation and extraction in-memory

Parameters
  • source (str | bytes | None) – The data source to be loaded to memory. If no source is specified (as by default) a new, empty zip archive will be created.

  • kwargs – Additional initializer parameters, see :clasS: zipfile.ZipFile.

Methods

close

Close the file, and for mode 'w', 'x' and 'a' write the ending records.

extract

Extract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. `member' may be a filename or a ZipInfo object. You can specify a different directory using `path'.

extractall

Extract all members from the archive to the current working directory. `path' specifies a different directory to extract to. `members' is optional and must be a subset of the list returned by namelist().

getinfo

Return the instance of ZipInfo given 'name'.

infolist

Return a list of class ZipInfo instances for files in the archive.

namelist

Return a list of file names in the archive.

open

Return file-like object for 'name'.

printdir

Print a table of contents for the zip file.

read

Return file bytes for name.

setpassword

Set default password for encrypted files.

testzip

Read all the files and check the CRC.

to_bytes

Closes the zip archive and it's content as bytes

write

Put the bytes from filename into the archive under the name arcname.

writestr

Write a file into the archive.

Attributes

__dict__

__doc__

__module__

__weakref__

list of weak references to the object (if defined)

_windows_illegal_name_trans_table

comment

The comment text associated with the ZIP file.

fp

_RealGetContents()

Read in the table of contents for the ZIP file.

_extract_member(member, targetpath, pwd)

Extract the ZipInfo object ‘member’ to a physical file on the path targetpath.

classmethod _sanitize_windows_name(arcname, pathsep)

Replace bad characters and remove trailing dots from parts.

_writecheck(zinfo)

Check for errors before writing a file to the archive.

close()[source]

Close the file, and for mode ‘w’, ‘x’ and ‘a’ write the ending records.

Return type

None

extract(member, path=None, pwd=None)

Extract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. `member’ may be a filename or a ZipInfo object. You can specify a different directory using `path’.

extractall(path=None, members=None, pwd=None)

Extract all members from the archive to the current working directory. `path’ specifies a different directory to extract to. `members’ is optional and must be a subset of the list returned by namelist().

getinfo(name)

Return the instance of ZipInfo given ‘name’.

infolist()

Return a list of class ZipInfo instances for files in the archive.

namelist()

Return a list of file names in the archive.

open(name, mode='r', pwd=None, *, force_zip64=False)

Return file-like object for ‘name’.

name is a string for the file name within the ZIP file, or a ZipInfo object.

mode should be ‘r’ to read a file already in the ZIP file, or ‘w’ to write to a file newly added to the archive.

pwd is the password to decrypt files (only used for reading).

When writing, if the file size is not known in advance but may exceed 2 GiB, pass force_zip64 to use the ZIP64 format, which can handle large files. If the size is known in advance, it is best to pass a ZipInfo instance for name, with zinfo.file_size set.

printdir(file=None)

Print a table of contents for the zip file.

read(name, pwd=None)

Return file bytes for name.

setpassword(pwd)

Set default password for encrypted files.

testzip()

Read all the files and check the CRC.

to_bytes()[source]

Closes the zip archive and it’s content as bytes

Return type

bytes

Returns

The zip data

write(filename, arcname=None, compress_type=None, compresslevel=None)

Put the bytes from filename into the archive under the name arcname.

writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)

Write a file into the archive. The contents is ‘data’, which may be either a ‘str’ or a ‘bytes’ instance; if it is a ‘str’, it is encoded as UTF-8 first. ‘zinfo_or_arcname’ is either a ZipInfo instance or the name of the file in the archive.

property comment

The comment text associated with the ZIP file.