Zipfile Extraction Utility

  • Share this:

Code introduction


This function accepts a path to a zipfile and a target extraction path, then checks if the file is a zipfile. If so, it extracts its contents to the specified target path.


Technology Stack : zipfile

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zipfile_extract(file_path, extract_to):
    import zipfile

    def is_zipfile(path):
        return zipfile.is_zipfile(path)

    if not is_zipfile(file_path):
        raise ValueError("Provided file is not a zipfile")

    with zipfile.ZipFile(file_path, 'r') as zip_ref:
        zip_ref.extractall(extract_to)

    return f"Extracted contents of {file_path} to {extract_to}"                
              
Tags: