User Tools

Site Tools


Zip Compress Each

Summary

Below is an example demonstrating how you can collect individual files into a zlib compressed (*.zip) file.

API Areas of Interest

Example

File_Zip_Compress_Each.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;
	};
 
	/***********************************************************************
	***** DsFileSystem Prototype *****
	***********************************************************************/
 
	/*********************************************************************/
	function DsFileSystem()
	{
	};
 
	/***********************************************************************/
	DsFileSystem.superclass = Object;
 
	/*********************************************************************/
	// Array<String|DzDir> : Method for collecting an array of directory objects
	DsFileSystem.prototype.getDirectories = function( oDir, regxFilter, nFilter, nSort, sType, bRecurse )
	{
		// Declare working variable
		var sAbsPath;
 
		// Get the directory names
		var aDirs = oDir.entryList( regxFilter, nFilter, nSort );
		// Iterate over the directory names
		for( var i = 0, nDirs = aDirs.length; i < nDirs; i += 1 ){
			// Get the absolute path of the 'current' dir
			sAbsPath = String( "%1/%2" ).arg( oDir.absPath() ).arg( aDirs[ i ] );
			// Based on the type requested
			switch( sType ){
				default:
				case "String":
					// Update the name with the absolute path of the directory
					aDirs[ i ] = sAbsPath;
					// If we are recursing
					if( bRecurse ){
						// Recursively collect the directory paths
						aDirs = aDirs.concat( this.getSubDirectories( new DzDir( sAbsPath ),
							regxFilter, nFilter, nSort, sType ) );
					}
					break;
				case "DzDir":					
					// Update the name with a directory object
					aDirs[ i ] = new DzDir( sAbsPath );
					// If we are recursing
					if( bRecurse ){
						// Recursively collect the directories
						aDirs = aDirs.concat( this.getSubDirectories( aDirs[ i ],
							regxFilter, nFilter, nSort, sType ) );
					}
					break;
			}
		}
 
		// Return the result
		return aDirs;
	};
 
	/*********************************************************************/
	// Array<String|DzDir> : Method for recursively collecting an array of directory objects
	DsFileSystem.prototype.getSubDirectories = function( oDir, regxFilter, nFilter, nSort, sType )
	{
		// Get the immediate child directories
		var aDirs = this.getDirectories( oDir, regxFilter, nFilter, nSort, sType, true );
		// Iterate over the directories
		for( var i = 0, nDirs = aDirs.length; i < nDirs; i += 1 ){
			// Based on the type requested
			switch( sType ){
				default:
				case "String":
					// Recursively collect the directory paths
					aDirs = aDirs.concat( this.getSubDirectories( new DzDir( aDirs[ i ] ),
						regxFilter, nFilter, nSort, sType ) );
					break;
				case "DzDir":	
					// Recursively collect the directories
					aDirs = aDirs.concat( this.getSubDirectories( aDirs[ i ],
						regxFilter, nFilter, nSort, sType, true ) );
					break;
			}
		}
 
		// Return the result
		return aDirs;
	};
 
	/*********************************************************************/
	// Array<String|DzFileInfo|DzFile> : Method for collecting an array of files
	DsFileSystem.prototype.getFiles = function( oDir, regxFilter, nFilter, nSort, sType )
	{
		// Declare working variable
		var sAbsFilePath;
 
		// Get the file names
		var aFiles = oDir.entryList( regxFilter, nFilter, nSort );
		// Iterate over the file names
		for( var i = 0, nFiles = aFiles.length; i < nFiles; i += 1 ){
			// Get the absolute path of the 'current' file
			sAbsFilePath = oDir.absFilePath( aFiles[ i ] );
			// Based on the type requested
			switch( sType ){
				default:
				case "String":
					// Update the name with the absolute path of a file
					aFiles[ i ] = sAbsFilePath;
					break;
				case "DzFileInfo":
					// Update the name with a file info object
					aFiles[ i ] = new DzFileInfo( sAbsFilePath );
					break;
				case "DzFile":
					// Update the name with a file object
					aFiles[ i ] = new DzFile( sAbsFilePath );
					break;
			}
		}
 
		// Return the result
		return aFiles;
	};
 
	/*********************************************************************/
	// Array<String|DzDir> : Method for retrieving a list of directories
	DsFileSystem.prototype.getDirectoryList = function( sPath, sFilter, sType, bRecurse, bRelative )
	{
		// Declare the output
		var aDirs = [];
 
		// Create a directory object
		var oBaseDir = new DzDir( sPath );
		// If the directory doesn't exist
		if( !oBaseDir.exists() ){
			// We are done...
			return aDirs;
		}
 
		// Get the directories
		var aDirs = this.getDirectories( oBaseDir, sFilter,
			DzDir.Dirs | DzDir.NoDotAndDotDot, DzDir.Name, sType, bRecurse );
 
		// If we do not want relative
		if( !bRelative ){
			// Return the result
			return aDirs;
		}
 
		// Declare working variable
		var sAbsPath, sRelPath;
		var oDir;
 
		// Iterate over the directories
		for( var i = 0, nDirs = aDirs.length; i < nDirs; i += 1 ){
			// Based on the type requested
			switch( sType ){
				default:
				case "String":
					// Get the 'current' path
					sAbsPath = aDirs[ i ];
					// Get the relative portion of the path
					sRelPath = sAbsPath.substring( sPath.length );
					// Update the path
					aDirs[ i ] = sRelPath;
					break;
				case "DzDir":
					// Get the 'current' directory
					oDir = aDirs[ i ];
					// Get the path
					sAbsPath = oDir.path();
					// Get the relative portion of the path
					sRelPath = sAbsPath.substring( sPath.length );
					// Update the path
					oDir.setPath( sRelPath );
					// Update the directory
					aDirs[ i ] = oDir;
					break;
			}
		}
 
		// Return the result
		return aDirs;
	};
 
	/*********************************************************************/
	// Array<String|DzFileInfo|DzFile> : Method for retrieving a list of files
	DsFileSystem.prototype.getFileList = function( sPath, sFilter, sType, bRecurse )
	{
		// Declare the output
		var aFiles = [];
 
		// Get the directories
		var aDirs = this.getDirectoryList( sPath, "*", "DzDir", bRecurse );
		// Iterate over the directories
		for( var i = 0, nDirs = aDirs.length; i < nDirs; i += 1 ){
			// Append the files from the 'current' directory to the output
			aFiles = aFiles.concat(
				this.getFiles( aDirs[ i ], sFilter, DzDir.Files, DzDir.Name, sType ) );
		}
 
		// Return the result
		return aFiles;
	};
 
	/*********************************************************************/
	// 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 source directory path
	var sSourcePath = String("%1/%2/%3")
		.arg( sAppData )
		.arg( App.orgName )
		.arg( sBuild );
 
	// 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 list of relative paths to zip
	var aRelativePaths = [
		"actions.dsx",
		"customactions.dsx",
		"layout.dse",
		"layout.dsx",
		"menus.dsx",
		"toolbars.dsx"
	];
 
	// Declare working variable
	var sAbsFilePath, sRelFilePath;
 
	// Construct the user layouts path
	var sLayoutsPath = String("%1/user layouts").arg( sSourcePath );
	// Create a file system object
	var oFileSystem = new DsFileSystem();
	// Get all of the files in the directory
	var aFiles = oFileSystem.getFileList( sLayoutsPath, "*", "String", true );
	// Iterate over the files
	for( var i = 0, nFiles = aFiles.length; i < nFiles; i += 1 ){
		// Get the 'current' file
		sAbsFilePath = aFiles[ i ];
		// Get the relative portion of the path
		sRelFilePath = sAbsFilePath.substring( sSourcePath.length + 1 );
		// Update with the relative path
		aFiles[ i ] = sRelFilePath;
	}
	// Append any user layouts found
	aRelativePaths = aRelativePaths.concat( aFiles );
 
	// Create a zip file object
	var oZipFile = new DzZipFile( String("%1/DS_Layouts_Backup.zip").arg( sTargetPath ) );
	// Open the zip file for writing
	oZipFile.open( DzZipFile.WriteOnly );
 
	// Iterate over the relative paths
	for( var i = 0, nFiles = aRelativePaths.length; i < nFiles; i += 1 ){
		// Get the 'current' relative path
		sRelFilePath = aRelativePaths[ i ];
		// Add the file to the zip
		oZipFile.addFileToZip( String("%1/%2").arg( sSourcePath ).arg( sRelFilePath ), sRelFilePath );
	}
 
	// Close the zip file
	oZipFile.close();
 
// Finalize the function and invoke
})();