An universal routine to create a zip archive from a file or folder

¢ ZipIt creates a zip archive form a file or folder
¢ In case of a file the name of the zip archive omits the file extension of the original file (abc.pdf → abc.zip)
¢ the parameter z can be alias, file specifier or HFS-path
¢ with erase deletes the source file/folder


ZipIt from (choose file) with erase
ZipIt from (choose folder) without erase

on ZipIt from z given erase:erase
	set {name:Nm, name extension:Ex, folder:Fo, package folder:Pa} to info for z as alias
	set source to POSIX path of z as alias
	if Fo and Pa is false then
		set source to text 1 thru -2 of source
		set destination to source & ".zip"
	else
		set parentFolder to text 1 thru ((offset of Nm in source) - 1) of source
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set destination to parentFolder & Nm & ".zip"
	end if
	try
		do shell script "/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of source & " " & quoted form of destination
		if erase then do shell script "rm -r " & quoted form of source
		return POSIX file destination as alias
	on error
		return false
	end try
end ZipIt

stefan ,iam stil a newbie in AS
and i clearly dont unersand what most of code do , can you add comments to your code , after every line of code?

OK, here the commented version


-- create a zip archive from a chosen file and delete the original file
ZipIt from (choose file) with erase
-- create a zip archive from a chosen folder and leave the original folder untouched
ZipIt from (choose folder) without erase

-- Subroutine
on ZipIt from z given erase:erase
	-- get some file info of the source file
	set {name:Nm, name extension:Ex, folder:Fo, package folder:Pa} to info for z as alias
	-- get the POSIX path of the source
	set source to POSIX path of z as alias
	-- check if folder (folder is true and package folder is false) or file
	if Fo and Pa is false then
		-- cut the slash at the end of the folder path
		set source to text 1 thru -2 of source
		-- specify destination file path
		set destination to source & ".zip"
	else
		-- get parent folder of source file
		set parentFolder to text 1 thru ((offset of Nm in source) - 1) of source
		-- strip file extension from the file name
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		-- specify destination file path
		set destination to parentFolder & Nm & ".zip"
	end if
		try
		-- create archive
		do shell script "/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of source & " " & quoted form of destination
		-- if boolean parameter erase is true then delete original file/folder
		if erase then do shell script "rm -r " & quoted form of source
		-- return the alias of the created archive as result of the subroutine
		return POSIX file destination as alias
	on error
		-- if an error occured return boolean false
		return false
	end try
end ZipIt

Finally, an extended version which can handle single and multiple files or folders with a few more parameters


(* `ZipIt` parameters
   ---------------------
       someSource         [mixed]   file or folder, list of files or folders. Must be an alias, file, or HFS path or a list of these classes
       destinationFolder  [mixed]   destination folder. Must be an alias, file, or HFS path. Leaving it empty ("" or missing value) saves the archive in the directory of the source
       erase                   [bool]     if true, erase files/folders after creating the archive
      keepParent            [bool]      if true, keep original name of parent folder while creating an archive of a folder
      
      result: the alias of the created archive or boolean false, if an error occured
       
*)
on ZipIt from someSource given erase:erase, keepParent:keepParent, destinationFolder:dFolder
	if class of someSource is list then
		set tempFolder to quoted form of (POSIX path of (path to temporary items) & "zip_temp_folder")
		try
			do shell script "mkdir " & tempFolder
		on error
			try
				do shell script "rm -r " & tempFolder & "/*"
			end try
		end try
		repeat with i in someSource
			set s to POSIX path of (i as alias)
			if s ends with "/" then set s to text 1 thru -2 of s
			do shell script "cp -pr " & quoted form of s & space & tempFolder
		end repeat
		set {name:Nm} to info for item 1 of someSource as alias
		set source to POSIX path of (item 1 of someSource as alias)
		try
			set destination to POSIX path of (dFolder as alias) & "Archive.zip"
		on error
			set destination to text 1 thru ((offset of Nm in source) - 1) of source & "Archive.zip"
		end try
		try
			do shell script "/usr/bin/ditto -c -k -rsrc " & tempFolder & space & quoted form of destination
			if erase then
				repeat with i in someSource
					do shell script "rm -r " & quoted form of POSIX path of (i as alias)
				end repeat
			end if
			try
				do shell script "rm -r " & tempFolder
			end try
			return POSIX file destination as alias
		on error
			return false
		end try
	else
		set k to space
		if keepParent then set k to " --keepParent "
		set {name:Nm, name extension:Ex, folder:Fo, package folder:Pa} to info for someSource as alias
		set source to POSIX path of (someSource as alias)
		if Fo and Pa is false then
			try
				set destination to text 1 thru -2 of (POSIX path of (dFolder as alias)) & ".zip"
			on error
				set destination to text 1 thru -2 of source & ".zip"
			end try
		else
			if Ex is missing value then set Ex to ""
			if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
			try
				set destination to POSIX path of (dFolder as alias) & Nm & ".zip"
			on error
				set destination to text 1 thru ((offset of Nm in source) - 1) of source & Nm & ".zip"
			end try
		end if
		try
			do shell script "/usr/bin/ditto -c -k -rsrc" & k & quoted form of source & space & quoted form of destination
			if erase then do shell script "rm -r " & quoted form of source
			return POSIX file destination as alias
		on error
			return false
		end try
	end if
end ZipIt

-- Examples

ZipIt from (choose file with multiple selections allowed) without erase and keepParent given destinationFolder:path to desktop
ZipIt from (choose folder) with keepParent without erase given destinationFolder:""
tell application "Finder"
	set s to files of (path to pictures folder) whose name extension is "jpg"
end tell
ZipIt from s without keepParent and erase given destinationFolder:choose folder