Saturday, November 28, 2009

Unzipping a file using PHP

The correct way is to use the unzip library but there are various problems with that approach that I won't harp on here. Another correct way is to use

exec("unzip /path/to/file.zip");

but I have found some zip files that the unix unzip command can't handle but that the mac os x built-in archive utility has no problem with. So I came up a simple, but goofy approach:

use php exec() to call an applescript which tells finder to open the file with archive utility, using a keyboard shortcut.

PHP blah.php
exec('osascript /applications/mamp/htdocs/zipfolder/asunzip.scpt');

APPLESCRIPT asunzip.scpt

tell application "Finder"
activate
end tell

tell application "System Events"
tell process "Finder"
key code 125 — down arrow
keystroke "z" using command down
end tell
end tell

KEYBOARD SHORTCUT - application-specific (system preferences)
command z is pulling file -> open with -> archive utility out of finder toolbar

———————————Addendum———————————————————

Here is a better AppleScript that does the same thing. But instead of bringing Finder to the forefront, then using down arrows to navigate to a file, and then using a keyboard shortcut to open it, it just directly opens the file by name quietly in the background:

set filepath to POSIX path of "Macintosh HD:Applications:MAMP:htdocs:zipfolder:neworig.zip"
application
try
set command to "open " & quoted form of filepath
do shell script command
end try

No comments:

Post a Comment