Compress programs

6

Write a program/script that decompresses and passes any compressed data appended at the end of the binary/file as standard input to a program specified as arguments.

$ gzip --to-stdout input >> your_solution
$ your_solution program args...

should have the same effects as:

$ program args... < input

Alexandru

Posted 2011-01-28T16:10:54.370

Reputation: 5 485

Answers

7

zsh: (20 chars)

Edit: because zsh doesn't re-parse arguments like bash, I can lose the "s and it will still work with spaces in args

#!/usr/bin/zsh
tail -n+3 $0|zcat|$@

Bash (25 24 chars not including shebang line)

#!/bin/bash
tail -n+3 "$0"|zcat|"$@"

tobyodavies

Posted 2011-01-28T16:10:54.370

Reputation: 991

1Could -n +3 be changed to -n+3? – PleaseStand – 2011-01-31T23:13:09.803

@idealmachine yes, yes it could... (tho i had to test that :D) – tobyodavies – 2011-01-31T23:25:59.480

Also just realized that if i used zsh, I would be able to lose the "s and still have it work with spaces... – tobyodavies – 2011-01-31T23:27:34.907

2

C (251 chars)

Way too long to win, but I wanted to do one in a compiled language to make things interesting. (It turned out to be quite straightforward actually).

It's probably very compiler-dependent (I used GCC 4.4.3 with the default settings on 32-bit Linux)

#include<stdio.h>
#define q(x)FILE*x=x##open(
#define w strcpy(o+b
o,s=7462;char b[99];main(int i,char**a){q(f)*a,"r");while(--s)fgetc(f);
for(o=0;++s<i;o++[b]=32)w,a[s]),o+=strlen(a[s]);w,"|zcat");q(p)b,"w");
for(;~(i=fgetc(f));fputc(i,p));fclose(p);}

marinus

Posted 2011-01-28T16:10:54.370

Reputation: 30 224

1

Bash (OP solution)

This is an example I came up with:

cat $0 | awk 'NR > 1' | zcat | $* ; exit

Alexandru

Posted 2011-01-28T16:10:54.370

Reputation: 5 485

1this fails if there are spaces in any args – tobyodavies – 2011-01-28T16:35:43.520

Thanks for the info. I will leave it as such, your solution is better. – Alexandru – 2011-01-28T16:51:24.273