Search code examples
pythonzipstringioplpython

In Python, how to write back a stringIO text file to a zip archive, then back to bytea field in PostgreSQL?


A relative noob to Python, I've successfully pulled a text file, out of a zip archive, contained in a PostgreSQL bytea field, using this code:

myzip = ZipFile(StringIO(rv[0]["archivefield"]), 'a')
data = myzip.read("content.txt",'a')

# *** WORK ON content.txt HAPPENS HERE ***

I've then done the work I need on that text file... So far, so good!

Now, though, am a bit confused on syntax to get the content.txt zipped back into its correct place in the archive, then written back to 'archivefield'.

Could any of you experts here suggest the little bit of syntax I'd need? Almost there!

Well, thanks to help received here - we're now certainly writing back to 'archivefield' - this is great!

Issues now are:

1) I'm apparently doubling the size of the original field. Is this a function of 'append' mode? How to 'wind back to zero and rewrite over the field? - and/or -

2) data I'm writing back doesn't encode back into hex(?) mode as the original data appeared to be - and would be standard for v9 of PostgreSQL. - OR -

Is the problem that the data is not being zipped again, hence its large size?


Solution

  • Use ZipFile.writestr:

    strf = StringIO(rv[0]["archivefield"])
    zipf = ZipFile(strf, "a")
    data = myzip.read("content.txt"')
    
    # process data, changing its value
    
    zipf.writestr("content.txt", data)
    zipf.close()  # actually write contents out
    

    then write str.getvalue() back to your PostgreSQL database, overwriting the previous value at rv[0]["archivefield"]. I can't help you with that, I'm afraid.