{"id":161,"date":"2019-01-28T15:40:10","date_gmt":"2019-01-28T15:40:10","guid":{"rendered":"https:\/\/privateblog.nobytesleft.net\/?p=161"},"modified":"2019-01-28T15:41:15","modified_gmt":"2019-01-28T15:41:15","slug":"a-self-extracting-tar-archive","status":"publish","type":"post","link":"https:\/\/privateblog.nobytesleft.net\/?p=161","title":{"rendered":"A self-extracting tar archive"},"content":{"rendered":"<p>If you want to create a self-extracting archive, or a self-contained executable, you may create a shell script which contains packed data, and can extract those data automatically. You proceed as follows.<\/p>\n<h5>Creating the archive<\/h5>\n<p>The archive is created with name $TGZ_NAME, containing the files $FILES_TO_PACK<\/p>\n<pre class=\"\">$ tar czf $TGZ_NAME $FILES_TO_PACK<\/pre>\n<h5>Creating the installer script<\/h5>\n<p>The installer script is a normal bash script, which can access the packed data in a special way.<\/p>\n<pre class=\"\">BUILTIN_PACKAGE_SEARCH_RES=$(grep -hnam1 '^TGZFILE_DATA:[^:]\\+:$' $0)\r\nif [ $? -eq 0 ]; then\r\n\u00a0\u00a0\u00a0 BUILTIN_PACKAGE_LINE=$(echo \"$BUILTIN_PACKAGE_SEARCH_RES\" | cut -d ':' -f 1)\r\n\u00a0\u00a0\u00a0 BUILTIN_PACKAGE_NAME=$(echo \"$BUILTIN_PACKAGE_SEARCH_RES\" | cut -d ':' -f 3)\r\nelse\r\n\u00a0\u00a0\u00a0 # error\r\nfi<\/pre>\n<p>We have also recovered the original package name, which can contain useful information.<\/p>\n<p>The package is then unpacked with:<\/p>\n<pre class=\"\">START_LINE=$(($BUILTIN_PACKAGE_LINE + 1))\r\ntail -n +$START_LINE $0| tar xzvf - -C \"$DEST_DIR\" &gt;&gt; \"$LOG_FILE_NAME\" 2&gt;&amp;1<\/pre>\n<p>Where $DEST_DIR is a destination directory (existing &#8211; it may be a temporary directory or something else) and $LOG_FILE_NAME is a log file for the result of the unpacking. We can avoid it with:<\/p>\n<pre class=\"\">tail -n +$START_LINE $0| tar xzvf - -C \"$DEST_DIR\" &gt;&gt; \"\/dev\/null\" 2&gt;&amp;1<\/pre>\n<p>At the end you may also self-destruct the script with:<\/p>\n<pre class=\"\">rm $0<\/pre>\n<h5>Packaging everything<\/h5>\n<p>Assume that the installer script is called script.sh &#8211; any name is ok since it won&#8217;t appear in the final product.<\/p>\n<p>$SH_NAME is the name of the resulting package.<\/p>\n<pre class=\"\">$ cat script.sh &gt; $SH_NAME$ cat \"TGZFILE_DATA:$TGZ_NAME:\" &gt;&gt; $SH_NAME$ cat $TGZ_NAME &gt;&gt; $SH_NAME\r\n$ chmod +x $SH_NAME<\/pre>\n<p>At the end, $SH_NAME will be an executable script, able to access the packaged data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to create a self-extracting archive, or a self-contained executable, you may create a shell script which contains packed data, and can extract those data automatically. You proceed&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=161"}],"version-history":[{"count":2,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions"}],"predecessor-version":[{"id":163,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions\/163"}],"wp:attachment":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}