473,386 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Adding to a zip file


I'm having trouble adding a file to a .zip file using python2.3.
The write method of a ZipFile object needs a filename in
order to add a file to the archive.

The problem is that I want to add a 'file-like' object... ie the 'file'
doesn't have a real filename.

Any suggestions on what I could do? :)

Oh and also, I looked at os.tmpname() to try and create a temporary
file.. but I keep getting warnings about it's security. Does anyone know
how safe it would be to use it?
Jul 18 '05 #1
3 1954
Dennis Hotson wrote:
I'm having trouble adding a file to a .zip file using python2.3.
The write method of a ZipFile object needs a filename in
order to add a file to the archive.

The problem is that I want to add a 'file-like' object... ie the 'file'
doesn't have a real filename.

Any suggestions on what I could do? :)
Look for the writestr() method of the ZipFile object.
Oh and also, I looked at os.tmpname() to try and create a temporary
file.. but I keep getting warnings about it's security. Does anyone know
how safe it would be to use it?


Exactly as safe as the docs suggest? In any case, I would
suggest looking at the "tempfile" module instead.

-Peter
Jul 18 '05 #2
On Wed, 28 Jul 2004 06:47:49 -0400, Peter Hansen wrote:

Look for the writestr() method of the ZipFile object.


Ahhh... works a treat!
Cheers! :D
Jul 18 '05 #3
Dennis Hotson wrote:
On Wed, 28 Jul 2004 06:47:49 -0400, Peter Hansen wrote:
Look for the writestr() method of the ZipFile object.

Ahhh... works a treat!


Check the compression rates within the zipfile. I had some
problems with the writestr method:

----

import zipfile, time

zfile = zipfile.ZipFile('test.zip', 'w', zipfile.ZIP_DEFLATED)
data = 'whatever ' * 100
zinfo = zipfile.ZipInfo('message.txt', time.localtime()[:6])
zfile.writestr(zinfo, data)
zfile.close

-----

On my windows system this generates a test.zip file that
contains the message.txt entry but without compression!
(0% compression ratio).

I wasn't motivated enough to find out what exactly is happening
because the file based methods worked properly and turned out
to be a better solution for my problem.

Istvan.

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Garry | last post by:
Hi, I am new to python, hope someone can help me here: I have a MS Access exported .txt file which is tab delimited in total 20 columns, now I need to add another column of zero at the 4th column...
9
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
7
by: Wysiwyg | last post by:
Is there any way to add an embedded resource to a project without copying it to the project's directory? I have shared resources and don't want each project using the images, xml files, etc. to...
1
by: Christopher W. Douglas | last post by:
I am building a VB.NET application in Visual Studio.NET 2003. I have an existing project with code and forms I want to reuse in this project. If I am copying a module file, then Add Existing Item...
1
by: Max | last post by:
Searched all over google and can't seem to find how to do this. How do I go about adding comments about my own functions and sub-procedures? Like when I use them, the tool-tip tells me more then...
6
by: UJ | last post by:
I have a couple of files (type definitions, constants, ...) that I want to use in multiple projects. How do I make it so that I have only one copy of the file for multiple projects? If I do add...
4
by: Saurabh Aggrawal | last post by:
Hi, I have made a dll and it is working fine in the debug build (as expected) but when I run it in the release build it is working strangely. For example, the dll saves the 10 properties on the...
5
by: DBC User | last post by:
I have a situation, where I need to add 4 or 5 data files (they change every time I build) in my project during build time and somehow I need a way to access these files during runtime. So I have...
1
by: John | last post by:
Is there such a thing as storing icons or bitmaps into a resource file, and adding more icons or bitmaps into the resource file at runtime? I want to store a bunch of icons into a resource file,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...

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.