Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mp_unzip.sas
Go to the documentation of this file.
1 /**
2  @file mp_unzip.sas
3  @brief Unzips a zip file
4  @details Opens the zip file and copies all the contents to another directory.
5  It is not possible to retain permissions / timestamps, also the BOF marker
6  is lost so it cannot extract binary files.
7 
8  Usage:
9 
10  filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
11  %inc mc;
12 
13  %mp_unzip(ziploc="/some/file.zip",outdir=/some/folder)
14 
15  <h4> Dependencies </h4>
16  @li mf_mkdir.sas
17  @li mf_getuniquefileref.sas
18 
19  @param ziploc= fileref or quoted full path to zip file ("/path/to/file.zip")
20  @param outdir= directory in which to write the outputs (created if non existant)
21 
22  @version 9.4
23  @author Allan Bowe
24  @source https://github.com/sasjs/core
25 
26 **/
27 
28 %macro mp_unzip(
29  ziploc=
30  ,outdir=%sysfunc(pathname(work))
31 )/*/STORE SOURCE*/;
32 
33 %local fname1 fname2 fname3;
34 %let fname1=%mf_getuniquefileref();
35 %let fname2=%mf_getuniquefileref();
36 %let fname3=%mf_getuniquefileref();
37 
38 filename &fname1 ZIP &ziploc; * Macro variable &datazip would be read from the file*;
39 
40 /* Read the "members" (files) from the ZIP file */
41 data _data_(keep=memname isFolder);
42  length memname $200 isFolder 8;
43  fid=dopen("&fname1");
44  if fid=0 then stop;
45  memcount=dnum(fid);
46  do i=1 to memcount;
47  memname=dread(fid,i);
48  /* check for trailing / in folder name */
49  isFolder = (first(reverse(trim(memname)))='/');
50  output;
51  end;
52  rc=dclose(fid);
53 run;
54 filename &fname1 clear;
55 
56 /* loop through each entry and either create the subfolder or extract member */
57 data _null_;
58  set &syslast;
59  if isFolder then call execute('%mf_mkdir(&outdir/'!!memname!!')');
60  else call execute('filename &fname2 zip &ziploc member='
61  !!quote(trim(memname))!!';filename &fname3 "&outdir/'
62  !!trim(memname)!!'" recfm=n;data _null_; rc=fcopy("&fname2","&fname3");run;'
63  !!'filename &fname2 clear; filename &fname3 clear;');
64 run;
65 
66 %mend;