473,545 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Could zipfile module process the zip data in memory?

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
8 3932
On Apr 29, 9:15 pm, 人言落日是 天涯,望极 天涯不见家 <kelvin....@gma il.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
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
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 "zipFileDat a", but the first argument
of the "ZipFile" is filename. I would like to let the ZipFile() open
the file from "zipFileDat a" 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
人言落日是 天涯,望极 天涯不见家 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 "zipFileDat a", but the first argument
of the "ZipFile" is filename. I would like to let the ZipFile() open
the file from "zipFileDat a" 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
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 "zipFileDat a", but the first argument
of the "ZipFile" is filename. I would like to let the ZipFile() open
the file from "zipFileDat a" 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
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 "zipFileDat a", but the first argument
of the "ZipFile" is filename. I would like to let the ZipFile() open
the file from "zipFileDat a" 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
On Apr 29, 9:51 pm, 人言落日是 天涯,望极 天涯不见家 <kelvin....@gma il.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 "zipFileDat a", but the first argument
of the "ZipFile" is filename. I would like to let the ZipFile() open
the file from "zipFileDat a" 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(zipFil eData)
zipp = ZipFile(filelik eobj, 'r')
for name in zipp.namelist() :
# etc etc etc

Apr 29 '07 #8
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 "zipFileDat a", but the first argument
of the "ZipFile" is filename. I would like to let the ZipFile() open
the file from "zipFileDat a" 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2909
by: LC | last post by:
Hi, I'm having a problem using the zipfile module in Windows 2000 sp4. When I use it to zip a small file it works fine, but large file doesnt. Here's the error msg i get... --------------------------------------------------------------------- dynamite4_db_archive.py", line 45, in ? file.write(name, os.path.basename(name),...
0
1356
by: | last post by:
Hi! I need to fresh a zip-archive using zipfile. However, it seems it is no function to fresh the archive. When I use the append-mode of ZipFile, the written file is merely appended to the archive while the old copy is still in the archive. As a result, there are several files with the same name in the archive. Is it valid state of the...
4
3243
by: vincent_delft | last post by:
I've a simple python script that read a directory and put the files into a Zip file. I'm using the os.walk method to get the directory content, I'm creating ZipInfo objects and set "filename", ... to what os.walk give me. .... And it works!!!! BUT!!
3
3736
by: Alan Toppen | last post by:
I was unable to use the ZipFile class in the zipfile module in Python2.4. I got an error that zlib could not be found. Comparing my Python 2.2 installation I noticed Python 2.4 was missing a certain file: /usr/lib/python2.2/lib-dynload/zlibmodule.so. Unable to find a more elegant solution, I copied the file from my Python 2.2 directory into my...
2
1647
by: Aravind | last post by:
Unable to consume Process reserved memory Hi , This is a Windows form application which interacts with the unmanaged C++ codes . In unmanaged c++ code we allocate around 130MB on the heap for annalysing high resolution images . Earlier during the processing ee do lot of process on the image and the memory reaches high at one point of time...
3
2184
by: Sehboo | last post by:
Hi, Is it possible that you can monitor your memory usage and kill some process when memory usage reaches 80% or so? I have an application which uses lot of memory and then system hangs, but it takes about 5 hours before it gets to that point. I want to write a monitor app which kills this process and restarts when memory is 80% used...
11
7565
by: Hari Sekhon | last post by:
I do import zipfile zip=zipfile.ZipFile('d:\somepath\cdimage.zip') zip.namelist() then either of the two: A) file('someimage.iso','w').write(zip.read('someimage.iso'))
5
2113
by: OriginalBrownster | last post by:
This will probably sound like a very dumb question. I am trying to zip some files within a directory. I want to zip all the files within a directory called "temp" and have the zip archive saved in a directory with temp called ziptemp I was trying to read up on how to use the zipfile module python provides, but I cannot seem to find...
15
2028
by: Martin Maney | last post by:
To quote from zipfile.py (2.4 library): # Search the last END_BLOCK bytes of the file for the record signature. # The comment is appended to the ZIP file and has a 16 bit length. # So the comment may be up to 64K long. We limit the search for the # signature to a few Kbytes at the end of the file for efficiency. # also, the signature must...
0
7411
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.