-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2xz
More file actions
executable file
·255 lines (220 loc) · 11.2 KB
/
2xz
File metadata and controls
executable file
·255 lines (220 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
#--------------------------------------------------------------------------
#
# ▞▀▖
# ▗▘▚▗▘▀▜▘
# ▗▘ ▗▚ ▗▘
# ▀▀▘▘ ▘▀▀▘
#
# Description:
# (Re)compresses everything to .xz or .tar.xz.
#
#-------------------------------------------------------------------------
#
# Found this useful? Appalling? Appealing? Please let me know.
# The Unabashed welcomes your impressions.
#
# You will find the
# unabashed
# at the location opposite to
# moc • thgimliam
#
#-------------------------------------------------------------------------
#
# License:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
#--------------------------------------------------------------------------
# Program name from its filename
prog=${0##*/}
# Some colors
LIGHTRED='\e[1;31m'
LIGHTPURPLE='\e[1;35m'
YELLOW='\e[1;33m'
NC='\e[0m'
# Calling for help is the same as calling without arguments.
case $1 in --help|-[h?]) $0; exit 1 ;; esac
# Usage if argument isn't given
[[ $# -eq 0 ]] && {
clear
echo -e "
${LIGHTRED}Description:${NC}
Converts all (well, a lot of) archive types into xz or tar.xz files.
Why do that? Because .xz compresses fairly better than .zip or .gz. I've seen a file.tar.gz
shrink from 32Mb to 8Mb when converted to .tar.xz — and that is because of the smart
way xz handles compressing when many of the files to be compressed are similar in contents.
For more, check:
${LIGHTPURPLE}https://en.wikipedia.org/wiki/Xz${NC} and ${LIGHTPURPLE}http://tukaani.org/xz/${NC}
Below: supported extensions to convert. Other files will be xz'd. Dirs will be tar.xz'd.
${YELLOW}.7z .ar .bz2 .cb7 .cbr .cbt .cbz .gz .jar .lz .lzma .lzo .rar .tar .tar.bz2
${YELLOW}.tar.7z .tar.gz .tar.lz .tar.lzma .tar.lzo .tbz .tbz2 .tgz .tlz .tzo .Z .zip
${YELLOW}.xz${LIGHTPURPLE}* ${YELLOW}.tar.xz${LIGHTPURPLE}* ${YELLOW}.txz${LIGHTPURPLE}*${NC} (${LIGHTPURPLE}*${NC} yep, we recompress them, to ensure maximum compression)
${LIGHTRED}Usage and examples:${NC}
${YELLOW}$prog [-n] <files>${NC}
${YELLOW}$prog f1.jar f2.zip f3.tgz${NC} # Converts those three, and backs up originals at ./OldArchives
${YELLOW}$prog -n *.tar.gz${NC} # Converts all .tar.gz and deletes the originals.
${LIGHTRED}Dependencies:${NC}
Must have installed {unrar-free, lzip, lzop, p7zip-full, gzip, bzip2}
if you want to extract, respectively: {.rar, .lzip, .lzop, .lzma, .7z, .gz, .bz2}
${YELLOW}sudo apt-get install xz-utils unrar-free lzip lzop p7zip-full gzip bzip2${NC}
${LIGHTRED}Limitations (not-exhaustive):${NC}
- Recognizes filetype by extension, not by ${YELLOW}/usr/share/file/magic${NC}
- Does not handle password protection.
- Limit cases not thoroughly tested. (Filenames with crazy characters, nested archives etc.)
- Conversion of filenames with spaces not thoroughly tested; be cautious.
I recommend ${YELLOW}detoxing${NC} those files. (${YELLOW}sudo apt-get install detox${NC})
${LIGHTRED}Final notes:${NC}
1) To extract a .xz, run: ${YELLOW}xz -d MYFILE.xz${NC}. To extract a .tar.xz, run: ${YELLOW}tar xvJf MYFILE.tar.xz${NC}
2) Note that unrar-free fails often. If you have only a few .rar, I'd suggest extracting them
by hand with Peazip, and then recompressing the files normally to .tar.xz.
3) USE IT AT YOUR OWN RISK. I wrote this carefully — but I suggest you back up your input files
before using ${YELLOW}$prog${NC}, as I may have overlooked something critical.
Don't blame me if you run ${YELLOW}$prog${NC} and your archive gets warped or your cat microwaved.
But tell me about it, or fix it, so we can avoid other people's cat from being microwaved.
" | more
exit 1
}
# TODO: more error handling (e.g.: if mktemp or achive extraction fails)
# TODO: add other formats not yet covered
# TODO: frill: add a flag option to a pv pipe to show progress bar when
# compressing stuff. I would need to use shopt to handle flags in this case.
#----------------------------------------------------------------------------
# Check for missing commands
needed_commands="sed xz tar"
missing_counter=0
for needed_command in $needed_commands; do
if ! hash "$needed_command" >/dev/null 2>&1; then
printf "Command not found in PATH: %s\n" "$needed_command" >&2
: $((missing_counter++))
fi
done
if [[ $missing_counter -eq 1 ]]; then
printf "At least %d command is missing, install it\n" "$missing_counter" >&2
exit 1
elif [[ $missing_counter -gt 1 ]]; then
printf "At least %d commands are missing, install them\n" "$missing_counter" >&2
exit 2
fi
#----------------------------------------------------------------------------
mydir="$PWD"
oldarchivesdir="$mydir/OldArchives"
# Environment flag: defaults xz compression to maximum
export XZ_OPT=${XZ_OPT:-"-9"}
# Initialize variable defining if original file was .xz, .tar.xz or .txz
originalWasXZ=0 # defaults to not
# Create flag to indicate whether or not to back up originals. Default is yes.
case $1 in
-n|--no-backup)
backup=0
shift
;;
*)
backup=1
;;
esac
find_extension ()
# Gets the full extension of the file.
# Well, for our scope, it means it includes the .tar as part of the extension
# in a file.tar.gz case (extension = tar.gz, not only gz).
# Deals well with the file.with.dots.tar.bz2 scenario...
# Maybe TODO: if file has no extension, execute `file` and grep from there.
# (For that, create a separate program to add extension based on `file` results)
{
input=$1
ext1=$(basename "$input" | sed 's/.*\.//')
[[ "$ext1" = "$input" ]] && ext1=""
rest=$(echo "$input" | sed 's|\.[^.]*$||')
[[ "$(echo "$rest" | sed 's/.*\(....\)$/\1/')" = ".tar" ]] && ext2="tar."
echo "${ext2}${ext1}"
}
# Process each argument
for argument in "$@"; do
canonical=$(readlink --canonicalize "${argument}")
extension=$(find_extension "$canonical")
basename=$(basename "$canonical" | sed "s/\.${extension}$//")
pathbasename=$(echo "$canonical" | sed "s/\.${extension}$//") # full path; important if symlink
# Removed the message. Unnecessary. Silence is golden:
# echo -e ${YELLOW}"\n== Converting ${basename}.${extension} to xz format... ==\n"${NC}
# Is it a directory? tar.xz it. The original directory will be kept.
if [[ -d "$canonical" ]]; then
(cd "$canonical" && tar --create --atime-preserve * | xz - > "$canonical.tar.xz")
# Oh, it's a file then? Very well.
elif [[ -f "$canonical" ]]; then
case "$canonical" in
# Multiple-file extraction; must use $mytempdir
*.zip | *.jar | *.7z | *.rar | *.ar | *.cbz | *.cb7 | *.cbr)
mytempdir=$(mktemp --tmpdir="$TMPDIR" -d ${prog}.XXXXXXXXXX)
cd "$mytempdir"
case "$canonical" in
*.zip | *.jar | *.cbz) unzip -q -d . "$canonical" ;;
*.7z | *.cb7) 7z -o. x "$canonical" ;;
*.rar | *.cbr) unrar x "$canonical" ;;
*.ar) ar x "$canonical" ;;
*) echo "Can't happen..." ;;
esac
tar --create --atime-preserve . | xz - > "${pathbasename}.tar.xz"
# IF not error in any of the pipes of previous command AND backup flag is off
# THEN delete original file.
[[ $(echo ${PIPESTATUS[@]}) =~ 0 ]] && [[ $backup = 0 ]] && rm --force "$canonical"
[[ $backup = 1 ]] && mkdir -p "$oldarchivesdir" && mv "$canonical" "${oldarchivesdir}/"
rm --recursive --force $mytempdir
cd "$mydir"
;;
# Single-file extraction (we won't "untar")
*.tar.* | *.tgz | *.tbz | *.tbz2 | *.tzo | *.tlz | *.txz)
case "$canonical" in
*.tar.gz | *.tgz | *.tar.Z) gunzip -c "$canonical" | xz - > "${pathbasename}.tar.xz" ;;
*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) bunzip2 -c "$canonical" | xz - > "${pathbasename}.tar.xz" ;;
*.tar.lzma) unlzma -c "$canonical" | xz - > "${pathbasename}.tar.xz" ;;
*.tar.lzo | .tzo) lzop --decompress -c "$canonical" | xz - > "${pathbasename}.tar.xz" ;;
*.tar.lz |.tlz) lzip --keep --decompress -c "$canonical" | xz - > "${pathbasename}.tar.xz" ;;
*.tar.xz |*.txz) xz -d -c "$canonical" | xz - > "${pathbasename}.tx" &&
mv "${pathbasename}.tx" "${pathbasename}.tar.xz" &&
originalWasXZ=1 ;;
esac
[[ $(echo ${PIPESTATUS[@]}) =~ 0 ]] && [[ $backup = 0 ]] && rm --force "$canonical"
[[ $originalWasXZ = 1 ]] || {
[[ $backup = 1 ]] &&
mkdir -p "$oldarchivesdir" &&
mv "$canonical" "${oldarchivesdir}/"
}
;;
*.gz | *.Z | *.bz | *.bz2 | *.lzma | *.lzo | *.lz | *.xz)
case "$canonical" in
*.gz | *.Z) gunzip -c "$canonical" | xz - > "${pathbasename}.xz" ;;
*.bz | *.bz2) bunzip2 -c "$canonical" | xz - > "${pathbasename}.xz" ;;
*.lzma) unlzma -c "$canonical" | xz - > "${pathbasename}.xz" ;;
*.lzo) lzop --decompress -c "$canonical" | xz - > "${pathbasename}.xz" ;;
*.lz) lzip --keep --decompress -c "$canonical" | xz - > "${pathbasename}.xz" ;;
*.xz) xz -d -c "$canonical" | xz - > "${pathbasename}.x" &&
mv "${pathbasename}.x" "${pathbasename}.xz" &&
originalWasXZ=1 ;;
esac
[[ $(echo ${PIPESTATUS[@]}) =~ 0 ]] && [[ $backup = 0 ]] && rm --force "$canonical"
[[ $originalWasXZ = 1 ]] || {
[[ $backup = 1 ]] &&
mkdir -p "$oldarchivesdir" &&
mv "$canonical" "${oldarchivesdir}/"
}
;;
# All else, including *.tar, *.cbt and non-archives, etc: just xz them individually
# No backup is made here, even without adding the -n flag.
*) xz "$canonical" ;;
esac
else
echo >&2 "'$1' does not exist"
exit 1
fi
done
#------------------ END of PROGRAM ------------------#