Konstantin
Konstantin

Reputation: 3123

Is it possible to add a file to a zip stream?

Let's suppose I download a zip archive, and I mean something like adding some file on the fly to the data stream, avoiding usage of a temp file:

wget http://example.com/archive.zip -O - | zipadder -f myfile.txt | pv

I read somewhere that bsdtar can manipulate such streams.

Upvotes: 2

Views: 359

Answers (1)

Christoph Sommer
Christoph Sommer

Reputation: 6943

This will likely be hard on RAM, as it needs you to manipulate the zip structure entirely in memory. That said, it should be possible to write zipadder in python, using StringIO to manipulate a memory-backed file-like object read from stdin like so:

#!/usr/bin/env python

import zipfile
import sys
import StringIO

s = StringIO.StringIO(sys.stdin.read())  # read buffer from stdin
f = zipfile.ZipFile(s, 'a')
f.write('myfile.txt')  # add file to buffer
f.close()
print s.getvalue()  # write buffer to stdout

Upvotes: 0

Related Questions