1
0
mirror of https://github.com/sasjs/core.git synced 2026-01-16 04:50:05 +00:00

fix: addressing corrupted unzips in certain cases

This commit is contained in:
munja
2021-12-05 00:39:47 +00:00
parent 8c21b9397f
commit 966f2cf78d

View File

@@ -7,19 +7,23 @@
Usage: Usage:
filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas"; filename mc url
"https://raw.githubusercontent.com/sasjs/core/main/all.sas";
%inc mc; %inc mc;
%mp_unzip(ziploc="/some/file.zip",outdir=/some/folder) %mp_unzip(ziploc="/some/file.zip",outdir=/some/folder)
<h4> SAS Macros </h4> More info: https://blogs.sas.com/content/sasdummy/2015/05/11/using-filename-zip-to-unzip-and-read-data-files-in-sas/
@li mf_mkdir.sas
@li mf_getuniquefileref.sas
@param ziploc= Fileref or quoted full path to zip file ("/path/to/file.zip") @param ziploc= Fileref or quoted full path to zip file ("/path/to/file.zip")
@param outdir= (%sysfunc(pathname(work))) Directory in which to write the @param outdir= (%sysfunc(pathname(work))) Directory in which to write the
outputs (created if non existant) outputs (created if non existant)
<h4> SAS Macros </h4>
@li mf_mkdir.sas
@li mf_getuniquefileref.sas
@li mp_binarycopy.sas
@version 9.4 @version 9.4
@author Allan Bowe @author Allan Bowe
@source https://github.com/sasjs/core @source https://github.com/sasjs/core
@@ -31,14 +35,16 @@
,outdir=%sysfunc(pathname(work)) ,outdir=%sysfunc(pathname(work))
)/*/STORE SOURCE*/; )/*/STORE SOURCE*/;
%local f1 f2 f3; %local f1 f2 ;
%let f1=%mf_getuniquefileref(); %let f1=%mf_getuniquefileref();
%let f2=%mf_getuniquefileref(); %let f2=%mf_getuniquefileref();
%let f3=%mf_getuniquefileref();
/* Macro variable &datazip would be read from the file */ /* Macro variable &datazip would be read from the file */
filename &f1 ZIP &ziploc; filename &f1 ZIP &ziploc;
/* create target folder */
%mf_mkdir(&outdir)
/* Read the "members" (files) from the ZIP file */ /* Read the "members" (files) from the ZIP file */
data _data_(keep=memname isFolder); data _data_(keep=memname isFolder);
length memname $200 isFolder 8; length memname $200 isFolder 8;
@@ -53,21 +59,32 @@ data _data_(keep=memname isFolder);
end; end;
rc=dclose(fid); rc=dclose(fid);
run; run;
filename &f1 clear;
filename &f2 temp;
/* loop through each entry and either create the subfolder or extract member */ /* loop through each entry and either create the subfolder or extract member */
%mf_mkdir(&outdir)
data _null_; data _null_;
set &syslast; set &syslast;
file &f2;
if isFolder then call execute('%mf_mkdir(&outdir/'!!memname!!')'); if isFolder then call execute('%mf_mkdir(&outdir/'!!memname!!')');
else do; else do;
call execute( qname=quote(cats("&outdir/",memname));
cats('filename &f2 zip &ziploc member="',memname,'" recfm=n;') bname=cats('(',memname,')');
); put '/* hat tip: "data _null_" on SAS-L */';
call execute('filename &f3 "&outdir/'!!trim(memname)!!'" recfm=n;'); put 'data _null_;';
call execute('data _null_; rc=fcopy("&f2","&f3");run;'); put ' infile &f1 ' bname ' lrecl=256 recfm=F length=length eof=eof unbuf;';
call execute('filename &f2 clear; filename &f3 clear;'); put ' file ' qname ' lrecl=256 recfm=N;';
put ' input;';
put ' put _infile_ $varying256. length;';
put ' return;';
put 'eof:';
put ' stop;';
put 'run;';
end; end;
run; run;
%inc &f2/source2;
filename &f2 clear;
%mend mp_unzip; %mend mp_unzip;