Created
May 16, 2025 10:04
-
-
Save hmidani-abdelilah/b1e3a9ab6cea96f7c4c569547f2b7c03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
import py7zr | |
import argparse | |
import sys | |
from tqdm import tqdm | |
def compress_with_options(folder_path, archive_name, keep_original=False, password=None): | |
folder_path = os.path.abspath(folder_path) | |
if not os.path.isdir(folder_path): | |
print("❌ The specified folder does not exist.") | |
sys.exit(1) | |
parent_dir = os.path.dirname(folder_path) | |
archive_path = os.path.join(parent_dir, archive_name + ".7z") | |
try: | |
print(f"🔄 Compressing folder: {folder_path}") | |
files_to_add = [] | |
for root, _, files in os.walk(folder_path): | |
for file in files: | |
full_path = os.path.join(root, file) | |
arcname = os.path.relpath(full_path, start=os.path.dirname(folder_path)) | |
files_to_add.append((full_path, arcname)) | |
# إنشاء الأرشيف باستخدام py7zr بدون تحديد مستوى الضغط | |
with py7zr.SevenZipFile(archive_path, 'w', password=password) as archive: | |
with tqdm(total=len(files_to_add), unit="file", desc="📦 Compressing", ncols=80) as progress: | |
for file_path, arcname in files_to_add: | |
archive.write(file_path, arcname) | |
progress.update(1) | |
print(f"✅ Folder was compressed to: {archive_path}") | |
if not keep_original: | |
shutil.rmtree(folder_path) | |
print(f"🗑️ Original folder has been deleted: {folder_path}") | |
else: | |
print("ℹ️ Original folder was kept.") | |
except Exception as e: | |
print(f"❌ Compression failed: {e}") | |
sys.exit(1) | |
def extract_archive(archive_path, output_path=None, password=None): | |
archive_path = os.path.abspath(archive_path) | |
if not os.path.isfile(archive_path): | |
print("❌ The specified archive does not exist.") | |
sys.exit(1) | |
if not output_path: | |
output_path = os.path.splitext(archive_path)[0] + "_extracted" | |
try: | |
print(f"🔓 Extracting archive: {archive_path}") | |
with py7zr.SevenZipFile(archive_path, mode='r', password=password) as archive: | |
all_files = archive.getnames() | |
with tqdm(total=len(all_files), unit="file", desc="📂 Extracting", ncols=80) as progress: | |
archive.extractall(path=output_path) | |
progress.update(len(all_files)) | |
print(f"✅ Archive was extracted to: {output_path}") | |
except Exception as e: | |
print(f"❌ Extraction failed: {e}") | |
sys.exit(1) | |
def main(): | |
parser = argparse.ArgumentParser(description="📦 7z CLI Tool") | |
subparsers = parser.add_subparsers(dest="command", required=True) | |
compress_parser = subparsers.add_parser("compress") | |
compress_parser.add_argument("folder") | |
compress_parser.add_argument("name") | |
compress_parser.add_argument("-k", "--keep", action="store_true") | |
compress_parser.add_argument("-p", "--password") | |
extract_parser = subparsers.add_parser("extract") | |
extract_parser.add_argument("archive") | |
extract_parser.add_argument("-o", "--output") | |
extract_parser.add_argument("-p", "--password") | |
args = parser.parse_args() | |
if args.command == "compress": | |
compress_with_options(args.folder, args.name, args.keep, args.password) | |
elif args.command == "extract": | |
extract_archive(args.archive, args.output, args.password) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment