def _unzip(zip_file: BytesIO) -> BytesIO: """ Unzip and return an in-memory file e.g. gadm36_MCO_gpkg.zip. Te archive is expected to contain 2 files: license.txt and gadm36_{country_code_alpha3}.gpkg. :param zip_file: :return: unzipped geopackage file """ zip_file: ZipFile = ZipFile(zip_file) filenames = zip_file.namelist() assert ( len(filenames) == 2 ), f"expected 2 files in zip archive, got {len(filenames)}" for archive_file in zip_file.namelist(): if ".gpkg" in archive_file: return BytesIO(zip_file.read(archive_file)) raise RuntimeError(f"missing .gpkg in zip file {repr(zip_file)}")
def load_dwd_xml_events(): """ Load ZIP-file from DWD OpenData-API, unzip it, return list of events in XML """ data_zip = BytesIO() data_zip.write(requests.get(API_URL).content) data_zip.seek(0) data_zip = ZipFile(data_zip) return { splitext(name)[0]: data_zip.read(name).decode("utf-8") for name in data_zip.namelist() }
def extract_files(target_path, zip_data): files = BytesIO(zip_data) files = zipfile.ZipFile(files, mode='r', compression=zipfile.ZIP_LZMA) files.extractall(path=target_path) return set(files.namelist())
''' from zipfile import ZipFile from getchallenge import challenge from io import BytesIO import re data = challenge("channel.zip") zipped = BytesIO(data.content) # takes the data in as bytestream data # lets us work entirely in memory zipped = ZipFile(zipped) # | converts the zipfile into a dictionary using # V the names as keys and the contents as values stuff = {name: zipped.read(name).decode("utf-8") for name in zipped.namelist()} stuff = stuff def linkedPy(start, files): # function to go through the dictionary text = str(files.get(start)) pat = re.search('[-+]?\d+[\.]?\d*', text) if pat is None: return "not a file" else: return pat.group(0) # returns a string result from re.search out = [] clue = '90052.txt' # from the readme file in the archive for i in range(len(zipped.namelist())): step = linkedPy(clue, stuff)
requests to pull the file automatically ''' from zipfile import ZipFile from getchallenge import challenge from io import BytesIO import re data = challenge("channel.zip") zipped = BytesIO(data.content) # takes the data in as bytestream data # lets us work entirely in memory zipped = ZipFile(zipped) # | converts the zipfile into a dictionary using # V the names as keys and the contents as values stuff = {name: zipped.read(name).decode("utf-8") for name in zipped.namelist()} stuff = stuff def linkedPy(start, files): # function to go through the dictionary text = str(files.get(start)) pat = re.search('[-+]?\d+[\.]?\d*', text) if pat is None: return "not a file" else: return pat.group(0) # returns a string result from re.search out = [] clue = '90052.txt' # from the readme file in the archive for i in range(len(zipped.namelist())):