compress/decompress on OS X

Rob Cozens rcozens at pon.net
Thu Nov 4 12:13:05 EST 2004


Hi Rob,

>My solution (from Serendipity Library & called by SDB Utilities and 
>SDB Tools) is below;

A lot of the text in the handlers I posted is not necessary for the 
limited task of compressing/decompressing a single file--remember the 
handlers were designed to be called within SDB Utilities & SDB Tools.

For example, saving source & destination folder paths and placing the 
compression/decompression logic in a repeat loop was done to 
streamline the process of manually compressing or decompressing 
several files in the same folder.  Also, there is a lot of logic just 
checking for long file names in Mac OS Classic and setting the 
archive file type.  Finally, there is logic to capture all 
information the detailedFiles function returns for the compressed 
file-- now or in the future [see sgzFooter() in my previous post] -- 
and deal with Mac OS Creator/File Type defaults for files compressed 
on a non-Mac system.

on SDBCompressFile filePath,archivePath,archiveType -- minimal code 4 
Nov 04:RCC
   put URL ("binfile:"&filePath) into sourceData
   get the result
   if it is not empty then
     beep
     answer error it
     exit SDBCompressFile
   end if
   if the platform is "MacOS" then
     put URL ("resfile:"&filePath) into sourceResource
     get the result
     if it is not empty then
       beep
       answer error it
       exit SDBCompressFile
     end if
   else put empty into sourceResource
   get sgzFooter(filePath)
   if the number of items of it < 2 then
     put length(sourceData) into item 2 of it
     put "????????" into item 11 of it
   end if
   put numToChar(29)&it into dataSuffix
   if sourceResource is empty then put compress(sourceData&dataSuffix) 
into compressedData
   else put compress(sourceData&sourceResource&dataSuffix) into compressedData
   get the result
   if it is not empty then
     beep
     answer error it
     exit SDBCompressFile
   end if
   put "binfile:"&&archivePath into compressedFile
   put the fileType into savedType
   set the fileType to archiveType
   put compressedData into URL compressedFile
   get the result
   set the fileType to savedFileType
   if it is not empty then
     beep
     answer error it
   end if
end SDBCompressFile

on SDBExpandFile archivePath,folderPath -- minimal?? code 4 Nov 04:RCC
   put (the platform is not "MacOS") into runningMacOS
   put decompress(URL "binfile:"&archivePath) into expandedData
   get the result
   if it is not empty then
     beep
     answer error it
     exit SDBExpandFile
   end if
   put numToChar(29) into groupSeparator
   put empty into expandedResource
   repeat with x = length(expandedData) down to 1
     get char x of expandedData
     if it is groupSeparator then
       put char (x+1) to -1 of expandedData into recordFooter
       put URLDecode(item 1 of recordFooter) into originalFileName
       put item 2 of recordFooter into dataForkBytes
       put item 3 of recordFooter into resourceForkBytes
       put item 11 of recordFooter into compressedFilesType
       delete char x to -1 of expandedData
       exit repeat
     end if
   end repeat
   if resourceForkBytes <> 0 then
     if not runningMacOS then
       beep
       answer error "File is in Mac OS format!"
       exit SDBExpandFile
     else
       put dataForkBytes+1 into firstResourceChar
       put char firstResourceChar to -1 of expandedData into expandedResource
       delete char firstResourceChar to -1 of expandedData
     end if
   else put empty into expandedResource
   put folderPath&originalFileName into filePath
   put the fileType into savedType
   if runningMacOS and compressedFilesType is "0" then
     ask question "Enter a creator code & file type:" with "ttxtTEXT"
     if length(it) <> 8 then get "ttxtTEXT"
     put it into compressedFilesType
   end if
   set the fileType to compressedFilesType
   put expandedData into URL ("binfile:"&filePath)
   get the result
   set the fileType to savedFileType
   if it is not empty then
     beep
     answer error it
     exit SDBExpandFile
   end if
   if expandedResource is not empty then
     put expandedResource into URL ("resfile:"&expandedFile)
     get the result
     if it is not empty then
       beep
       answer error it
     end if
   end if
   set the fileType to savedFileType
end SDBExpandFile

-- 
Rob Cozens, Staff Conservator
Mendonoma Marine Life Conservancy

"...fishers came to us and said, 'Look! You've closed one of our best 
fishing grounds.'  Our response was, 'But you told us that you didn't 
fish there.'  They then responded, 'But surely you didn't expect us 
to tell you where our best fishing grounds were!'"

    -- Graeme Kelleher, Great Barrier Reef Marine Park Authority, in 
MPA News, March, 2003


More information about the use-livecode mailing list