Last active
December 15, 2021 17:57
-
-
Save porg/258bcd052d84c05c3ff430f9e90cb24c to your computer and use it in GitHub Desktop.
Encode the file order in an archive to the metadata of the decompressed 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-15 | |
# - Fixed base60 vs. base100 timestamp bug | |
# - Added more verbose script output | |
# Dependencies: p7zip, pipx, osxmetadata | |
# $ brew install p7zip | |
# $ brew install pipx | |
# $ pipx install osxmetadata | |
# 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") | |
TOUCHSTAMP=$(date -jf "%s" "$UNIXSTAMP" +"%Y%m%d%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 "Date Stamp: $DATESTAMP" | |
echo "Touch Stamp: $TOUCHSTAMP" | |
echo "Unix Stamp: $UNIXSTAMP" | |
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 "- Modification date will be created artificially" | |
echo " - First file in archive gets same mod-date as archive" | |
echo " - Next file each will be one second younger than the previous file" | |
echo | |
echo "Extracting files..." | |
echo | |
echo -e "Nr\tUnix Date\tYYYY-MM-DD hh:mm:ss\tFile" | |
echo | |
echo "$FILES" | while read f | |
do | |
# Show progress | |
TOUCHSTAMP=$(date -jf "%s" "$UNIXSTAMP" +"%Y%m%d%H%M.%S") | |
YYYYMMDDhhmmss=$(date -jf "%s" "$UNIXSTAMP" +"%Y-%m-%d %H:%M:%S") | |
echo -e "$COUNTER\t$UNIXSTAMP\t$YYYYMMDDhhmmss\t$f" | |
# Encode archive order into metadata | |
osxmetadata -s findercomment "$COUNTER" "$BASE/$f" # Finder comment | |
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 modified only descending | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment