Hi all,
Below is a shell script I have been writing on and off for a number of months. It's purpose is to reduce the space taken up by OpenDocument files, the format used by default by OOo and KOffice.
I'm not an expert programmer so any programming related suggestions would be welcome. I know some better error handling would help and some more loops rather than lists of commands.
Code:
#!/bin/bash
#######################################################################
# Author: Edward Holness #
# Date: 2006-04-24 #
# ScriptName: optimOD (Optimise OpenDocument) #
# Description: A script that optimises OpenDocument (and *.sx* files,#
# with modification) losslessly. It unzips the files, #
# optimises PNG and JPEG images, removes unneccessary #
# contents,then recompresses using higher compression #
# yet still being compatible with OpenDocument. #
# Requirements: 7za, unzip, advpng, advzip, optipng, jpegoptim, #
# jpegtran and xmllint in your path. #
# Command line instructions: sh optimod <filename> or add to your path#
# Known bugs: #
#######################################################################
if test -f $1 # test that file exists
then echo $1 found # confirmation of file existence
else
echo no file to optimise, exiting nicely. # warn that file doesn't exist and exit
exit
fi
unzip -oq $1 -d optimod_$1 # Extract the file
echo $1 extracted to optimod_$1
#### Pictures
advpng -z4 optimod_$1/Pictures/*.png
echo running optipng...
for zw in 32k 16k 8k 4k 2k 1k 512
do
optipng -q -zw$zw -zc1-9 -zm1-9 -zs0-3 -f0-5 optimod_$1/Pictures/*.png
echo optipng $zw complete
done
echo PNG graphics optimised
jpegoptim --strip-all --quiet optimod_$1/Pictures/*.jpg # Optimise JPEG's losslessly
## JPEG progressive
for a in `ls optimod_$1/Pictures/*.jpg`
do
jpegtran -progressive -outfile $a $a #.prog $a
done;
jpegoptim --strip-all --quiet optimod_$1/Pictures/*.jpg # Optimise JPEG's losslessly
jpegoptim --strip-all -m95 --quiet optimod_$1/Pictures/*.jpg # Cap JPEG's at m%
for a in `ls optimod_$1/Pictures/*.jpg`
do
jpegtran -progressive -outfile $a $a #.prog $a
done;
jpegoptim --strip-all --quiet optimod_$1/Pictures/*.jpg # Optimise JPEG's losslessly
echo JPEG graphics optimised
#### reduce contents, comment out for original content
rm optimod_$1/layout-cache
echo Removed layout-cache
rm optimod_$1/settings.xml
echo settings.xml removed
cd optimod_$1/
rm -rf Thumbnails/
echo Removed Thumbnails/
rm -rf Configurations2/
echo Removed Configurations2/
cd ../
#### content.xml
xmllint --nsclean --noblanks -o optimod_$1/content.xml.lint optimod_$1/content.xml
mv optimod_$1/content.xml.lint optimod_$1/content.xml
echo Compacted content.xml
#### styles.xml
xmllint --nsclean --noblanks -o optimod_$1/styles.xml.lint optimod_$1/styles.xml
mv optimod_$1/styles.xml.lint optimod_$1/styles.xml
echo Compacted styles.xml
#### meta.xml
xmllint --nsclean --noblanks -o optimod_$1/meta.xml.lint optimod_$1/meta.xml
mv optimod_$1/meta.xml.lint optimod_$1/meta.xml
echo Compacted meta.xml
#### manifest.xml
xmllint --nsclean --noblanks -o optimod_$1/META-INF/manifest.xml.lint optimod_$1/META-INF/manifest.xml
mv optimod_$1/META-INF/manifest.xml.lint optimod_$1/META-INF/manifest.xml
echo Compacted manifest.xml
#### Compress
cd optimod_$1/
zip -rq9DX ../$1.pass0 *
advzip ../$1.pass0 -z4
for (( pass = 4 ; pass <= 15; pass++ ))
do
for (( fb = 170 ; fb <= 258; fb++ ))
do
7za a -tzip -mfb$fb -mpass$pass ../$1.pass$pass.$fb * #-mx$x
#advzip ../$1.pass$pass.$fb -z4
done
done
for b in `ls -Sr ../$1.pass*`
do
mv $b ../$1
echo Compressed
#### Clear up the junk
rm -rf *
cd ../
rmdir optimod_$1/
rm $1.pass*
exit
done