# ReadFromZip - Retrieves the contents of ZIPped files from ZIP archives..Built # specifically for retrieving ZIPped syslog text. Note that the # error checking for valid ZIP files can be broken by files that # contain appended comments (at least as of Python 2.1.1) # # Written by Tim Sharpe # # Revision List # Date Ver Description #================================================================== # 03/17/2005 0.80 Original coding. # 03/18/2005 0.81 Added error handling for invalid ZIP files. Cleaned up error message # for non-matching filenamemask. # # # # # # # import os.path, sys, zipfile # # Store program name and version for inclusion into the standard banner banner1='ReadFromZip, v0.81' banner2='Copyright (C) 2005, Tim Sharpe . ' print banner1 + ', ' + banner2 print 'This program comes with ABSOLUTELY NO WARRANTY and is covered by the' print 'GNU General Public License, version 2. See the "license.txt"' print 'file for details. This is free software, and you are welcome to ' print 'redistribute it under conditions of the GNU General Public License.' print def usage(): print banner1 print banner2 print print 'ReadFromZip - Lists files with a ZIP archive (List Mode) or extracts file' print ' contents from the ZIP archive (Extract Mode). In Extract Mode,' print ' dumps contents of all files that contain the FileNameMaskFile' print ' contents are dumped to stdout. This program is really built to' print ' work with archived text files, such as syslogs.' print print 'Usage:' print ' List Mode: readfromzip.py -L [ZipFileName]' print ' Extract Mode: readfromzip.py [FileNameMask] [ZipFileName]' print print 'Where: FileNameMask = text to match against names of files in the Zip' print ' archive. Any file which contains the FileNameMask' print ' anywhere within its filename will be extracted' print ' Zipfilename = Path/Name of the Zip archive' print print 'Notes: -If there are spaces in the FileNameMask or ZipFileName, enclose it in' print ' quotes so the OS shell will correctly group the text.' print ' -ZIP archive files with appended comments may not be recognized as valid.' print sys.exit() # Main Program ListMode=0 print # Check command-line arguments # Get info the "listmode" option try: if sys.argv[1].lower()=='-l': ListMode=1 else: filenamemask=sys.argv[1] except IndexError: usage() # # Obtain the zipfilenane try: infile=sys.argv[2] except IndexError: print 'ERROR! Missing ZipFileName!' print usage() if not os.path.exists(infile): print 'ERROR! ZipFileName does not exist!' print usage() # try: ZIP=zipfile.ZipFile(infile,'r') except zipfile.BadZipfile: print 'ERROR! \'' + infile + '\' does not appear to be a valid ZIP archive! Exiting...\n\n' sys.exit() # ZIPList=ZIP.namelist() # # Do the actual work if ListMode==1: # Handle List Mode print 'Files in ' + infile + ':' for file in ZIPList: print file else: #Handle Extract Mode # # Make sure that the filenamemask acutally matches some files in the zip files=0 for file in ZIPList: if file.find(filenamemask)>=0: files+=1 if files==0: print 'ERROR! \'' + filenamemask + '\' does not match any files in the ZIP archive! Exiting...\n\n' sys.exit() # # Extract and print for file in ZIPList: if file.find(filenamemask)>=0: # Directly print the file contents vice storing in a string to conserve # memory and speed this up print ZIP.read(file)