Compression Module

The Compression module is responsible for creating, extracting, and reading compressed archive files in tar and zip formats. It leverages Python’s built-in tarfile and zipfile modules to handle these operations efficiently.

Features

The Compression module offers robust and versatile tools for managing file compression and extraction, whether you prefer the tar or zip format.

  • Create Tar Archive: Compresses a single file, directory, or a list of files and directories into a tar archive, bundling them into a single file for easier management and transport.

  • Extract Tar Archive: Extracts files from a tar archive to a specified destination, with options to extract all files or a specified list, providing flexibility in handling archive contents.

  • Read Tar Archive: Reads and lists the contents of a tar archive without extracting them, allowing you to inspect files and directories before extraction.

  • Create Zip Archive: Compresses a single file, directory, or a list of files and directories into a zip archive, similar to tar but in a different format.

  • Extract Zip Archive: Extracts files from a zip archive to a specified destination, with options to extract all files or a specified list.

  • Read Zip Archive: Reads and lists the contents of a zip archive without extracting them, providing a convenient way to inspect files and directories.

TarFile

The TarFile module provides tools for managing file compression and extraction in tar format, making it an essential tool for efficient file management.

ZipFile

The ZipFile module provides tools for managing file compression and extraction in zip format, offering robust functionality for efficient file management.

Methods

from filesystem import compression

Method

Description

compression.tarfile.create_tar(fullpath_files, destination)

Compresses files or directories into a TAR file. Returns a message indicating whether a single file/directory or a list of files/directories was compressed.

compression.tarfile.extract(tar_filename, destination, extraction_list=[])

Extracts files from a tar archive. It can extract all files or a specified list of files. Returns True if successful, "[FileSystem Pro]: File Not Found" if the tar file is not found, False if a specified item in extraction_list is not found, or an Error Message if any other error occurs.

compression.tarfile.read_tar_archive(tar_filename)

Reads the contents of a TAR archive file and returns a list of the names of the files contained within it.

compression.zipfile.create_zip(fullpath_files, destination)

Compresses files or directories into a ZIP file. Returns a message indicating whether a single file/directory or a list of files/directories was compressed.

compression.zipfile.extract(zip_path, destination, extraction_list=None)

Reads the contents of a ZIP file and extracts files based on the provided parameters.

compression.zipfile.read_zip_archive(zip_filename, show_compression_system_files=True)

Reads the contents of a ZIP file and returns a list of the names of the files contained within it.

API Reference

TarFile

# TarFile

## Overview This module provides functions to create, extract, and read tar archives. It is designed to handle both single files and directories, making it versatile for various file compression and extraction needs.

## Features - Create Tar Archives: Compress single files or directories into a tar archive. - Extract Tar Archives: Extract all or specific files from a tar archive. - Read Tar Archives: List the contents of a tar archive.

## Usage To use these functions, simply import the module and call the desired function with appropriate parameters:

```python import os import tarfile

# Example: Creating a tar archive create_tar(‘/path/to/file_or_directory’, ‘/path/to/destination.tar’)

# Example: Extracting from a tar archive extract(‘/path/to/archive.tar’, ‘/path/to/destination’)

# Example: Reading a tar archive contents = read_tar_archive(‘/path/to/archive.tar’) print(contents) ```

filesystem.compression.tarfile.create_tar(fullpath_files, destination)[source]

# compression.create_tar(fullpath_files, destination)

### Overview Creates a tar archive at the specified destination path, compressing one or multiple files or directories provided in fullpath_files.

### Parameters: - fullpath_files (str or list): The full path of a single file/directory or a list of files/directories to compress. - destination (str): The destination path where the tar archive will be created.

### Returns: - str: A message indicating whether a single file/directory or a list of files/directories was compressed.

### Raises: - FileNotFoundError: If any of the specified files or directories do not exist. - PermissionError: If permission is denied for accessing the files or writing to the destination. - ValueError: If fullpath_files is neither a string nor a list.

### Examples: - Compresses a single file or directory into a tar archive.

`python create_tar("/path/to/file_or_directory", "/path/to/destination.tar") `

  • Compresses multiple files or directories into a tar archive.

`python files_to_compress = ["/path/to/file1", "/path/to/file2", "/path/to/dir"] create_tar(files_to_compress, "/path/to/destination.tar") `

filesystem.compression.tarfile.extract(tar_filename, destination, extraction_list=[])[source]

# compression.extract(tar_filename, destination, extraction_list=[])

### Overview Extracts files from a tar archive to the specified destination directory. You can extract the entire archive or specify a list of files to extract.

### Parameters: - tar_filename (str): The path of the tar archive to extract files from. - destination (str): The directory where the files will be extracted. - extraction_list (list, optional): A list of files or directories to extract. If empty, extracts all files. Defaults to an empty list.

### Returns: - bool: Returns True if extraction is successful, or False if a KeyError occurs.

### Raises: - FileNotFoundError: If the tar archive does not exist. - KeyError: If the specified file or directory is not found in the tar archive. - Exception: For any other errors that occur during the extraction process.

### Examples: - Extracts all files from a tar archive to the specified destination directory.

`python extract("/path/to/archive.tar", "/destination/directory") `

  • Extracts specific files from a tar archive.

`python extract("/path/to/archive.tar", "/destination/directory", extraction_list=["file1.txt", "file2.txt"]) `

filesystem.compression.tarfile.read_tar_archive(tar_filename)[source]

