In an earlier question I asked, I asked if it was possible to unzip files. I read a blog (Using FILENAME ZIP to unzip and read data files in SAS - The SAS Dummy), but at the moment I am having a hard time following it using JSL.
I have a folder with 150 zipped folders in it. Each folder has a device name, followed by an order score, then the date (ie. Device X_2016_07_20.zip). I want to create a new folder with the device name (which I can get working). I can get open a zip folder and see all the different files within, but I can not figure out how to unzip (or even copy) to the new folder. Can someone please give me a hand?
This is what I have so far:
directory = Pick Directory ();
filesInDirectory = Files In Directory(directory);
nFiles = N Items(filesInDirectory); //get number of zip files in directory
For(i=1, i<=nFiles, i++,
fileName = filesInDirectory;
pos = Contains(fileName, "_");
deviceName = left(fileName,pos-1); //get the device name
subDir = directory || deviceName;
//Create Directory(subDir); //eventually create a new sub directory
za = Open(directory || fileName, zip);
dirlist = za << dir;
Show(dirlist) // see all contents of zipped folder
);
the next line uses the zip archive object, za, to read a member from the archive. something like this:
za = Open( "$desktop\test.zip", zip );
dirlist = za << dir;
Show( dirlist ); // see all contents of zipped folder
// dirlist = {"fft.jmp", "fft2.jsl"};
For( i = 1, i <= N Items( dirlist ), i++,
If( Right( dirlist, 4 ) == ".jmp" /*binary data*/,
blob = za << read( dirlist, Format( blob ) );
Save Text File( "$desktop\deleteme_" || dirlist, blob );
);
If( Right( dirlist, 4 ) == ".jsl" /*text data*/,
text = za << read( dirlist );
Save Text File( "$desktop\deleteme_" || dirlist, text );
);
);
there are two examples, in bold, because a zip file might contain binary data (a JMP data table in this case) or plain text (a JMP script). The binary data can't be stored in a JSL string, but a BLOB will work. The next line, saveTextFile, understands both strings and BLOBs (and will save a binary file for a BLOB, in spite if its name). You should not depend on the order of the members in the zip file, especially if someone might have edited/added/deleted members from them.
(I'm assuming you want to extract the members, not copy the .zip file to another .zip file. You could do that without opening the zip file at all.)
edit: you can use BLOB for text files too, if you are just copying them. If you want to process the text in JSL, BLOB will make it harder.
the next line uses the zip archive object, za, to read a member from the archive. something like this:
za = Open( "$desktop\test.zip", zip );
dirlist = za << dir;
Show( dirlist ); // see all contents of zipped folder
// dirlist = {"fft.jmp", "fft2.jsl"};
For( i = 1, i <= N Items( dirlist ), i++,
If( Right( dirlist, 4 ) == ".jmp" /*binary data*/,
blob = za << read( dirlist, Format( blob ) );
Save Text File( "$desktop\deleteme_" || dirlist, blob );
);
If( Right( dirlist, 4 ) == ".jsl" /*text data*/,
text = za << read( dirlist );
Save Text File( "$desktop\deleteme_" || dirlist, text );
);
);
there are two examples, in bold, because a zip file might contain binary data (a JMP data table in this case) or plain text (a JMP script). The binary data can't be stored in a JSL string, but a BLOB will work. The next line, saveTextFile, understands both strings and BLOBs (and will save a binary file for a BLOB, in spite if its name). You should not depend on the order of the members in the zip file, especially if someone might have edited/added/deleted members from them.
(I'm assuming you want to extract the members, not copy the .zip file to another .zip file. You could do that without opening the zip file at all.)
edit: you can use BLOB for text files too, if you are just copying them. If you want to process the text in JSL, BLOB will make it harder.
Thank you so much! I was definitely over complicating it. Excellent explanation, too.