Jump to content
Server Maintenance This Week. ×

Zip and Unzip any field -- without plugins


This topic is 1116 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Hi all,

you can zip and unzip any FileMaker field without a plugin. This article will describe the Mac side of things and I’m still looking for a way to do the same on Windows. Will post it here when I figure it out. (Don’t know yet how to send PowersShell commands in the background and get screen output back into FM.

1) Zipping

Pretty straightforward: 1.1) You export a container field to the TemporaryPath directory, 1.2) zip it and 1.3) insert the zipped file back into a container field. From then on you could store it in any type of field, just be sure to use the Bas64 functions, otherwise the contents will be changed when saving to a different field type.

A note on paths: The script steps that I use need two kinds of paths: a FileMaker path like "/Macintosh HD/var/folders/3f/fq556kns535272jz2d8pw4xh0000gn/T/S10/" (output from the FM function Get ( TemporaryPath )) and the same path in Mac OS notation "/var/folders/3f/fq556kns535272jz2d8pw4xh0000gn/T/S10/".  Notice the small difference, it matters.

To easily get any path in any kind of fashion, I have written a custom function GetPath ( "…" ) that gives me anything that I need right when I need it. Depending on the argument, I can get the temporary path in FileMaker notation or in mac notation (like in my example). I could also get the DesktopPath in both fashions, or the DocumentsPath, and as well the path to the current user directory (i.e. GetPath ( "user fm" ). I have included the custom function with this article. Theoretically, it can also create pc file paths (like "temp pc"), but I haven’t implememted that yet. In case you will, please let me know.

1.1 Export 

First, I create the FileMaker path and then I use it in FileMaker’s Export Field Contents script step. I then have the unzipped file in FileMaker’s temporary path.

1.2 Zip

Here, I create a Mac terminal command that looks like this:

zip -j /pathtotempfolder/.zip /pathtotempfolder/

And store this command in a variable $Cmd_mac. I pass this variable to the Perform Applescript script step, that in turn will send the command to the Mac terminal command line. To simplify this, I have written another custom function for this purpose: ShellScriptString ( $Cmd_mac ). It's very simple and consists of only one important line: "do shell script " & Quote ( ~cmd ). If you don’t have this custom function, you will go crazy with all the quoting and elevating that is needed in this procedure.

The "-j" option in the terminal command is to store only this one file into the zip archive, not the complete OS folder structure with it.

1.3 Insert zipped file

Again, I first create the appropriate path for the following Insert File script step, and then execute it.

Voilà, I now have a zipped file in my container field, that is pretty helpful in my case, because of so many files I need to store in my FM solution.

 

2) Unzipping

Doing the reverse is also pretty straightforward. In this case I am using the user’s home directory.

2.1 Export zipped file

Using the GetPath custom function, I create a FM path to the user’s home directory and then use the variable in the following Export Field Contents script step.

2.2 Unzip

To unzip, I use the following terminal command stored in the $Cmd_mac variable:

unzip -p /pathtofolder/file.zip > ~/file

That’s a bit tricky, because I first send the unzip output to stdout (-p) and from there into a file. If I don’t do that, I get write permission errors.

2.3 Insert unzipped file

Pretty straightforward: First I create a FM file path in the $File_fm variable, that I then use the Insert File script step. Now I have my (original) unzipped file back in FileMaker. This really saves space, when you have a lot of files.

2.4 Clean up home directory

If you don’t use the temporary folder for this operation, you need to clean up the two files that you created in your Mac home folder. Again, I’m creating a variable $Cmd_mac that contains this command line:

rm /pathtofolder/file ; rm /pathtofolder/file.zip

I left the actual variable that I’m using in the png, so that you can see that I can call my GetPath function also just with simple letters, like "u mac", for getting the Mac file path to the user’s home directory.

 

I hope this is helpful for someone. I created this procedure not because I dislike plugins, but at least in my experience they are a hassle when it comes to maintenance (updating, upgrading, paying, etc.). Therefore I like to keep it minimalistic.

Oh yes, You can even do the unzipping on the fly (using the onRecordLoad script step)!  It’s pretty darn fast.

 

Take care

 

Zip Script Steps.png

Unzip Script Steps.png

GetPath Custom Function.txt ShellScriptString Custom Function.txt

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.