BookmarkSubscribe
Choose Language Hide Translation Bar
Highlighted
natalie_
Community Trekker

How to unzip using JMP Zip Archive Commands

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

	;
);

 

 

0 Kudos
1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Staff (Retired)

Re: How to unzip using JMP Zip Archive Commands

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.

Craige
4 REPLIES 4
Craige_Hales
Staff (Retired)

Re: How to unzip using JMP Zip Archive Commands

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.

Craige
natalie_
Community Trekker

Re: How to unzip using JMP Zip Archive Commands

Thank you so much!  I was definitely over complicating it.  Excellent explanation, too.

Craige_Hales
Staff (Retired)

Re: How to unzip using JMP Zip Archive Commands

Welcome/thanks, and a late follow-up: Load Compressed Data might be helpful too.

Craige
youngkic
Senior Member

Re: How to unzip using JMP Zip Archive Commands

I am following above example code, but I got empty data when I try to read the contents of zip file. 

For example, file1.csv zipped to file1.zip, and I am trying to read file1.csv from file1.zip

Would you please point me what is wrong in my code?

 

 

za = Open( unzip_file_list[1], zip);
dirlist = za << dir;

// below line correctly giving me file1.csv
show(dirlist);

// below two lines correctly gives me full file path.
full_path = directory || dirlist[1];
show(full_path);

// below line returns "Empty()" in log windows. 
txt = za << read( full_path);

 

0 Kudos