Last active
May 9, 2025 01:18
-
-
Save ldemailly/45b51186a7999b7eeba04a6639787d2d to your computer and use it in GitHub Desktop.
Magic compression - best ratio ever ;-) 1:442604 for instance on a 22Mb binary to 52bytes !
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
| #!/bin/sh | |
| # Fun idea by https://github.com/ldemailly | |
| set -e | |
| filename="$1" | |
| if [ -z "$filename" ]; then | |
| echo "Usage: $0 <file|file.mc>" >&2 | |
| exit 1 | |
| fi | |
| get_size() { | |
| # unlike stat should work on both macos and linux | |
| ls -l "$1" | awk '{print $5}' | |
| } | |
| if [ "${filename##*.}" = "mc" ]; then | |
| base="${filename%.mc}" | |
| mv ".$base" "$base" | |
| rm "$filename" | |
| echo "Uncompressed succesfully back to $base" | |
| else | |
| if [ ! -f "$filename" ]; then | |
| echo "File not found: $filename" >&2 | |
| exit 1 | |
| fi | |
| mv "$filename" ".$filename" | |
| echo "Supercompressed $filename (c) magic compression people" > "$filename.mc" | |
| orig_size=$(get_size ".$filename") | |
| new_size=$(get_size "$filename.mc") | |
| ratio=$(awk "BEGIN {printf \"%.0f\", $orig_size / $new_size}") | |
| echo "Compression ratio: 1:$ratio achieved for $filename -> $filename.mc" | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A bit of a private jokes for the fine folks at the Programmer's Hangout discord :)