cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
natalie_
Level V

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

	;
);

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

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

View solution in original post

6 REPLIES 6
Craige_Hales
Super User

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_
Level V

Re: How to unzip using JMP Zip Archive Commands

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

Craige_Hales
Super User

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
Level I

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);

 

hogi
Level XII

Re: How to unzip using JMP Zip Archive Commands

to get the respective entries, you have to use i as an index, e.g. dirlist[i] instead of dirlist
then the code from above works great.

 

za = Open( "$desktop\test.zip", zip );
dirlist = za << dir;

For( i = 1, i <= N Items( dirlist ), i++,
  blob = za << read( dirlist[i], Format( blob ) );

);

  

lala
Level VIII

回复: How to unzip using JMP Zip Archive Commands

  • Extract JSL I used myself:

RP = Run Program( Executable( "C:\Program Files\WinRAR\WinRAR.exe" ), Options( {"e -or C:\1\abc.7z C:\2"} ) );