Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 1 | import zipfile, os |
| 2 | |
| 3 | srcname = "junk9630.tmp" |
| 4 | zipname = "junk9708.tmp" |
| 5 | |
| 6 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 7 | fp = open(srcname, "w") # Make a source file with some lines |
| 8 | for i in range(0, 1000): |
| 9 | fp.write("Test of zipfile line %d.\n" % i) |
| 10 | fp.close() |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 11 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 12 | zip = zipfile.ZipFile(zipname, "w") # Create the ZIP archive |
| 13 | zip.write(srcname, srcname) |
| 14 | zip.write(srcname, "another.name") |
| 15 | zip.close() |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 16 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 17 | zip = zipfile.ZipFile(zipname, "r") # Read the ZIP archive |
| 18 | zip.read("another.name") |
| 19 | zip.read(srcname) |
| 20 | zip.close() |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 21 | finally: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 22 | if os.path.isfile(srcname): # Remove temporary files |
| 23 | os.unlink(srcname) |
| 24 | if os.path.isfile(zipname): |
| 25 | os.unlink(zipname) |