hup.io.zip module

File I/O for ZIP Archives.

class File(filepath: Union[str, os.PathLike, None] = None, pwd: Optional[bytes] = None)

Bases: object

In-Memory Zip Archives.

Parameters:
  • filepath – String or path-like object, that points to a valid ZipFile or None. If the filepath points to a valid ZipFile, then the class instance is initialized with a memory copy of the file. If the given file, however, does not exist or isn’t a valid ZipFile, respectively one of the errors FileNotFoundError or BadZipFile is raised. The default behaviour, if the filepath is None, is to create an empty ZipFile in the memory.
  • pwd – Bytes representing password of ZipFile.
append(source: Union[str, os.PathLike], target: Union[str, os.PathLike, None] = None) → bool

Append file to the ZipFile.

Parameters:
  • source – String or path-like object, that points to a valid file in the directory structure if the system. If the file does not exist, a FileNotFoundError is raised. If the filepath points to a directory, a IsADirectoryError is raised.
  • target – String or path-like object, that points to a valid directory in the directory structure of the ZipFile. By default the root directory is used. If the directory does not exist, a FileNotFoundError is raised. If the target directory already contains a file, which name equals the filename of the source, a FileExistsError is raised.
Returns:

Boolean value which is True if the file has been appended.

changed

Tells whether the ZipFile has been changed.

close() → None

Close current ZipFile and buffer.

copy(source: Union[str, os.PathLike], target: Union[str, os.PathLike]) → bool

Copy file within ZipFile.

Parameters:
  • source – String or path-like object, that points to a file in the directory structure of the ZipFile. If the file does not exist, a FileNotFoundError is raised. If the filepath points to a directory, an IsADirectoryError is raised.
  • target – String or path-like object, that points to a new filename or an existing directory in the directory structure of the ZipFile. If the target is a directory the target file consists of the directory and the basename of the source file. If the target file already exists a FileExistsError is raised.
Returns:

Boolean value which is True if the file was copied.

files

List of all files within the ZipFile.

folders

List of all folders within the ZipFile.

get_file_accessor(path: Union[str, os.PathLike]) → hup.io.abc.Connector

Get connector to ZipFile member.

Parameters:path – String or path-like object, that represents a ZipFile member. In reading mode the path has to point to a valid ZipFile, or a FileNotFoundError is raised. In writing mode the path by default is treated as a file path. New directories can be written by setting the argument is_dir to True.
Returns:File accessor to ZipFile member.
load(filepath: Union[str, os.PathLike], pwd: Optional[bytes] = None) → None

Load Workspace from file.

Parameters:
  • filepath – String or path-like object, that points to a valid ZipFile file. If the filepath points to a valid ZipFile, then the class instance is initialized with a memory copy of the file. If the given file, however, does not exist or isn’t a valid ZipFile respectively one of the errors FileNotFoundError or BadZipFile is raised.
  • pwd – Bytes representing password of ZipFile.
mkdir(dirpath: Union[str, os.PathLike], ignore_exists: bool = False) → bool

Create a new directory at the given path.

Parameters:
  • dirpath – String or path-like object, that represents a valid directory name in the directory structure of the ZipFile. If the directory already exists, the argument ignore_exists determines, if a FileExistsError is raised.
  • ignore_exists – Boolean value which determines, if FileExistsError is raised, if the target directory already exists. The default behaviour is to raise an error, if the file already exists.
Returns:

Boolean value, which is True if the given directory was created.

move(source: Union[str, os.PathLike], target: Union[str, os.PathLike]) → bool

Move file within ZipFile.

Parameters:
  • source – String or path-like object, that points to a file in the directory structure of the ZipFile. If the file does not exist, a FileNotFoundError is raised. If the filepath points to a directory, an IsADirectoryError is raised.
  • target – String or path-like object, that points to a new filename or an existing directory in the directory structure of the ZipFile. If the target is a directory the target file consists of the directory and the basename of the source file. If the target file already exists a FileExistsError is raised.
Returns:

Boolean value which is True if the file has been moved.

name

Filename of the ZipFile without file extension.

open(path: Union[str, os.PathLike], mode: str = 'r', encoding: Optional[str] = None, is_dir: bool = False) → IO[Any]

Open file within the ZipFile.

