| import os |
| import zipfile |
| import time |
| import sys |
| from pathlib import Path |
|
|
| def zip_directory_with_progress(source_dir, output_zip): |
| source_path = Path(source_dir) |
| |
| print(f"Scanning directory: {source_dir} ...", flush=True) |
| file_list = [] |
| total_size = 0 |
| for root, dirs, files in os.walk(source_dir): |
| for file in files: |
| file_path = os.path.join(root, file) |
| file_list.append(file_path) |
| total_size += os.path.getsize(file_path) |
| |
| total_gb = total_size / (1024**3) |
| print(f"Total files: {len(file_list)}", flush=True) |
| print(f"Total size: {total_gb:.2f} GB", flush=True) |
| print("-" * 30, flush=True) |
|
|
| processed_size = 0 |
| start_time = time.time() |
|
|
| with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_STORED) as zf: |
| for file_path in file_list: |
| arcname = os.path.relpath(file_path, source_path.parent) |
| zf.write(file_path, arcname) |
| |
| file_size = os.path.getsize(file_path) |
| processed_size += file_size |
| |
| percent = (processed_size / total_size) * 100 |
| elapsed_time = time.time() - start_time |
| |
| if processed_size > 0: |
| total_expected_time = (elapsed_time / processed_size) * total_size |
| remaining_time = total_expected_time - elapsed_time |
| else: |
| remaining_time = 0 |
|
|
| mb_per_sec = (processed_size / (1024**2)) / elapsed_time if elapsed_time > 0 else 0 |
| |
| progress_msg = ( |
| f"\rProgress: [{int(percent/2)*'=':50}] {percent:.1f}% | " |
| f"Speed: {mb_per_sec:.2f} MB/s | " |
| f"ETA: {int(remaining_time // 60)}m {int(remaining_time % 60)}s" |
| ) |
| sys.stdout.write(progress_msg) |
| sys.stdout.flush() |
|
|
| total_duration = time.time() - start_time |
| print(f"\n\nArchive Complete! Total Time: {int(total_duration // 60)}m {int(total_duration % 60)}s", flush=True) |
| print(f"Output File: {output_zip}", flush=True) |
|
|
| if __name__ == "__main__": |
| TARGET = "" |
| OUTPUT = "" |
| |
| zip_directory_with_progress(TARGET, OUTPUT) |