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> SAS Macros </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= (%sysfunc(pathname(work))) Directory in which to write the
21 outputs (created if non existant)
22
23 @version 9.4
24 @author Allan Bowe
25 @source https://github.com/sasjs/core
26
27**/
28
29%macro mp_unzip(
30 ziploc=
31 ,outdir=%sysfunc(pathname(work))
32)/*/STORE SOURCE*/;
33
34%local fname1 fname2 fname3;
35%let fname1=%mf_getuniquefileref();
36%let fname2=%mf_getuniquefileref();
37%let fname3=%mf_getuniquefileref();
38
39/* Macro variable &datazip would be read from the file */
40filename &fname1 ZIP &ziploc;
41
42/* Read the "members" (files) from the ZIP file */
43data _data_(keep=memname isFolder);
44 length memname $200 isFolder 8;
45 fid=dopen("&fname1");
46 if fid=0 then stop;
47 memcount=dnum(fid);
48 do i=1 to memcount;
49 memname=dread(fid,i);
50 /* check for trailing / in folder name */
51 isFolder = (first(reverse(trim(memname)))='/');
52 output;
53 end;
54 rc=dclose(fid);
55run;
56filename &fname1 clear;
57
58/* loop through each entry and either create the subfolder or extract member */
59data _null_;
60 set &syslast;
61 if isFolder then call execute('%mf_mkdir(&outdir/'!!memname!!')');
62 else call execute('filename &fname2 zip &ziploc member='
63 !!quote(trim(memname))!!';filename &fname3 "&outdir/'
64 !!trim(memname)!!'" recfm=n;data _null_; rc=fcopy("&fname2","&fname3");run;'
65 !!'filename &fname2 clear; filename &fname3 clear;');
66run;
67
68%mend mp_unzip;