This topic contains 0 replies, has 1 voice, and was last updated by  lev 4 years, 3 months ago.

Running external un-zip executable with io.popen() call

  • Sometimes we have to execute external executable from within Lua script.

    For example, you received zip file zipped in 7-Zip and you wish to unzip using very same program 7-Zip.

    In order to unzip it, there is need to use external executable 7-Zip from ‘7-Zip Command Line Version’ .

    Below is quick simple code demonstrating how to do it:

    function main ()
     local e = '\"C:/7za920/7za.exe\" \" e -y -oC:/temp/7zipOut C:/7za920/a.zip\"'
     local E = io.popen(e)
     local out = E:read('*a')
     io.close(E)
    end

    Paths:

    – 7zip is installed in c:\7za920 (it is ‘7-Zip Command Line Version’ for Windows downloaded from http://www.7-zip.org/download.html)

    – Files from zip extracted into output folder c:\temp\7zipOut

    – Archive a.zip is located in C:\7za920 folder

    This creates a bunch of files in output folder.

    Then we can use io.popen() to list extracted files into List of Files and process them one by one in a loop.

    require('files')
     
    function main ()
     
       local e = '\"C:/7za920/7za.exe\" \" e -y -oC:/temp/7zipOut C:/7za920/a.zip\"'
       local E=io.popen(e)
       local out=E:read('*a')
       io.close(E)
     
       local P = io.popen('dir /b c:\\temp\\7zipOut\\*.txt')
       local List = P:read('*a')
     
       for k, v in pairs (List:split('\n')) do 
          local f=io.open('c:\\temp\\7zipOut\\'..v,'r') 
     
          if (f) then  
             local s=f:read("*all")  
             queue.push{data=s}
             io.close(f) 
          end 
       end
    end

    Note: we call function string.split() defined elsewhere, e.g. http://wiki.interfaceware.com/534.html

You must be logged in to reply to this topic.