Parameters:
  • path – String or path-like object, that represents a ZipFile member. In reading mode the path has to point to a valid ZipFile, or a FileNotFoundError is raised. In writing mode the path by default is treated as a file path. New directories can be written by setting the argument is_dir to True.
  • mode – String, which characters specify the mode in which the file is to be opened. The default mode is reading in text mode. Suported characters are: ‘r’: Reading mode (default) ‘w’: Writing mode ‘b’: Binary mode ‘t’: Text mode (default)
  • encoding – In binary mode encoding has not effect. In text mode encoding specifies the name of the encoding, which in reading and writing mode respectively is used to decode the stream’s bytes into strings, and to encode strings into bytes. By default the preferred encoding of the operating system is used.
  • is_dir – Boolean value which determines, if the path is to be treated as a directory or not. This information is required for writing directories to the ZipFile. The default behaviour is not to treat paths as directories.
Returns:

File object in reading or writing mode.

Examples

>>> with self.open('config.ini') as file:
>>>     print(file.read())
path

Filepath of the ZipFile.

read_bytes(filepath: Union[str, os.PathLike]) → bytes

Read bytes from file.

Parameters:filepath – String or path-like object, that points to a valid file in the dirctory structure of the ZipFile. If the file does not exist a FileNotFoundError is raised.
Returns:Contents of the given filepath as bytes.
read_text(filepath: Union[str, os.PathLike], encoding: Optional[str] = None) → str

Read text from file.

Parameters:
  • filepath – String or path-like object, that points to a valid file in the directory structure of the ZipFile. If the file does not exist a FileNotFoundError is raised.
  • encoding – Specifies the name of the encoding, which is used to decode the stream’s bytes into strings. By default the preferred encoding of the operating system is used.
Returns:

Contents of the given filepath encoded as string.

rmdir(dirpath: Union[str, os.PathLike], recursive: bool = False, ignore_missing: bool = False) → bool

Remove directory from ZipFile.

Parameters:
  • dirpath – String or path-like object, that points to a directory in the directory structure of the ZipFile. If the directory does not exist, the argument ignore_missing determines, if a FileNotFoundError is raised.
  • ignore_missing – Boolean value which determines, if FileNotFoundError is raised, if the target directory does not exist. The default behaviour, is to raise an error if the directory is missing.
  • recursive – Boolean value which determines, if directories are removed recursively. If recursive is False, then only empty directories can be removed. If recursive, however, is True, then all files and subdirectories are alse removed. By default recursive is False.
Returns:

Boolean value, which is True if the given directory was removed.

save() → None

Save the ZipFile to it’s filepath.

saveas(filepath: Union[str, os.PathLike]) → None

Save the ZipFile to a file.

Parameters:filepath – String or path-like object, that represents the name of a ZipFile.
search(pattern: Optional[str] = None) → List[str]

Search for files in the ZipFile.

Parameters:pattern – Search pattern that contains Unix shell-style wildcards: ‘*’: Matches arbitrary strings ‘?’: Matches single characters [seq]: Matches any character in seq [!seq]: Matches any character not in seq By default a list of all files and directories is returned.
Returns:List of files and directories in the directory structure of the ZipFile, that match the search pattern.

Remove file from ZipFile.

Parameters:
  • filepath – String or path-like object, that points to a file in the directory structure of the ZipFile. If the filepath points to a directory, an IsADirectoryError is raised. For the case, that the file does not exist, the argument ignore_missing determines, if a FileNotFoundError is raised.
  • ignore_missing – Boolean value which determines, if FileNotFoundError is raised, if the target file does not exist. The default behaviour, is to ignore missing files.
Returns:

Boolean value, which is True if the given file was removed.

write_bytes(blob: Union[bytes, bytearray, memoryview], filepath: Union[str, os.PathLike]) → int

Write bytes to file.

Parameters:
  • blob – Bytes, which are to be written to the given file.
  • filepath – String or path-like object, that represents a valid filename in the dirctory structure of the ZipFile.
Returns:

Number of bytes, that are written to the file.

write_text(text: str, filepath: Union[str, os.PathLike], encoding: Optional[str] = None) → int

Write text to file.

Parameters:
  • text – String, which has to be written to the given file.
  • filepath – String or path-like object, that represents a valid filename in the dirctory structure of the ZipFile.
  • encoding – Specifies the name of the encoding, which is used to encode strings into bytes. By default the preferred encoding of the operating system is used.
Returns:

Number of characters, that are written to the file.