473,549 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add file to zip, or replace file in zip

I have a script which zips up a directory, once it does with that
(before it closes the zip file) I want to replace a file that was added
to the zip, say "Foo.txt".

So I tried this...
[code]
z = zipfile.ZipFile ("blah.zip", "w")
# zip the directory
#...
z.writestr("c:\ \path\\to\\curr ent\\foo_txt_fi le\\Foo.txt", "some new
data")
z.close()

All this does is add a new Foo.txt file in the zip alongside the
existing one....any suggestions?

Thanks

Apr 28 '06 #1
7 14460
First note that zipfile is a plain Python module, so reading
Python.../Lib/zipfile.py will reveal all its secrets.

I don't think it is possible to replace archive members using the
module. You could copy all the files into a new zip file, replacing the
ones you want to change as you go. But it might be easier just to use
os.system() or something similar to run an external zip program.

Apr 28 '06 #2
Roger Miller wrote:
First note that zipfile is a plain Python module, so reading
Python.../Lib/zipfile.py will reveal all its secrets.

I don't think it is possible to replace archive members using the
module. You could copy all the files into a new zip file, replacing the
ones you want to change as you go. But it might be easier just to use
os.system() or something similar to run an external zip program.

It is not currently possible to delete or replace individual elements
in a zip file, nor (in all likelihood) would you want to do so. It
would require having the zip[ file in a broken state for some time
as you copy data from one area of the zip to another. Any error
(or raised exception like Control-C) during this process is likely
to leave you with an inconsistent and therefore unreadable zip file.
The typical technique is to mark some files "deleted" in the directory,
add new files (possibly the replacements for the deleted files), and,
in one pass, copy all non-deleted files to a new zip (which you can then
swap for the original zip). Shortcutting this process puts all data in
your zip file at risk.

--Scott David Daniels
sc***********@a cm.org
Apr 30 '06 #3
Scott David Daniels wrote:
It is not currently possible to delete or replace individual elements
in a zip file, nor (in all likelihood) would you want to do so. It
would require having the zip[ file in a broken state for some time
as you copy data from one area of the zip to another.
If zips just used sequential access like tar files, you could append newer
versions without a problem. You windows kids and your crazy data formats.
Any error
(or raised exception like Control-C) during this process is likely
to leave you with an inconsistent and therefore unreadable zip file.
Isn't that true of any modifications to the zip archive, e.g. appending a
new file rather than replacing an existing one?
in one pass, copy all non-deleted files to a new zip (which you can then
swap for the original zip). Shortcutting this process puts all data in
your zip file at risk.


Again, isn't this true of any substantive change to any file whatsoever?
Errors during write can always leave your data in an inconsistent state,
unless your data uses a structured append format like journaled
filesystems. That seems like an orthogonal issue to replacing a file in
the archive.

Apr 30 '06 #4
Edward Elliott wrote:
Scott David Daniels wrote:
...

... You windows kids and your crazy data formats.

There were a few oth OS's than Linux and Windows. Maybe you
should call me "you crazy Tenex kid." Knuth says, "the fastest
way to search is to know where to go." -- Zips have locations of
files, and you needn't read in a lot of a huge zip to find and
extract a couple of files.
Any error
(or raised exception like Control-C) during this process is likely
to leave you with an inconsistent and therefore unreadable zip file.


Isn't that true of any modifications to the zip archive, e.g. appending a
new file rather than replacing an existing one?


Nope. There is enough info in the zip to rebuild the directory
with a forward scan of the zip. (Each entry has a file descr).
"appending" is really replacing backing up before the zip archive
directory and writing another entry, followed by a new directory.
in one pass, copy all non-deleted files to a new zip (which you can then
swap for the original zip). Shortcutting this process puts all data in
your zip file at risk.


Again, isn't this true of any substantive change to any file whatsoever?
Errors during write can always leave your data in an inconsistent state,
unless your data uses a structured append format like journaled
filesystems. That seems like an orthogonal issue to replacing a file in
the archive.


--Scott David Daniels
sc***********@a cm.org
May 1 '06 #5
Scott David Daniels wrote:
Edward Elliott wrote:
Scott David Daniels wrote:
... > ... You windows kids and your crazy data formats.

There were a few oth OS's than Linux and Windows. Maybe you
should call me "you crazy Tenex kid."


Windows popularized the zip format, but if you insist:
You crazy Tenex kids, with your char-at-a-time raw password checking and
new-fangled virtual memory. Back in my day, all we had was Multics and we
liked it that way.

Despite having no memory prior to the Apple II, I do know a bit of history.

Knuth says, "the fastest
way to search is to know where to go." -- Zips have locations of
files, and you needn't read in a lot of a huge zip to find and
extract a couple of files.


You whippersnappers and your random access, always in such a hurry to get
your data. You want faster search, just wait a few machine generations.

May 1 '06 #6
Edward Elliott wrote:
Scott David Daniels wrote:
Edward Elliott wrote:
Scott David Daniels wrote:
...
> ... You windows kids and your crazy data formats.

There were a few oth OS's than Linux and Windows. Maybe you
should call me "you crazy Tenex kid."


Windows popularized the zip format, but if you insist:
You crazy Tenex kids, with your char-at-a-time raw password checking and
new-fangled virtual memory. Back in my day, all we had was Multics and we
liked it that way.


Actually I think it was a combination of CP/M and DOS that popularized
the ZIP format; essentially the floppy-disk set, for whom the zip format
was a godsend.

--
-Scott David Daniels
sc***********@a cm.org
May 1 '06 #7
Scott David Daniels wrote:
Actually I think it was a combination of CP/M and DOS that popularized
the ZIP format; essentially the floppy-disk set, for whom the zip format
was a godsend.


Ah you're right. I just lump all Microsoft OSes under the term 'Windows'
now, though I suppose that's unfair to Xenix.

May 1 '06 #8

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

Similar topics

3
9225
by: Jeffrey D. Gordon | last post by:
I'm wanting to replace Field Values in an existing PDF, I've done this with PHP by doing a replace in the file. I've been able to read the file in a byte array in c# but all my attempts to replace strings in a binary file have led to failure for the last couple of days. I've stared at this function for too long I've become crosseyed ;) ...
11
4025
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType = "text/xml" Response.AddHeader("Content-Disposition", "filename=test.XML")
11
3743
by: MPF | last post by:
Alas, I surrender... In a file from a COBOL dump, which is in ASCII, one of the fields is defined as S9(9) V99 Value +0. The value in this location is 0000018922D, which according to the author of the source, translates to 00000189224. How can this be translated via .Net? I've tried StreamReader and trying to convert everything to bytes,...
17
1998
by: J.S. | last post by:
I have a text file with parameters like the following embedded in the text: @@TextBox1@@, @@TextBox2@@, etc. I know how to read this text file. However, I am trying to figure out how to replace these parameters in the text file with values of the textboxes on my Windows Form called Form1. For example, replace @@TextBox1@@ with value of...
2
1731
by: Astra | last post by:
Hi All Creating an rss.xml file dynamically via ASP/ADO/DB, but find errors in the file. Don't think it's an ASP prob to be honest. Think its more to do with the fact that the ampersands are accepted in the file. Do I take it that you have to 'escape' all non alphanumeric chars to produce a valid file? I thought xml files were...
7
5271
by: Jchick | last post by:
Firstly, I am a noobie with some exposure to VB.net Secondly, I am tasked with trying to do the following: I have a CSV file that I'd like to have a vb.net program make a change to: Here is a line from the CSV file: "3/16/2006
1
3719
by: lactaseman | last post by:
While I know this is not the correct venue... I realize this is of little to no importance to most out there... however, if I had found this in my initial searches, I would have used this. So, as an alternative to the mentalis.org's IniReader, I submit the following files to the web news group DBs...: ...
13
4165
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them from the html page (replace the words in the html page with blank space) I'm new to python and could use a little push in the right direction, any...
3
16909
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it with: import fileinput, string, sys fileQuery = "Text.txt" sourceText = '''SOURCE'''
6
5777
by: saif.shakeel | last post by:
Hi, I need to replace a string in xml file with something else.Ex - <SERVICEPARAMETER id="_775" Semantics="subfunction" DDORef="_54"> <SHORTNAME>rate</SHORTNAME> <LONGNAME>rate</LONGNAME> <VALUE role="constant" DataType="unsigned" value="1" /> <BYTEPOSITION role="position" BytePos="1" /> </SERVICEPARAMETER> - <SERVICEPARAMETER id="_776"...
0
7546
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7471
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
7985
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...
1
7503
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...
0
6071
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5387
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
5111
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
3517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.