Create zip file on Mac

Hello,

I was going through the manage => http://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/zip.1.html
and this is what I want…

Command format. The basic command format is

          zip options archive inpath inpath ...

   where  archive  is  a  new  or existing zip archive and inpath is a directory or file path optionally
   including wildcards.  When given the name of an existing zip archive, zip  will  replace  identically
   named  entries  in  the  zip  archive  (matching  the relative names as stored in the archive) or add
   entries for new names.  For example, if foo.zip exists and contains foo/file1 and foo/file2, and  the
   directory foo contains the files foo/file1 and foo/file3, then:

          zip -r foo.zip foo

   or more concisely

          zip -r foo foo

How can I implement that on Mac…
The folder I want to “zip” is special folder.Desktop.child("The unzipped docs)

Thanks.

Lennox

Check out Thomas Tempelmann’s Zip Package:

http://www.tempel.org/RB/ZipPackage

There are issues when using the command line Zip function, namely it includes the full path in the compressed file. I’d suggest checking into TT’s Zip as Kem mentioned.

This is not my experience. It uses the standard unix convention of taking the input literally. To make a relative directory tree you just need to compress from one level above.

/Users/Eduo$ zip -r files /Users/Eduo/Desktop/Files/*
Final zip file contains /Users/Eduo/Desktop/Files/*

/Users/Eduo$ zip -r files Desktop/Files/*
Final zip file contains Desktop/Files/*

So the trick is to first move to the directory above the one you want to compress, and then zip that. If you pass the full path from elsewhere you compress the full path (but just the contents you intend).

Tar works in the same way.

OSX has ZIP and TAR functions built in… no need for 3rd party plugins if you are willing to use the SHELL to call the command directly

I didn’t know that, but then if I need to zip a specific file, I must add a filter tight enough to capture only that file?

/Users/Eduo$ zip -r files Desktop/Files/*yImage.jpg

if I wanted to capture the file myImage.jpg in the folder “Files” on the desktop.

If you use any of the shell functions in OS/X will your application get accepted if you
submit to the app store??

[quote=21212:@George Balla]If you use any of the shell functions in OS/X will your application get accepted if you
submit to the app store??[/quote]

You’d likely need to use some sort of Entitlement to allow access to the specific function you need, and you’d need to justify using the shell to access that function instead of some other API.

I’m not sure what entitlement you’d need to add though… https://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/AboutEntitlements.html#//apple_ref/doc/uid/TP40011195

Remember that -r means “recursive”. If you remove it then you store only the files, not their paths.

If you still wanted to use -r then move to Desktop/Files first and from there run zip so your “root” for relative paths is that folder.

[quote=21226:@Thomas Erwin]You’d likely need to use some sort of Entitlement to allow access to the specific function you need, and you’d need to justify using the shell to access that function instead of some other API.

I’m not sure what entitlement you’d need to add though… https://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/AboutEntitlements.html#//apple_ref/doc/uid/TP40011195[/quote]

IIRC you can call the shell class from sandboxed applications. Actually from an old post by @Sam Rowlands

http://forums.realsoftware.com/viewtopic.php?p=248564&sid=c921439ed04fd706cae367fa53d146c7#p248564

Thanks for all the replies…

I got this from another forum…

*********** quote************
For a Mac only solution, I use a shell script :

Dim cmd, qPosixOriginalFolder, qPosixBackupFile, theResult As String
Dim sh As Shell

qPosixOriginalFolder = source.ShellPath
qPosixBackupFile = destination.ShellPath

cmd = "/usr/bin/ditto -c -k --rsrc --keepParent "+ qPosixOriginalFolder + " " + qPosixBackupFile

sh = new Shell
sh.execute cmd

If sh.errorCode = 0 then
theResult = sh.result
else
MsgBox "Error " + Str(sh.errorCode) + " : " + sh.Result
end if

Best regards.

Franois
*********** end quote************

… and that works for me.

Thanks again.

Lennox

This is another one…
http://macscripter.net/viewtopic.php?pid=85208

(* 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

Lennox

1 Like

[quote=21347:@Lennox Jacob]Thanks for all the replies…

I got this from another forum…

*********** quote************
For a Mac only solution, I use a shell script :

Dim cmd, qPosixOriginalFolder, qPosixBackupFile, theResult As String
Dim sh As Shell

qPosixOriginalFolder = source.ShellPath
qPosixBackupFile = destination.ShellPath

cmd = "/usr/bin/ditto -c -k --rsrc --keepParent "+ qPosixOriginalFolder + " " + qPosixBackupFile

sh = new Shell
sh.execute cmd

If sh.errorCode = 0 then
theResult = sh.result
else
MsgBox "Error " + Str(sh.errorCode) + " : " + sh.Result
end if

Best regards.

François
*********** end quote************

… and that works for me.

Thanks again.

Lennox[/quote]

yes that works fine, but how to unzip?

Crash on sh.execute cmd

I use in DropDown

[code]
/// unzip
if right (item.name,3)=“zip” then
Dim sh As Shell
Dim cmd as String
dim dlg as New SelectFolderDialog
dim f as FolderItem
dlg.ActionButtonCaption = “Select”
dlg.Title = “Data Folder”
dlg.PromptText = “Select Folder”
f = dlg.ShowModalWithin(Window1)
cmd = "unzip -a " + item.ShellPath + " -d " + f.ShellPath
if f <> nil then
sh.execute cmd /// crash here!!
else
return
end
else
///// zip
dim source, destination as FolderItem
Dim cmd, qPosixOriginalFolder, qPosixBackupFile, theResult As String
Dim sh As Shell
source=Item
dim dlg as SaveAsDialog
dlg=new SaveAsDialog
dlg.Filter=FileTypes1.ApplicationZip
dlg.SuggestedFileName=item.Name
destination=dlg.ShowModalWithin(Window1)

if destination<>nil then
  qPosixOriginalFolder = source.ShellPath
  qPosixBackupFile = destination.ShellPath
  cmd = "/usr/bin/ditto -c -k -X --rsrc  "+ qPosixOriginalFolder + " " + qPosixBackupFile
  sh = new Shell
  sh.execute cmd
  
  If sh.errorCode = 0 then
    theResult = sh.result
    return
  else
    MsgBox "Error " + Str(sh.errorCode) + " : " + sh.Result
    return
  end if
else
  return
end

end[/code]

I got it.

replaced

“Dim sh As Shell” with “Dim sh As New Shell” and it worked.

Hi,

I created a temporary Folder “MyFolder” with Files and Subfolders inside:

[code]MyFolder

  • ABC.txt
  • Other Folder
    ++ new.txt[/code]
    I only want to store the Files and Folders inside of MyFolder into Zip. How will the right OS X Shell-Command looks like?

Dim savePath As String = GetFolderItem("Test.zip").ShellPath saves a huge Folderstructure inside…

[quote=19611:@Kem Tekinay]Check out Thomas Tempelmann’s Zip Package:

http://www.tempel.org/RB/ZipPackage[/quote]

Thanks Tim,
But i want to use the OS X way without using other Libraries :wink:

@Martin: TTs ZipPackage IS the Mac OS way. But have a go at it yourself. Open Terminal and type “man zip”.

AFAIR the ditto command also can do zip files.

There is surely a command line option. What about -j ? Does OS X have Unix man pages - if so look there?