Could zipfile module process the zip data in memory?

Daniel Nogradi nogradi at gmail.com
Sun Apr 29 07:37:30 EDT 2007


> I made a C/S network program, the client receive the zip file from the
> server, and read the data into a variable. how could I process the
> zipfile directly without saving it into file.
> In the document of the zipfile module, I note that it mentions the
> file-like object? what does it mean?
>
> class ZipFile( file[, mode[, compression[, allowZip64]]])
>          Open a ZIP file, where file can be either a path to a file (a
> string) or a file-like object.

Yes it is possible to process the content of the zipfile without
saving every file:

[untested]

        from zipfile import ZipFile
        from StringIO import StringIO

        zipp = ZipFile( this_is_the_zip_file_from_your_server, 'r' )
        for name in zipp.namelist( ):
                content = zipp.read( name )
                s = StringIO( )
                s.write( content )
                # now the file 'name' is in 's' (in memory)
                # you can process it further
                # ............
                s.close( )
        zipp.close( )


HTH,
Daniel



More information about the Python-list mailing list