About zipfile on WinXP

R

rzed

I create a zip file on my WinXP system, using this function:

<fn>
import zipfile
import os
import os.path

def zipdir(dirname, zfname):
zf = zipfile.ZipFile(zfname, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(dirname):
for f in files:
fullname = os.path.join(root, f)
zf.write(fullname)
zf.close()
</fn>

The file is created, and, again using zipfile, I can read it and
extract from it and so on.

However, WinXP has a "feature" (if that's what it is): if you click
on a zip file in explorer, an explorer window opens that shows the
contents of the zip file. If I use WinZip or UltimateZip to create
the files, this works more-or-less okay, but when I click on the
zip file Python creates, no content shows at all (just a blank
explorer window). If I explicitly use one of the other zip-handling
packages, I can view the python-created file's contents.

Has anyone else observed this? If not, is there something in the
code that I should change to permit XP to view the contents?
 
S

Scott David Daniels

rzed said:
I create a zip file on my WinXP system, using this function:
...
However, WinXP has a "feature" (if that's what it is): if you click
on a zip file in explorer, an explorer window opens that shows the
contents of the zip file. If I use WinZip or UltimateZip to create
the files, this works more-or-less okay, but when I click on the
zip file Python creates, no content shows at all (just a blank
explorer window). If I explicitly use one of the other zip-handling
packages, I can view the python-created file's contents.

Has anyone else observed this? If not, is there something in the
code that I should change to permit XP to view the contents?

Are you using a name like 'mumble.zip'? Some identification goes
by extension. Other possible tests include trying to make a minimal
zip with a tiny file in it. compare both the binary contents of the
file and any attributes it get through the file system.

Here's an interesting experiment:

1) Make a zip file (original.zip) with one of the apps that "works".
2) Run the following code:
original = open('original.zip', 'rb')
copy = open('copy.zip', 'wb')
copy.write(original.read())
original.close()
copy.close()
3) Now see if copy.zip can be opened by clicking.

--Scott David Daniels
(e-mail address removed)
 
K

Kent Johnson

rzed said:
I create a zip file on my WinXP system, using this function:

<fn>
import zipfile
import os
import os.path

def zipdir(dirname, zfname):
zf = zipfile.ZipFile(zfname, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(dirname):
for f in files:
fullname = os.path.join(root, f)
zf.write(fullname)
zf.close()
</fn>

The file is created, and, again using zipfile, I can read it and
extract from it and so on.

Has anyone else observed this? If not, is there something in the
code that I should change to permit XP to view the contents?

A couple of guesses:

- If dirname is an absolute path then you are including the drive
specifier in the path in the zip file. If dirname is a relative path
with elements like '.' or '..' then you are including them in the zip.
Try something like
fullname = os.path.join(root, f)[len(dirname):]
to get relative paths in the zip

- WinZip puts empty entries for directories in the zip. Maybe WinXP
requires these.

Kent
 
J

Justin Ezequiel

here's a small script I wrote and use
note the line
arcname = os.path.splitdrive(p)[-1].lstrip(os.sep)

if I comment out `.lstrip(os.sep)`, I get a zip file like rzed
describes

###############################
import sys
import zipfile
import time
import os

if __name__ == '__main__':
pths = sys.argv[1:]
assert pths, 'usage: %s file1 [file2 [file3 ...]]' % sys.argv[0]
zpth = os.path.abspath(time.strftime('%Y%m%d%H%M%S.zip'))
zf = zipfile.ZipFile(zpth, 'w', zipfile.ZIP_DEFLATED)
for p in pths:
p = os.path.abspath(p)
arcname = os.path.splitdrive(p)[-1].lstrip(os.sep)
zf.write(p, arcname)
print arcname, p
zf.close()
print '%s files written to %s' % (len(pths), zpth)

###############################
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top