Last active
March 25, 2022 16:21
-
-
Save porg/449ad9a61a75c113f771760b328ebe01 to your computer and use it in GitHub Desktop.
File order in archive will be baked into metadata of extracted files
This file contains 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/bash | |
# https://github.com/aonez/Keka/issues/932#issuecomment-989762514 | |
# By Matt Sephton @gingerbeardman 2021-12-11 | |
# - Created all versions til then | |
# By @porg 2021-12-16 | |
# + Hiding file extension on all extracted files | |
# + Setting creation and modification date of output directory | |
# Dependencies: p7zip, pipx, osxmetadata, SetFile (ships with Xcode) | |
# $ brew install p7zip | |
# $ brew install pipx | |
# $ pipx install osxmetadata | |
# $ xcode-select --install | |
# Check your PATH and confirm operation | |
# Get base of filename | |
BASE=$(basename "$1" .zip) | |
# Get files in zip order, use 7z to respect character encodings | |
FILES=$(7z l -slt -ba "$1" | grep "Path" | sed 's/Path \= //g;') | |
# Prepare timestamps | |
DATESTAMP=$(date -r "$1") | |
UNIXSTAMP=$(date -jf "%a %b %d %T %Z %Y" "$DATESTAMP" +"%s") | |
TOUCHSTAMP=$(date -jf "%s" "$UNIXSTAMP" +"%Y%m%d%H%M.%S") | |
CREATIONSTAMP=$(date -jf "%s" "$UNIXSTAMP" +"%m/%d/%Y %H:%M:%S") | |
YYYYMMDDhhmmss=$(date -jf "%s" "$UNIXSTAMP" +"%Y-%m-%d %H:%M:%S") | |
# Unzip and only show interesting lines of output | |
7z x "$1" -aoa -o"$BASE" | grep "[th]ing" | |
# Set counters | |
COUNTER=1 | |
# Info on archive | |
echo "Files in archive: $(echo "$FILES" | wc -l | tr -d "[:blank:]" )" | |
echo "Archive mod-date: $YYYYMMDDhhmmss" | |
echo | |
## Loop through file list | |
echo "File order in archive will be baked into metadata of extracted files:" | |
echo "- Finder comment gets sequence number." | |
echo "- Timestamps will be created artificially:" | |
echo " - First file in archive gets same mod-date as archive." | |
echo " - Next file will be one second older than the previous file." | |
echo " - Mod-dates will also be written to creation-dates for ultimate persistence." | |
echo | |
echo "Extracting files..." | |
echo | |
echo "Nr\tUnix Date\tYYYY-MM-DD hh:mm:ss\tFile" | |
echo | |
echo "$FILES" | while read f | |
do | |
# Show progress | |
CREATIONSTAMP=$(date -jf "%s" "$UNIXSTAMP" +"%m/%d/%Y %H:%M:%S") | |
YYYYMMDDhhmmss=$(date -jf "%s" "$UNIXSTAMP" +"%Y-%m-%d %H:%M:%S") | |
echo "$COUNTER\t$UNIXSTAMP\t$YYYYMMDDhhmmss\t$f" | |
# Encode archive order into metadata | |
# Finder comment | |
osxmetadata -s findercomment "$COUNTER" "$BASE/$f" | |
# Date created, date modified, hide extension | |
SetFile -d "$CREATIONSTAMP" -m "$CREATIONSTAMP" -a E "$BASE/$f" | |
# touch -t $TOUCHSTAMP "$BASE/$f" # Date modified | |
# Adapt counters | |
COUNTER=$((COUNTER+1)) # No zero padding necessary. Finder sorts correctly. | |
UNIXSTAMP=$((UNIXSTAMP-1)) # Decrement b/c Finder icon view sorting by date is descending only | |
done | |
# Set artificial dates on extraction folder too | |
SetFile -d "$CREATIONSTAMP" -m "$CREATIONSTAMP" "$BASE/" | |
echo "\nSetting creation and modification date of output directory...\n" | |
echo "/\t$UNIXSTAMP\t$YYYYMMDDhhmmss\t$BASE/\n" | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment