User Tools

Site Tools


Zip Extract Each

Summary

Below is an example demonstrating how you can extract the individual entries of a zlib compressed (*.zip) archive.

API Areas of Interest

Example

File_Zip_Extract_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;
	};
 
	/*********************************************************************/
	// 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 source directory path
	var sSourcePath = "c:/temp";
 
	// Create a directory object
	var 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 target directory path
	var sTargetPath = "c:/temp";
 
	// Create a directory object
	oDir = new DzDir( sTargetPath );
	// If the directory doesn't exist
	if( !oDir.exists() ){
		// Create all the missing directories in the path
		oDir.mkpath( sTargetPath );
	}
 
	// Define the basename of the zip file
	var sBasename = "DS_Layouts";
 
	// Create a zip file object
	var oZipFile = new DzZipFile( String("%1/%2_Backup.zip").arg( sSourcePath ).arg( sBasename ) );
	// If the file doesn't exist
	if( !oZipFile.exists() ){
		// Provide feedback
		print( oZipFile.filePath(), "does not exist!" );
		// We are done...
		return;
	}
 
	// Open the zip file for reading
	oZipFile.open( DzZipFile.ReadOnly );
 
	// Start at the first file
	var bNext = oZipFile.goToFirstFile();
	// Do the following at least once
	do {
		// If the entry is a folder
		if( oZipFile.getCurrentFileIsFolder() ){
			// Provide feedback
			print( "Folder:", oZipFile.getCurrentFileName() );
			print( "\tComment:", oZipFile.getCurrentFileComment() );
		// If the entry is anything else
		} else {
			// Provide feedback
			print( "File:", oZipFile.getCurrentFileName() );
			print( "\tComment:", oZipFile.getCurrentFileComment() );
			print( "\tAtributes:", oZipFile.getCurrentFileAtributes() );
			print( "\tUncompressed Size:", App.byteSizeToString( oZipFile.getCurrentFileUncompressedSize() ) );
			print( String("\tCompressed Size: %1 (%2%)").arg( App.byteSizeToString( oZipFile.getCurrentFileCompressedSize() ) )
				.arg( 100 - oZipFile.getCurrentFileCompressionRatio() ) );
			print( "\tCompression Method:", oZipFile.getCurrentFileCompressionMethod() );
			print( "\tCRC-32:", oZipFile.getCurrentFileCRC() );
		}
 
		// If the current entry cannot be extracted
		if( !oZipFile.extractCurrentFile( String("%1/%2").arg( sTargetPath ).arg( sBasename ) ) ){
			// Provide feedback
			print( "\tExtraction failed." );
		// If the current entry can be extracted
		} else {
			// Provide feedback
			print( "\tExtraction succeeded." );
		}
	// Continue if another file
	} while( oZipFile.goToNextFile() );
 
	// Close the zip file
	oZipFile.close();
 
// Finalize the function and invoke
})();