cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Choose Language Hide Translation Bar
Compress a Folder into a Zip File

 

Problem

 

You need to compress an entire folder into a single ".zip" file.

 

Solution

 

Use JMP's built-in support for compressing files to create a ZipArchive object. Once you have a ZipArchive object, you can write blob representations of the uncompressed files to the ".zip" file.

 

/*
	Arguments:
		folderToZip - "path" - path to the folder you want to compress
		zipFileLoc - "path" - path to the output zip file
		overwriteFile - <0|1> - do you want to overwrite an existing file. default is 0
		recursive - <0|1> - do you want all files in the folder or just top leve. default is 1
*/
zipFolder = Function( {folderToZip, zipFileLoc, overwriteFile = 0, recursive = 1},
	{Default Local}, 
	
	// Convert the filepath to the OS and add a trailing slash if one is missing
	If( Host is( "Windows" ),
		folderToZip = Convert File Path( folderToZip, "Windows" );
		zipFileLoc = Convert File Path( zipFileLoc, "Windows" );
		If( Substr( folderToZip, -1 ) != "\",
			folderToZip = Concat( folderToZip, "\" )
		);
	,
		folderToZip = Convert File Path( folderToZip, "posix" );
		zipFileLoc = Convert File Path( zipFileLoc, "posix" );
		If( Substr( folderToZip, -1 ) != "/",
			folderToZip = Concat( folderToZip, "/" )
		);
	);
	
	// Check to see if the file exst
	If( File Exists( zipFileLoc ),
		
		Match( overwriteFile,
			0, // if we should not overwrite, write to the log and return 0
				Write( "\!NFile already exists" );
				Return( 0 );
		,
			1, // else just delete the existing file
				Delete File( zipFileLoc ),
		);
	);
	
	// create the zipfile and ZipArchive object (za)
	za = Open( zipFileLoc, zip );
	
	// get our list of files to compress, based on recursive setting
	If( recursive == 1,
		dirlist = Files In Directory( folderToZip, "recursive" ),
		dirlist = Files In Directory( folderToZip )
	);	
	
	// loop through each file
	For( iFile = 1, iFile <= N Items( dirlist ), iFile++, 
		
		// read in the uncompressed file as a blob
		tmpBlob = Load Text File( folderToZip || dirlist[iFile], blob );
		
		// write compressed file to the ZipArchive object
		za << Write( dirlist[iFile], tmpBlob );
	);
	
	Return( 1 );
);

Discussion

 

I've used variations of this function in several add-ins. Hopefully this will get you started with compressing your files!

See Also

JSL Syntax Reference > JSL Messages > Other Objects > Zip Archive

Scripting Index > ZipArchive

 

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.