Expand a zip archive (platform agnostic)

Function to expand a zip archive. Works on Mac and on Windows 8 and later.

See also Andrew Nelson's ZIP XOP and Jan Ilavsky's code snippet.

// returns 1 for success, 0 for failure
function unzipArchive(archivePathStr, unzippedPathStr)
    string archivePathStr, unzippedPathStr
       
    string validExtensions="zip;" // set to "" to skip check
    variable verbose=1 // choose whether to print output from executescripttext
    string msg, unixCmd, cmd
       
    GetFileFolderInfo /Q/Z archivePathStr

    if(V_Flag || V_isFile==0)
        printf "Could not find file %s\r", archivePathStr
        return 0
    endif

    if(itemsInList(validExtensions) && findlistItem(ParseFilePath(4, archivePathStr, ":", 0, 0), validExtensions, ";", 0, 0)==-1)
        printf "%s doesn't appear to be a zip archive\r", ParseFilePath(0, archivePathStr, ":", 1, 0)
        return 0
    endif
   
    if(strlen(unzippedPathStr)==0)
        unzippedPathStr=SpecialDirPath("Desktop",0,0,0)+ParseFilePath(3, archivePathStr, ":", 0, 0)
        sprintf msg, "Unzip to %s:%s?", ParseFilePath(0, unzippedPathStr, ":", 1, 1), ParseFilePath(0, unzippedPathStr, ":", 1, 0)
        doALert 1, msg
        if (v_flag==2)
            return 0
        endif
    else
        GetFileFolderInfo /Q/Z unzippedPathStr
        if(V_Flag || V_isFolder==0)
            sprintf msg, "Could not find unzippedPathStr folder\rCreate %s?", unzippedPathStr
            doalert 1, msg
            if (v_flag==2)
                return 0
            endif
        endif  
    endif
   
    // make sure unzippedPathStr folder exists - necessary for mac
    newpath /C/O/Q acw_tmpPath, unzippedPathStr
    killpath /Z acw_tmpPath

    if(stringmatch(StringByKey("OS", igorinfo(3))[0,2],"Win")) // Windows
        // The following works with .Net 4.5, which is available in Windows 8 and up.
        // current versions of Windows with Powershell 5 can use the more succinct PS command
        // 'Expand-Archive -LiteralPath C:\archive.zip -DestinationPath C:\Dest'
        string strVersion=StringByKey("OSVERSION", igorinfo(3))
        variable WinVersion=str2num(strVersion) // turns "10.1.2.3" into 10.1
        if (WinVersion<8)
            print "unzipArchive requires Windows 8 or later"
            return 0
        endif
       
        archivePathStr=parseFilePath(5, archivePathStr, "\\", 0, 0)
        unzippedPathStr=parseFilePath(5, unzippedPathStr, "\\", 0, 0)
        cmd="powershell.exe -nologo -noprofile -command \"& { Add-Type -A 'System.IO.Compression.FileSystem';"
        sprintf cmd "%s [IO.Compression.ZipFile]::ExtractToDirectory('%s', '%s'); }\"", cmd, archivePathStr, unzippedPathStr
    else // Mac
        sprintf unixCmd, "unzip %s -d %s", ParseFilePath(5, archivePathStr, "/", 0,0), ParseFilePath(5, unzippedPathStr, "/", 0,0)
        sprintf cmd, "do shell script \"%s\"", unixCmd
    endif
   
    executescripttext /B/UNQ/Z cmd
    if(verbose)
        print S_value // output from executescripttext
    endif
   
    return (v_flag==0)
end

 

Forum

Support

Gallery

Igor Pro 8

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More