-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifcz.sh
167 lines (132 loc) · 4.55 KB
/
ifcz.sh
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
#! /bin/bash
scriptversion="trunk"
scriptlastedit="20180114"
scriptauthor="John Pyper"
scriptsite="https://github.com/jpyper/bash-scripts"
#############
### ABOUT ###
#############
# This simple script compresses individual files with the same extension. It
# is useful for compressing emulator ROMs or PDFs.
#
# I was inspired to write this script based on a utility I used for Windows(tm)
# quite some time ago called ZipOne. It was/is a command line utility that was
# designed to do the same thing this script does.
####################
### REQUIREMENTS ###
####################
# ls,bc,p7zip-full
# OPTIONAL:
# (work in progress, not in listed order) gzip,p7zip,xz,rar,lzma,arj,...etc
###############
### LICENSE ###
###############
# This script is licensed under the MIT License.
# Details here: http://opensource.org/licenses/MIT
############################
### CONFIGURABLE OPTIONS ###
############################
# change this to where you want your compressed files to go.
outdir="."
#########################
### MAGIC INGREDIENTS ###
#########################
# start with a clean screen
clear
# set variables for $1 and $2 from the command line
filemask="$1"
#comptype="$2"
# check command line options, display help or run section.
#if "$2" ; then
# case "$2" in
# zip*) compexe="zip"
# compopt="--quiet --move -9"
# compext="zip" ;;
#
# gzip*) compexe="gzip"
# compopt="--quiet --best"
# compext="gz" ;;
#
# *) echo "[!] Unknown compression format."
# exit 1 ;;
# esac
#else
# echo "[*] Defaulting to ZIP compression format."
# compexe="zip"
# compopt="--quiet --move -9"
# compext="zip"
#fi
# TEMPORARY
#compexe="7z"
#compcom="-bb0 -bd -bso0 -bse2 -bsp0"
#compzip="-tzip -mx=9 -mfb=258 -mpass=15 -mm=Deflate -mmt=on"
#compopt="${compcom} ${compzip}"
compexe="zip"
compopt="--quiet -9"
compext="zip"
# Check if [outdir] exists, else make it.
if [ ! -d "${outdir}" ]; then
echo "[!] Can't find OUTPUT directory: $outdir"
echo "[!] Making directory."
mkdir -p "${outdir}"
if [ ${?} -gt 0 ]; then
echo "[!] There was a problem creating the directory."
echo "[!] Make sure you have write permissions to the directory specified."
echo
exit 1
fi
fi
# display script header
echo
echo "+-----------------------------------------------"
echo "| Individual File Compressor"
echo "| version: ${scriptversion} (${scriptlastedit})"
echo "| by: ${scriptauthor}"
echo "| web: ${scriptsite}"
echo
# get number of files. initialize counters.
filetotal=$(find . -name "*.${filemask}" | wc -l)
filetotalorigsizeb="0"
filetotalorigsizeh="0"
filetotalcompsizeb="0"
filetotalcompsizeh="0"
# conversion process...
echo "+--------------------+"
echo "| Compressing files. |"
echo "| Please wait... |"
echo "+--------------------+"
echo " * Compression: ${compext}"
echo " * File Ext: .${filemask}"
echo
for a in *."${filemask}"
do
# current file being worked with
fileorigname="${a}"
echo " FILE: ${a}"
# get byte and human readable original file size
fileorigsizeb=$(ls -l "${a}" | cut -d " " -f 5)
fileorigsizeh="$(echo "scale=3; x=${fileorigsizeb} / 1048576; if(x<1) print 0; x" | bc) MB"
echo " ORIG: ${fileorigsizeb} (${fileorigsizeh})"
### # compression command
#${compexe} a ${compopt} "${a}.${compext}" "${a}" > /dev/null
${compexe} ${compopt} "${a}.${compext}" "${a}" > /dev/null
# get byte and human readable compressed file size
filecompsizeb=$(ls -l "${fileorigname}.${compext}" | cut -d " " -f 5)
filecompsizeh="$(echo "scale=3; x=${filecompsizeb} / 1048576; if(x<1) print 0; x" | bc) MB"
echo " COMP: ${filecompsizeb} (${filecompsizeh})"
# echo "FILE SIZE: ${fileorigsizeb} (${fileorigsizeh}) --> ${filecompsizeb} (${filecompsizeh})"
# add compression ratio to each block
echo "RATIO: $(echo "scale=2; x=${filecompsizeb} / ${fileorigsizeb}; if(x<1) print 0; x" | bc | cut -d "." -f 2) %"
# add file size in bytes to variables for end display stats
filetotalorigsizeb=$((${filetotalorigsizeb} + ${fileorigsizeb}))
filetotalcompsizeb=$((${filetotalcompsizeb} + ${filecompsizeb}))
echo
done
echo; echo; echo
# display some stats before script ends
echo " Files Processed: ${filetotal}"
echo " Compression Method: ${compext}"
echo " Original File Space: ${filetotalorigsizeb} ($(echo "scale=3; x=${filetotalorigsizeb} / 1048576; if(x<1) print 0; x" | bc) MB)"
echo "Compressed File Space: ${filetotalcompsizeb} ($(echo "scale=3; x=${filetotalcompsizeb} / 1048576; if(x<1) print 0; x" | bc) MB)"
echo " Compression Ratio: $(echo "scale=2; x=${filetotalcompsizeb} / ${filetotalorigsizeb}; if(x<1) print 0; x" | bc | cut -d "." -f 2) %"
echo