User Tools

Site Tools


Zip Compress All

Summary

Below is an example demonstrating how you can collect the contents of directory into a zlib compressed (*.zip) file.

API Areas of Interest

Example

File_Zip_CompressAll.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	/*********************************************************************/
	// String : A function for retrieving a translation if one exists
	function text( sText )
	{
		// If the version of the application supports qsTr()
		if( typeof( qsTr ) != "undefined" ){
			// Return the translated (if any) text
			return qsTr( sText );
		}
 
		// Return the original text
		return sText;
	};
 
	/*********************************************************************/
	// If the application version is less than 4.9.3.149
	if( App.version64 < 0x0004000900030095 ){
		// Provide feedback
		MessageBox.information(
			text( "This script requires %1 4.9.3.149 or newer to continue." )
				.arg( App.appName ), text( "Version Error" ), text( "&OK" ) );
		// We are done..
		return;
	}
 
	// Define the target directory path
	var sTargetPath = "c:/temp";
 
	// Create a directory object
	var oDir = new DzDir( sTargetPath );
	// If the directory doesn't exist
	if( !oDir.exists() ){
		// Create all the missing directories in the path
		oDir.mkpath( sTargetPath );
	}
 
	// Get the "AppData" path
	var sAppData = System.getenv( "AppData" );
 
	// Initialize the build
	var sBuild = "Studio4";
	// Depending onthe release cycle, update
	switch( App.releaseCycle() ){
		default:
		case DzApp.GeneralRelease:
			break;
		case DzApp.PublicBuild:
		case DzApp.PublishingBuild:
		case DzApp.PrivateBuild:
		case DzApp.DevBuild:
			sBuild += " " + App.releaseCycleString();
			break;
	}
 
	// Define the name of the directory to backup
	var sSourceDirName = "ShaderBuilder";
 
	// Define the source directory path
	var sSourcePath = String("%1/%2/%3/%4")
		.arg( sAppData )
		.arg( App.orgName )
		.arg( sBuild )
		.arg( sSourceDirName );
 
	// Create a directory object
	oDir = new DzDir( sSourcePath );
	// Get whether or not the directory exists
	var bExists = oDir.exists();
 
	// If the directory doesn't exist
	if( !bExists ){
		// Provide feedback
		print( sSourcePath, "does not exist!" );
		// We are done...
		return;
	}
 
	// Define the basename of the destination file
	var sBasename = String("%1_Backup").arg( sSourceDirName );
 
	// Let the user know we are busy
	setBusyCursor();
 
	// Create a zip file object
	var oZipFile = new DzZipFile( String("%1/%2.zip").arg( sTargetPath ).arg( sBasename ) );
	// Open the zip file for writing
	oZipFile.open( DzZipFile.WriteOnly );
 
	// Add files/folders in the path to the zip file
	oZipFile.compressAll( sSourcePath, true );
 
	// Close the zip file
	oZipFile.close();
 
	// Let the user know we are done
	clearBusyCursor();
 
// Finalize the function and invoke
})();