This commit is contained in:
jpmvaz
2026-09-13 19:48:16 +01:00
commit 3e287bda1a
7 changed files with 2375 additions and 0 deletions
+253
View File
@@ -0,0 +1,253 @@
# 📦 Folder Compressor
A command-line Python script that lets you pick any sub-folder and compress it into a `.7z` archive using **7-Zip** with **no compression (store mode)** — maximum speed, zero CPU overhead.
---
## Features
- Browse and select from all sub-folders in any directory
- Displays folder sizes before you choose
- Real-time progress bar with:
- Percentage complete
- Elapsed time
- Live transfer speed (MB/s)
- File count
- Currently processing file name
- Summary report on completion (source size, archive size, time, avg speed)
- Color-coded terminal output (auto-disabled if not supported)
- Cross-platform: Windows, macOS, Linux
---
## Requirements
### Python
Version **3.10 or higher** is required (uses `str | None` union type syntax).
Check your version:
```bash
python --version
# or
python3 --version
```
No third-party packages are needed — only Python's standard library is used.
### 7-Zip
7-Zip must be installed and accessible on your system PATH.
| Platform | Install command |
|----------|----------------|
| **Linux** (Debian/Ubuntu) | `sudo apt install p7zip-full` |
| **Linux** (Fedora/RHEL) | `sudo dnf install p7zip p7zip-plugins` |
| **macOS** (Homebrew) | `brew install p7zip` |
| **macOS** (MacPorts) | `sudo port install p7zip` |
| **Windows** | Download installer from [https://www.7-zip.org/download.html](https://www.7-zip.org/download.html) |
> **Windows note:** After installing, make sure `C:\Program Files\7-Zip\` is added to your system PATH, or the script will auto-detect it from the default install location automatically.
> **Minimum version:** 7-Zip **15.06 or newer** is required for the `-bsp1` progress flag used for real-time output. Most current installs will meet this requirement.
Verify 7-Zip is available:
```bash
7z i
```
---
## Installation
No installation needed. Just download the script:
```bash
# Download compress_folder.py to any directory, then run it directly with Python
```
Optionally make it executable on Linux/macOS:
```bash
chmod +x compress_folder.py
```
---
## Usage
### Basic — scan the current directory
```bash
python compress_folder.py
```
### Specify a directory to scan
```bash
python compress_folder.py /path/to/directory
```
### On Linux/macOS (if made executable)
```bash
./compress_folder.py
./compress_folder.py /path/to/directory
```
### On Windows
```bash
python compress_folder.py
python compress_folder.py C:\Users\YourName\Documents
```
---
## Step-by-step walkthrough
**1. Launch the script**
```
📦 Folder Compressor
Scanning: /home/user/projects
```
**2. Browse the folder list**
All sub-folders are listed with their sizes:
```
Available folders:
[ 1] archive (240.0 MB)
[ 2] builds (1.4 GB)
[ 3] logs (88.3 MB)
[ 4] source (320.5 MB)
Enter the number of the folder to compress:
```
**3. Select a folder**
Type the number and press Enter:
```
Enter the number of the folder to compress: 2
✔ Selected: builds
Calculating source size… 1.4 GB
```
**4. Watch real-time progress**
The progress display updates live in your terminal:
```
████████████████░░░░░░░░░░░░░░░░░░░ 62% elapsed 0m 08s 178.4 MB/s files: 312
↳ builds/release/v2.1.0/installer.exe
```
| Element | Description |
|---------|-------------|
| `████░░░` | Progress bar filling left to right |
| `62%` | Percentage of files processed |
| `elapsed 0m 08s` | Time since compression started |
| `178.4 MB/s` | Rolling average throughput speed |
| `files: 312` | Number of files added so far |
| `↳ filename` | The file currently being processed |
**5. Completion summary**
```
✅ Archive created successfully!
Path : /home/user/projects/builds.7z
Source : 1.4 GB
Archive : 1.4 GB
Time : 0m 09s
Avg speed: 159.2 MB/s
```
The `.7z` archive is saved in the **same parent directory** as the folder you selected.
---
## Compression settings
The script uses the following 7-Zip flags:
| Flag | Value | Meaning |
|------|-------|---------|
| `-t7z` | — | Output format: 7z |
| `-mx=0` | 0 | Compression level 0 = **store** (no compression) |
| `-ms=off` | off | Solid archive **disabled** (faster for many files) |
| `-bsp1` | — | Stream progress output to stdout (enables live display) |
**Why no compression?**
Store mode (`-mx=0`) copies files into the archive as-is without compressing them. This is ideal when:
- Speed matters more than file size reduction
- The contents are already compressed (videos, images, zip files, etc.)
- You want to bundle files for transfer without the CPU cost of compression
---
## How the real-time progress works
7-Zip does not write progress on separate lines — instead it continuously overwrites the same terminal line using special control characters. The exact character it uses depends on the platform:
| Platform | Character used | Code |
|----------|---------------|------|
| Linux / macOS | Backspace | `\x08` |
| Windows | Carriage return | `\r` |
The script reads 7-Zip's output as a raw binary stream and splits on **both** characters, so progress is captured correctly on every platform. The percentage, file count, and current filename are extracted from each segment using a regex and rendered live into two reserved terminal lines that update in place.
---
## Output location
The archive is always created in the **same directory that contains the selected folder**.
Example:
```
/home/user/projects/ ← scanned directory
builds/ ← selected folder
builds.7z ← archive created here
```
If an archive with the same name already exists, 7-Zip will update it (adding/replacing files). Delete the existing `.7z` first if you want a clean archive.
---
## Troubleshooting
**`7-Zip not found` error**
- Ensure 7-Zip is installed (see Requirements above)
- Confirm `7z` is on your PATH: run `which 7z` (Linux/macOS) or `where 7z` (Windows)
- On Windows, try re-installing 7-Zip and ticking the "Add to PATH" option, or place `7z.exe` in `C:\Program Files\7-Zip\` which the script checks automatically
**Progress bar stays at 0% / no stats shown**
- This was a known bug that has been fixed. Make sure you are using the latest version of the script.
- The root cause was that Windows 7-Zip uses `\r` to update progress lines while Linux/macOS uses `\x08` (backspace). The old version only handled backspaces, so on Windows the progress stream was never parsed. The current version handles both.
- If you still see 0% after updating, confirm your 7-Zip version is 15.06 or newer: run `7z i` and check the version line at the top.
**No progress bar / garbled output**
- The live progress display requires a terminal that supports ANSI escape codes
- On older Windows CMD, switch to Windows Terminal or PowerShell
- Progress and stats are still printed even if color/ANSI is not supported
**`SyntaxError` on startup**
- Your Python version is below 3.10 — upgrade to Python 3.10+
**Permission denied on folder**
- Run the script with elevated permissions (`sudo` on Linux/macOS, Run as Administrator on Windows)
- Or select a folder you have read access to
---
## Examples
```bash
# Compress a folder in the current directory (interactive)
python compress_folder.py
# Compress a folder inside a specific path
python compress_folder.py /mnt/data/backups
# Windows example
python compress_folder.py "C:\Users\Alice\Desktop"
```
---
## License
This script is provided as-is for personal and commercial use. No warranty is expressed or implied.