2

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.

Konstantin
  • 2,983
  • 3
  • 33
  • 55

1 Answers1

0

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
Christoph Sommer
  • 6,893
  • 1
  • 17
  • 35
  • Thx, meanwhile I managed to find out a faster solution: "curl http://example.com/archive.zip|bsdtar -cf - @- |bsdtar -c -f - newfile.txt @-|bsdtar -cf - --format zip @- |pv" It first converts the zip stream to tar stream, add new file(s) and then convert the stream back to zip format. However I don't know how to add more than one file at the same time, unless repeating the 3rd part of this construction. – Konstantin Jul 16 '15 at 00:02
  • 1
    even shorter: "curl http://example.com/archive.zip|bsdtar --format zip -c -f - newfile.txt @- | pv" – Konstantin Jul 16 '15 at 00:10