Could zipfile module process the zip data in memory?
sjmachin at lexicon.net
Sun Apr 29 14:23:38 CEST 2007
On Apr 29, 9:51 pm, 人言落日是天涯，望极天涯不见家 <kelvin.... at gmail.com> wrote:
> On Apr 29, 7:37 pm, "Daniel Nogradi" <nogr... at gmail.com> wrote:
> > > 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
> Maybe my poor english makes you confusion:-). The client receive the
> zip file data from the server, and keep these data as a variable, not
> as a file in harddisk. such as "zipFileData", but the first argument
> of the "ZipFile" is filename. I would like to let the ZipFile() open
> the file from "zipFileData" directly but not file in harddisk
> zipp = ZipFile( this_is_the_zip_file_from_your_server, 'r' )
> ^ I don't have this file, all its data
> is in a variable.
Try something like this:
from zipfile import ZipFile
from StringIO import StringIO
filelikeobj = StringIO(zipFileData)
zipp = ZipFile(filelikeobj, 'r')
for name in zipp.namelist():
# etc etc etc
More information about the Python-list