handy-archives

0.2.0 · active · verified Thu Apr 16

handy-archives is a Python library providing a collection of convenient helpers for working with archive files, currently at version 0.2.0. It aims to simplify common archiving tasks, building upon functionalities available in Python's standard `zipfile`, `tarfile`, and `shutil` modules. Releases appear to follow an on-demand cadence as features or fixes are introduced.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create both ZIP and Tar.gz archives and then extract them using `handy-archives`. It leverages the re-exported `ZipFile` and `TarFile` classes for creation, and the convenient `unpack_archive` function for extraction, handling different archive formats automatically. Dummy files and directories are created for demonstration and then cleaned up.

import os
from pathlib import Path
from handy_archives import unpack_archive, TarFile, ZipFile

# Setup: Create some dummy files and a directory
Path('temp_files').mkdir(exist_ok=True)
Path('temp_files/file1.txt').write_text('Hello from file 1')
Path('temp_files/file2.log').write_text('Log entry 1\nLog entry 2')
Path('temp_files/subdir').mkdir(exist_ok=True)
Path('temp_files/subdir/nested.csv').write_text('a,b,c\n1,2,3')

archive_name_zip = 'my_archive.zip'
archive_name_tar_gz = 'my_archive.tar.gz'

# 1. Create a Zip Archive using handy_archives.ZipFile (re-export of standard library)
with ZipFile(archive_name_zip, 'w') as zipf:
    zipf.write('temp_files/file1.txt', 'file1.txt')
    zipf.write('temp_files/file2.log', 'file2.log')
    zipf.write('temp_files/subdir/nested.csv', 'subdir/nested.csv')
print(f"Created ZIP archive: {archive_name_zip}")

# 2. Create a Tar.gz Archive using handy_archives.TarFile (re-export of standard library)
with TarFile(archive_name_tar_gz, 'w:gz') as tarf:
    tarf.add('temp_files/file1.txt', arcname='file1.txt')
    tarf.add('temp_files/file2.log', arcname='file2.log')
    tarf.add('temp_files/subdir/nested.csv', arcname='subdir/nested.csv')
print(f"Created TAR.GZ archive: {archive_name_tar_gz}")

# 3. Extract the Zip Archive using handy_archives.unpack_archive
output_dir_zip = 'extracted_zip'
os.makedirs(output_dir_zip, exist_ok=True)
unpack_archive(archive_name_zip, output_dir_zip)
print(f"Extracted {archive_name_zip} to {output_dir_zip}")

# 4. Extract the Tar.gz Archive using handy_archives.unpack_archive
output_dir_tar = 'extracted_tar'
os.makedirs(output_dir_tar, exist_ok=True)
unpack_archive(archive_name_tar_gz, output_dir_tar)
print(f"Extracted {archive_name_tar_gz} to {output_dir_tar}")

# Cleanup
os.remove(archive_name_zip)
os.remove(archive_name_tar_gz)
os.remove('temp_files/file1.txt')
os.remove('temp_files/file2.log')
os.remove('temp_files/subdir/nested.csv')
os.rmdir('temp_files/subdir')
os.rmdir('temp_files')
os.rmdir(output_dir_zip)
os.rmdir(output_dir_tar)

view raw JSON →