LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Blogs > rainbowsally
User Name
Password

Notices

Rate this Entry

Create a self-extracting archive

Posted 02-16-2012 at 05:45 AM by rainbowsally
Updated 02-23-2012 at 07:50 AM by rainbowsally

CHANGELOG Feb 22, 2012
Changed the temp dir from /tmp to $HOME/tmp so everyone has write and execute perms in it. Added more cleanups including removal of the temp dir unless the user already had it and was using it.


Features:
  • Embedding binary data in a shell script.
  • Writing a program with a program.
  • Various useful snippets even if the app doesn't turn you on.

Requires:
  • base64 (encoder in core utils)
  • tar (core utils)
  • xz (lzma stuff, real old linux may not have it)
What this does is creates a self extracting archive, probably smaller than tar.gz that runs when you execute it on the commandline.

Create it and type new.sfxz --help for more info.

If you try to run a *.sfxz file by clicking on it, it will issue an error msg. xmessage is funky, but ubiquitous and at least functional.

file: new.sfxz (executable)
purpose: snippet collection if nothing else
Code:
#!/bin/sh
# base64 encoded xz-lzma compressed self extracting 
# file creator
# requires tar, xz (lzma) compression

############################
# usage check

if [ "$1" == "" ] || [ "$1" == "--help" ]; then
echo "
  Usage new.sfxz <filename | dirname>
  Creates self-extracting file named <filename>.sfxz
  Requires xz (lzma) at both ends of operation.

  Uses tar so expect similar path issues.

  If a post extraction script named 'post.sh' exists
  it will be run at extraction time.  It should assume the 
  extracted file(s) are in $HOME/tmp/sfxz when it runs.

  The default post extraction action is to move the 
  extracted file or dir from the $HOME/tmp/sfxz directory 
  to the current working directory.
 "
exit
fi

filename=`basename $1`
srcdir=`dirname $1`
post="post-extract"


############################
# compress xz

if ! tar -caf $filename.xz $srcdir/$filename >/dev/null; then
  echo "
  Can't convert '$srcdir/$filename' to self extractor"
  exit 1
fi


############################
# write output file header

cat << _EOF > $filename.sfxz
#!/bin/sh
# base64 self extractor
# file: $filename

filename=$filename

# don't allow user to click on the file
if ! tty >/dev/null; then
  xmessage -center "
  \$(basename \$0)
  must be run in a terminal 
  "
  exit
fi

# create and/or clear out tmp dir
mkdir -p \$HOME/tmp/sfxz
rm -rf \$HOME/tmp/sfxz/*
base64 -d << _BEOF >\$HOME/tmp/sfxz/\$filename.xz
_EOF


############################
# write output file footer
# with renamed post extraction
# script

base64 $filename.xz >> $filename.sfxz
echo "_BEOF

(cd \$HOME/tmp/sfxz && tar -xaf $filename.xz)
rm -f \$HOME/tmp/sfxz/$filename.xz
 " >> $filename.sfxz

if [ -e post.sh ]; then
  cp post.sh $post
else
cat << _EOF > $post
#!/bin/sh

is_yes() # returns OK if first char is Y or y
{
  local key=\`echo \$1 | cut -b1 | sed 's/y/Y/; /Y/!d'\`
  [ "\$key" == 'Y' ] && return 0 # true
  return 1 # false
}

if [ -e $filename ]; then
  printf "
  Overwrite existing file(s)? [N/y]:"
  read key
  if ! is_yes \$key ;then
  echo "Aborting.."
    exit 0
  fi
fi

mv \$HOME/tmp/sfxz/$filename .
_EOF
fi


echo "cat << _BEOF > \$HOME/tmp/sfxz/$post" >> $filename.sfxz
cat $post >> $filename.sfxz
echo "_BEOF

sh \$HOME/tmp/sfxz/$post
rm -rf \$HOME/tmp/sfxz/*
rmdir \$HOME/tmp/sfxz 2>/dev/null || true
rmdir \$HOME/tmp 2>/dev/null || true
 " >> $filename.sfxz


############################
# cleanup

rm -f $filename.xz
rm -f $post

############################
# make executable

chmod +x $filename.sfxz
Much of this code is counterintuitive. It will make more sense after you have created one self-extractor. You can open a self-extracting archive in an editor and see where the innards came from and why the code in the extractor creator looks a little... "odd", shall we say?

The problem with this self extractor is that it's not a stand-alone app because some of its dependencies are not standard on every linux.

A gzipping self extractor would be though. But compression gains would be reduced by the base64 compression scheme which increases the size of the binary data by about 30% in conversion to printable data.

See early blog posts (like page 1 or 2) for how and why to put this stuff in a 'sandbox'. These things in the entries for "kde utils", but most of them are not dependent on kde. In fact kde has done a great job of trying to break them -- but they all work again (at least in openSUSE 11.4).

:-)
Posted in Uncategorized
Views 1582 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 11:19 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration