By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Could zipfile module process the zip data in memory?

P: n/a
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.

Thanks!

Apr 29 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Apr 29, 9:15 pm, 人言落日是天涯,望极天涯不见家 <kelvin....@gmail.comwrote:
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.
A file-like object is an object that is not a file object, but behaves
like a file object. Instead of keeping the data on disk, it will use
Python objects (e.g. str or maybe array.array) or malloc its own
memory. Have a look at the StringIO module (pure Python) and the
similar but faster cStringIO module.

Regards,
John


Apr 29 '07 #2

P: n/a
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
Apr 29 '07 #3

P: n/a
On Apr 29, 7:37*pm, "Daniel Nogradi" <nogr...@gmail.comwrote:
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 processit further
* * * * * * * * # ............
* * * * * * * * s.close( )
* * * * zipp.close( )

HTH,
Daniel
Thanks!
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.

Apr 29 '07 #4

P: n/a
人言落日是天涯,望极天涯不见家 schrieb:
On Apr 29, 7:37 pm, "Daniel Nogradi" <nogr...@gmail.comwrote:
>>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
Thanks!
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.
You can use cStringIO for that as well. Read the module docs for it.

Diez
Apr 29 '07 #5

P: n/a
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
Thanks!
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.
Well, as you correctly pointed out in your original post ZipFile can
receive a filename or a file-like object. If the zip archive data is
in zipFileData then you might do:

from StringIO import StringIO
from zipfile import ZipFile

data = StringIO( )
data.write( zipFileData )
data.close( )

zipp = ZipFile( data )
..........
and continue in the same way as before.

Daniel
Apr 29 '07 #6

P: n/a
On Apr 29, 8:14*pm, "Daniel Nogradi" <nogr...@gmail.comwrote:
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
Thanks!
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.

Well, as you correctly pointed out in your original post ZipFile can
receive a filename or a file-like object. If the zip archive data is
in zipFileData then you might do:

from StringIO import StringIO
from zipfile import ZipFile

data = StringIO( )
data.write( zipFileData )
data.close( )

zipp = ZipFile( data )
.........

and continue in the same way as before.

Daniel- Hide quoted text -

- Show quoted text -
Thanks all of your kindly help!

Apr 29 '07 #7

P: n/a
On Apr 29, 9:51 pm, 人言落日是天涯,望极天涯不见家 <kelvin....@gmail.comwrote:
On Apr 29, 7:37 pm, "Daniel Nogradi" <nogr...@gmail.comwrote:
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

Thanks!
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

Apr 29 '07 #8

P: n/a
On Apr 29, 10:14 pm, "Daniel Nogradi" <nogr...@gmail.comwrote:
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
Thanks!
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.

Well, as you correctly pointed out in your original post ZipFile can
receive a filename or a file-like object. If the zip archive data is
in zipFileData then you might do:

from StringIO import StringIO
from zipfile import ZipFile

data = StringIO( )
data.write( zipFileData )
data.close( )
Even if that worked, it would be a long-winded way to do it. Please
contemplate the docs:

"""getvalue( )

Retrieve the entire contents of the ``file'' at any time before the
StringIO object's close() method is called.[snip note re mixing str &
unicode]

close( )

Free the memory buffer. """

The short working way is: """class StringIO( [buffer])

When a StringIO object is created, it can be initialized to an
existing string by passing the string to the constructor."""
Apr 29 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.