473,387 Members | 1,520 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,387 software developers and data experts.

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\\current\\foo_txt_file\\F oo.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 14437
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***********@acm.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***********@acm.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***********@acm.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
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...
11
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 =...
11
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...
17
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...
2
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...
7
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...
1
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...
13
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...
3
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...
6
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>...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...

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.