From J Wiki
Jump to navigation Jump to search

Extract Zip Archive

This script defines the verb unzip. unzip extracts a specified Zip archive in a specified sub-directory using the Zip tools installed as part of the J 6.02 installation (jpath ~tools/zip).

The code here was originally extracted from the source for the J 6.01 JAL Package Manager (pacman.ijs), but has been simplified for J 6.02. I have tried to retain the cross-platform and cross-external-unzip-program support, but have only tested the script on Windows Vista.


   load jpath '~temp/extractzip.ijs'
   load 'dir'

      (0 0$0)-: 2 1 dir 'c:\test folder\' NB. Show that folder doesn't already exist

   unzip (jpath '~addons/arc/zip/'); 'c:\test folder\'
Archive:  C:/Program Files/j602/addons/arc/zip/
  inflating: c:/test folder/test1.ijs
  inflating: c:/test folder/test2.txt
  inflating: c:/test folder/zlib.bmp
   creating: c:/test folder/other/
  inflating: c:/test folder/other/test3.txt

      2 1 dir 'c:\test folder\'
│other\   │2006 3 20 4 26 56│0     │rw-│----da│
│test1.ijs│2006 3 20 2 10 24│45    │rw-│-----a│
│test2.txt│2006 3 20 2 7 6  │21    │rw-│-----a│
│zlib.bmp │2006 3 20 4 21 4 │226086│rw-│-----a│


[{{#file: "extractzip.ijs"}} Download script: extractzip.ijs ]

NB. =========================================================
NB. Extracts Zip archive to desired sub-directory

coclass 'punzip'

3 : 0 ''
if. -.IFUNIX do. require 'task' end.

UNZIP=: '"',(jpath '~tools/zip/unzip.exe'),'" -o -C '
dquote=: '"'&, @ (,&'"')
hostcmd=: [: 2!:0 '(' , ] , ' || true)'"_

The non-Unix part of the shellcmd verb was changed to use spawn rather than shell, because it calls the external unzip program directly rather than using a batch file as was the case in J 6.01. [{{#file: "extractzip.ijs"}} Download script: extractzip.ijs ]

NB. =========================================================
shellcmd=: 3 : 0
  if. IFUNIX do.
    hostcmd y
    spawn y

Note that the unzip program cannot extract a zip archive to a sub-directory of a previously non-existing directory. That is you must ensure that the path to directory you wish extract into, already exists. You may find the pathcreate verb from the Addons/general/dirutils Addon useful in that situation. [{{#file: "extractzip.ijs"}} Download script: extractzip.ijs ]

NB. =========================================================
NB.*unzip v unzips file into given subdirectory
NB. returns 'Ok' if successful or 'Unexpected error' if not
NB. y is 2-item list of boxed strings
NB.  0{::y filename zip file to extract
NB.  1{::y name of directory to extract zip file to
NB. can't extract to non-existing multilevel path.
unzip=: 3 : 0
  'file dir'=.dquote each y
  e=. 'Unexpected error'
  if. IFUNIX do.
    e=. shellcmd 'tar -xzf ',file,' -C ',dir
    dir=. (_2&}. , PATHSEP_j_ -.~ _2&{.) dir
    e=. shellcmd UNZIP,' ',file,' -d ',dir
    NB. if. 'Ok'-: _2{.e -. CRLF do. e=. 'Ok' end.

unzip_z_=: unzip_punzip_

See Also

-- Ric Sherlock <<DateTime(2007-08-02T04:13:05Z)>>