def __init__(self, zipfile, entry=""): """ Create a new path pointer pointing at the specified entry in the given zipfile. :raise IOError: If the given zipfile does not exist, or if it does not contain the specified entry. """ if isinstance(zipfile, basestring): zipfile = OpenOnDemandZipFile(os.path.abspath(zipfile)) # Normalize the entry string: entry = re.sub("(^|/)/+", r"\1", entry) # Check that the entry exists: if entry: try: zipfile.getinfo(entry) except: # Sometimes directories aren't explicitly listed in # the zip file. So if `entry` is a directory name, # then check if the zipfile contains any files that # are under the given directory. if entry.endswith("/") and [n for n in zipfile.namelist() if n.startswith(entry)]: pass # zipfile contains a file in that directory. else: # Otherwise, complain. raise IOError("Zipfile %r does not contain %r" % (zipfile.filename, entry)) self._zipfile = zipfile self._entry = entry
def add_interesting_events(self, node_dictionary, zipfile, zipfilename): t = time.clock() self.filename = zipfilename # Figure out sizes for progress reporting for key in self.events.keys(): for name in zipfile.namelist(): if name.endswith('/' + key): self.total_size += zipfile.getinfo(name).file_size mortimer.update_progress_so_far(self.progress_queue, self.total_size, self.progress_size) for key in self.events.keys(): for name in zipfile.namelist(): if name.endswith('/' + key): tf = tempfile.TemporaryFile() tf.write(zipfile.open(name).read()) tf.seek(0) # some logs don't have a year in the timestamp, assume log file year is the one self.year = int((datetime(zipfile.getinfo(name).date_time[0], 1, 1) - datetime(1970,1,1)).total_seconds()) self.process_log(node_dictionary, key, tf) tf.close() self.process_time = time.clock() - t mortimer.update_progress_so_far(self.progress_queue, self.total_size, self.total_size) print "{}: Processing of node events took {} seconds".format(self.filename, self.process_time)
def __init__(self, zipfile, entry=''): """ Create a new path pointer pointing at the specified entry in the given zipfile. @raise IOError: If the given zipfile does not exist, or if it does not contain the specified entry. """ if isinstance(zipfile, basestring): zipfile = OpenOnDemandZipFile(os.path.abspath(zipfile)) # Normalize the entry string: entry = re.sub('(^|/)/+', r'\1', entry) # Check that the entry exists: if entry: try: zipfile.getinfo(entry) except: # Sometimes directories aren't explicitly listed in # the zip file. So if `entry` is a directory name, # then check if the zipfile contains any files that # are under the given directory. if (entry.endswith('/') and [n for n in zipfile.namelist() if n.startswith(entry)]): pass # zipfile contains a file in that directory. else: # Otherwise, complain. raise IOError('Zipfile %r does not contain %r' % (zipfile.filename, entry)) self._zipfile = zipfile self._entry = entry
def findsixthagain(zipfile,filename): filefd = open(filepath+'/'+filename,'r') lines = filefd.readlines() filefd.close() data = filter(str.isdigit,lines[0]) print zipfile.getinfo(filename).comment, if data: findsixthagain(zipfile,data+'.txt') else: print lines[0]
def get_datetime_for_zip(zipfile, filename): info = zipfile.getinfo(filename) dt = info.date_time central_tz = timezone('US/Central') return central_tz.localize( datetime(dt[0], dt[1], dt[2], dt[3], dt[4], dt[5]) )
def get_file_infos_from_zip(zipfile): # we return internal file name and md5 for all non-directory files within the zipped file # the separator within zip archive is always "/" even when produced on windows... return [ {'file':get_internal_zip_path(member), 'md5': md5_from_zipped_file(zipfile,member), 'size': zipfile.getinfo(member).file_size} for member in zipfile.namelist() if not member.endswith("/") ]
def open(filename, mode='r', signaturetag='ipodder'): "Open a file either from the update or the run directory." assert mode in ('r', 'rt', 'rb') updateinfo = loadedUpdates.get(signaturetag) if updateinfo is not None: try: zipfile = updateinfo['object'] fileinfo = zipfile.getinfo(filename) return StringIO.StringIO(zipfile.read(filename)) except KeyError: pass return file(os.path.join(getrundir(), filename, mode))
import zipfile import re seed = "90052" zipfile = zipfile.ZipFile("D:\channel.zip", mode='r') comments="" while True: file_like = zipfile.read(seed + ".txt") comments+=zipfile.getinfo(seed + ".txt").comment.decode() seed = "".join(re.findall('([0-9]{1,10})', str(file_like))) if seed == '': print(file_like) break print(comments) #print("".join(comments)) # else: # pass # #print(seed) #file_like.read()
def __init__(self, zipfile, name): info = zipfile.getinfo(name) for x in dir(info): if not (x.startswith("_") or x.endswith("_")): setattr(self, x, getattr(info, x)) self.size = self.file_size
#!/usr/bin/python # http://www.pythonchallenge.com/pc/def/channel.html import urllib, zipfile, re fileUrl = 'http://www.pythonchallenge.com/pc/def/channel.zip' (filename, ignore) = urllib.urlretrieve(fileUrl) zipfile = zipfile.ZipFile(filename) startingPattern = re.compile('start from ([0-9]*)') linkingNumberPattern = re.compile('Next nothing is ([0-9]*)') number = startingPattern.search(zipfile.read('readme.txt')).group(1) while number: filename = number + '.txt' fileContents = zipfile.read(filename) info = zipfile.getinfo(filename) print info.comment, numberMatch = linkingNumberPattern.search(fileContents) if numberMatch: number = numberMatch.group(1) else: number = None zipfile.close()
#!/usr/bin/python # http://www.pythonchallenge.com/pc/def/channel.html import urllib, zipfile, re fileUrl = 'http://www.pythonchallenge.com/pc/def/channel.zip' (filename,ignore) = urllib.urlretrieve(fileUrl) zipfile = zipfile.ZipFile(filename) startingPattern = re.compile('start from ([0-9]*)') linkingNumberPattern = re.compile('Next nothing is ([0-9]*)') number = startingPattern.search(zipfile.read('readme.txt')).group(1) while number: filename = number + '.txt' fileContents = zipfile.read(filename) info = zipfile.getinfo(filename) print info.comment, numberMatch = linkingNumberPattern.search(fileContents) if numberMatch: number = numberMatch.group(1) else: number = None zipfile.close()
tmp_dir_path = pathlib.Path(tmp_dir) if tmp_dir_path.is_dir(): shutil.rmtree(tmp_dir) atexit.register(cleanup) try: os.mkdir(tmp_dir) except FileExistsError: pass except Exception as e: print(str(e)) sys.exit(1) zip_url = "http://www.pythonchallenge.com/pc/def/channel.zip" channel_zip = urllib.request.urlretrieve(zip_url, tmp_dir + "channel.zip") zipfile = zipfile.ZipFile(tmp_dir + "channel.zip") zipfile.extractall(tmp_dir) nothing = "90052" while nothing is not None: filename = "channel/" + nothing + ".txt" f_data = open(filename).read() nothing = re.search(r'\d+$', f_data) if nothing is not None: nothing = nothing.group(0) zipinfo = zipfile.getinfo(nothing + '.txt') print(zipinfo.comment.decode('UTF-8'), end='')
# hint2: answer is inside the zip # print zipfile.read('90052.txt') # content of 90052.txt # Next nothing is 94191 regex = re.compile('nothing is (\d*)') text = zipfile.read('90052.txt') next_file = regex.search(text).groups()[0] comments = '' for i in xrange(0, total_files): text = zipfile.read(next_file + '.txt') info = zipfile.getinfo(next_file + '.txt') comments += info.comment try: next_file = regex.search(text).groups()[0] except: print '{0} of {1}: {2}.txt'.format(i, total_files, next_file) print text + '\n' break # output of 46145.txt: # Collect the comments. print comments # output of comments: # ***************************************************************
# hint2: answer is inside the zip # print zipfile.read('90052.txt') # content of 90052.txt # Next nothing is 94191 regex = re.compile("nothing is (\d*)") text = zipfile.read("90052.txt") next_file = regex.search(text).groups()[0] comments = "" for i in xrange(0, total_files): text = zipfile.read(next_file + ".txt") info = zipfile.getinfo(next_file + ".txt") comments += info.comment try: next_file = regex.search(text).groups()[0] except: print "{0} of {1}: {2}.txt".format(i, total_files, next_file) print text + "\n" break # output of 46145.txt: # Collect the comments. print comments # output of comments: # ***************************************************************
def watched_stream_getendsize(zipfile, entry_file, filename): endsize = zipfile.getinfo(entry_file).file_size logging.debug(entry_file + ' size of stat file= ' + str(endsize)) return endsize