# compression.read_tar_archive(tar_filename)

### Overview Reads the contents of a tar archive and returns a list of the files within it.

### Parameters: - tar_filename (str): The path of the tar archive to read.

### Returns: - list: A list of filenames contained in the tar archive.

### Raises: - FileNotFoundError: If the tar archive does not exist. - Exception: For any other errors that occur during the process.

### Examples: - Reads the contents of a tar archive.

`python read_tar_archive("/path/to/archive.tar") `

ZipFile

# ZipFile

## Overview This module provides functions to create, extract, and read zip archives. It is designed to handle both single files and directories, making it versatile for various file compression and extraction needs.

## Features - Create Zip Archives: Compress single files or directories into a zip archive. - Extract Zip Archives: Extract all or specific files from a zip archive. - Read Zip Archives: List the contents of a zip archive.

## Usage To use these functions, simply import the module and call the desired function with appropriate parameters:

```python import os import zipfile

# Example: Creating a zip archive create_zip(‘/path/to/file_or_directory’, ‘/path/to/destination.zip’)

# Example: Extracting from a zip archive extract(‘/path/to/archive.zip’, ‘/path/to/destination’)

# Example: Reading a zip archive contents = read_zip_archive(‘/path/to/archive.zip’) print(contents) ```

filesystem.compression.zipfile.create_zip(fullpath_files, destination)[source]

# compression.zipfile.create_zip(fullpath_files, destination)

### Overview Creates a zip archive at the specified destination path, compressing one or multiple files or directories provided in fullpath_files.

### Parameters: - fullpath_files (str or list): The full path of a single file/directory or a list of files/directories to compress. - destination (str): The destination path where the zip archive will be created.

### Returns: - str: A message indicating whether a single file/directory or a list of files/directories was compressed.

### Raises: - FileNotFoundError: If any of the specified files or directories do not exist. - PermissionError: If the permission is denied for accessing the files or writing to the destination. - ValueError: If fullpath_files is neither a string nor a list.

### Examples: - Compresses a single file or directory into a zip archive.

`python create_zip("/path/to/file_or_directory", "/path/to/destination.zip") `

  • Compresses multiple files or directories into a zip archive.

`python files_to_compress = ["/path/to/file1", "/path/to/file2", "/path/to/dir"] create_zip(files_to_compress, "/path/to/destination.zip") `

filesystem.compression.zipfile.extract(zip_path, destination, extraction_list=None)[source]

# compression.zipfile.extract(zip_path, destination, extraction_list=None)

### Overview Extracts files from a zip archive to the specified destination directory. You can extract the entire archive or specify a list of files to extract.

### Parameters: - zip_path (str): The path of the zip archive from which to extract files. - destination (str): The directory where the files will be extracted. - extraction_list (None or list or str, optional): If None, extracts all files. If a list, extracts only the files specified in the list. If a string, extracts the file specified. Defaults to None.

### Returns: - None

### Raises: - FileNotFoundError: If the zip archive does not exist. - PermissionError: If permission is denied for reading the zip archive or writing to the destination. - ValueError: If extraction_list is not None, a list, or a string.

### Examples: - Extracts all files from a zip archive to the specified destination directory.

`python extract("/path/to/archive.zip", "/destination/directory") `

  • Extracts specific files from a zip archive.

`python extract("/path/to/archive.zip", "/destination/directory", extraction_list=["file1.txt", "file2.txt"]) `

  • Extracts a single file from a zip archive.

`python extract("/path/to/archive.zip", "/destination/directory", extraction_list="file1.txt") `

filesystem.compression.zipfile.read_zip_archive(zip_filename, show_compression_system_files=True)[source]

# compression.zipfile.read_zip_archive(zip_filename, show_compression_system_files=True)

### Overview Reads the contents of a zip archive and returns a list of the files within it. You can choose to include or exclude compression system files (e.g., __MACOSX/, .DS_Store).

### Parameters: - zip_filename (str): The path of the zip archive to read. - show_compression_system_files (bool, optional): If True, includes compression system files in the list. Defaults to True.

### Returns: - list: A list of filenames contained in the zip archive.

### Raises: - FileNotFoundError: If the zip archive does not exist. - Exception: For any other errors that occur during the process.

### Examples: - Reads the contents of a zip archive and includes compression system files.

`python read_zip_archive("/path/to/archive.zip") `

  • Reads the contents of a zip archive and excludes compression system files.

`python read_zip_archive("/path/to/archive.zip", show_compression_system_files=False) `

Examples

TarFile Sample Codes

Creating a Tar Archive

from filesystem import compression
compression.tarfile.create_tar('/path/to/file_or_directory', '/path/to/destination.tar')

Extracting from a Tar Archive

from filesystem import compression
compression.tarfile.extract('/path/to/archive.tar', '/path/to/destination')

Reading a Tar Archive

from filesystem import compression
contents = compression.tarfile.read_tar_archive('/path/to/archive.tar')
print(contents)

ZipFile Sample Codes

Creating a Zip Archive

from filesystem import compression
compression.zipfile.create_zip('/path/to/file_or_directory', '/path/to/destination.zip')

Extracting from a Zip Archive

from filesystem import compression
compression.zipfile.extract('/path/to/archive.zip', '/path/to/destination')

Reading a Zip Archive

from filesystem import compression
contents = compression.zipfile.read_zip_archive('/path/to/archive.zip')
print(contents)