473,847 Members | 1,654 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 3949
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
2927
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), zipfile.ZIP_DEFLATED) File "C:\Python22\lib\zipfile.py", line 426, in write zinfo.file_size))
0
1376
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 archive? I would to achieve the result using a delete/remove operation, but it is also is not...
4
3290
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
3755
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 Python 2.4 directory. When running my Python script it gives a warning: ...
2
1660
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 to 1.2GB , after that we clear all the memory being used so the memory in the task manager comes to...
3
2197
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 up.
11
7628
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
2136
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 adequate documentation on function itself.
15
2054
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 not appear in the comment. END_BLOCK = min(filesize, 1024 * 4) So the author knows that there's...
0
9887
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9730
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10983
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10707
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7883
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7056
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4529
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
3
3164
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.