Apoorv Jain
Apoorv Jain

Reputation: 113

Writing data to a zip file

I have a script which I am running in the ubuntu terminal (bash) . Currently I am directly appending the output of the script to a file using the below command :

./run.sh > a.txt

But for some input files run.sh may produce output which is large in size without compression . Is it possible to write these output directly to a zip file without going through the dump file intermediate ? I know it is possible in Java and python . But I wanted a general method of doing it in the bash so that I could keep the run.sh same even if my running program is changing .

I have tried searching the web but haven't come across something useful .

Upvotes: 0

Views: 1848

Answers (3)

dash-o
dash-o

Reputation: 14452

The 'zip' format is for archiving. The 'zip' program can take an existing file and put compressed version into an archive. For example:

./run.sh > a.txt
zip a.zip a.txt

However, you question ask specifically for a 'streaming' solution (given file size). There are few utilities that use formats that are 'streaming-happy': gz, bz2, and xz. Each excel in different type of data, but for many cases, all will work.

./run.sh | gzip > a.txt.gz
./run.sh | bzip2 > a.txt.bz2
./run.sh | xz > a.txt.xz

If you are looking for widest compatibility, gzip is usually your friend.

Upvotes: 1

JUSHJUSH
JUSHJUSH

Reputation: 392

In bash you can use process substitution.

zip -FI -r file.zip <(./run.sh)

Upvotes: 0

Hans-Martin Mosner
Hans-Martin Mosner

Reputation: 846

In this case, a gzip file would be more appropriate. Unlike zip, which is an archive format, gzip is just a compressed data format and can easily be used in a pipe:

./run.sh | gzip > a.txt.gz

The resulting file can be uncompressed in place using the gunzip command (resulting in a file a.txt), viewed with zmore or listed with zcat which allows you to process the output with a filter without writing the whole decompressed file anywhere.

Upvotes: 1

Related Questions