Created
November 1, 2016 23:12
-
-
Save enderandpeter/461890ea6361e2085477d0b23008c264 to your computer and use it in GitHub Desktop.
An experimental script for using archiving programs to extract contents into uniquely named 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 | |
# This script's use is largely overshadowed by 7-zip | |
# 7z e Archives*.zip -aot -o"Destination Folder" # Rename duplicate filenames | |
# 7z e Archives*.zip -aoa -o"Destination Folder" # Overwrite duplicate filenames | |
# 7z e Archives*.zip -aos -o"Destination Folder" # Skip duplicate filenames | |
# It should be possible to use `7z e Archives*.zip -aos -o"Destination Folder" *.* -r` to avoid | |
# extracting empty directories, but any such filter, such as *.* or even *, results in no files extracted on Windows. | |
filecount=0 | |
for archive in $*; do | |
if [ "$archive" = "$1" ]; then | |
DESTINATIONFOLDER="$1" | |
if [ ! -d "$DESTINATIONFOLDER" ]; then | |
if ! mkdir "$DESTINATIONFOLDER"; then | |
echo "Could not make directory at $DESTINATIONFOLDER" | |
exit | |
fi | |
fi | |
FULLDESTINATIONPATH=$(realpath "$DESTINATIONFOLDER") | |
continue | |
fi | |
if [ -e "$archive" ]; then | |
archiveextension=$([[ "$archive" = *.* ]] && echo ".${archive##*.}" || echo '') | |
case "$archive" in | |
.zip) | |
photos=$(unzip -Z1 "$archive") | |
;; | |
.rar) | |
photos=$(unrar -lb "$archive") | |
;; | |
*) | |
echo Cannot list files from "$archive" of unknown file type | |
exit | |
;; | |
esac | |
for photo in $photos; do | |
filename="${photo%.*}" | |
extension=$([[ "$photo" = *.* ]] && echo ".${photo##*.}" || echo '') | |
writepath=$(echo $(mktemp -u --tmpdir="$FULLDESTINATIONPATH" --suffix=$extension | sed -nr "s/tmp.(.+)/\1/p")) | |
case "$archive" in | |
.zip) | |
if ! unzip -jp "$archive" "$photo" > "$writepath"; then | |
echo Could not save $photo in $archive to $writepath | |
fi | |
;; | |
.rar) | |
if ! unrar p -inul "$archive" "$photo" > "$writepath"; then | |
echo Could not save $photo in $archive to $writepath | |
fi | |
;; | |
*) | |
echo Cannot extract "$archive" of unknown file type | |
exit | |
;; | |
esac | |
filecount=$(($filecount + 1)) | |
done | |
else | |
echo Could not find archive $archive | |
fi | |
#unrar e "$archive" "$extractFolder" | |
#echo $archive | |
done | |
echo Added $filecount files to $FULLDESTINATIONPATH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment