Build archives on the fly

For 13.1 we get new Archive functions to build a zip archive on the fly. One of the use of deprecated ZipFile functions was to assemble an archive in memory from various files or containers. You can now do that with our Archive.Create function. Just call it to either start an on-disk or in-memory archive using the compression format and compression filter of your choice. We usually use zip for maximum compatibly, but you can also do tar or 7zip.

Next you can add content. For that you may use Archive.AddContainer to pass a container value as variable or pass a field reference. We read the content of the container and compress it in memory. If you have existing files on disk, you can use Archive.AddFile function. Please note that you can pass a list of files with relative path parts, e.g. "Documents/email.pdf¶Documents/answer.pdf" and then the base path to find them like "C:\Users\cs\Desktop". The relative file paths end up in the archive, so they are used when expanding the archive. If you like to add a file based of text, you can use Archive.AddText to create a text file directly in the new archive.

Let us show you an example script to zip up a few PDFs in records:

Set Variable [ $r ; Value: MBS( "Archive.Create"; "zip"; "deflate") ] 
If [ MBS("IsError") ] 
	Show Custom Dialog [ "Failed to create zip archive." ; $r ] 
	Exit Script [ Text Result:    ] 
End If
# 
Go to Record/Request/Page [ First ]
Set Variable [ $destPage ; Value: 1 ] 
Loop
	Set Variable [ $r ; Value: MBS( "Archive.AddContainer"; Merge PDFs::InputPDF) ] 
	Go to Record/Request/Page [ Next ; Exit after last: On ]
End Loop
Set Variable [ $zip ; Value: MBS( "Archive.Close"; "test.zip") ] 
If [ MBS("IsError") ] 
	Show Custom Dialog [ "Failed to create zip archive." ; $r ] 
Else
	Set Field [ Merge PDFs::FinalPDF ; $zip ] 
End If

As you see, we create an in-memory archive, loop over records and add PDF containers to it. Then we close the archive passing the file name and store it in a container field.

You can do the same with an archive file on disk by passing a native file path to the Archive.Create function:

Set Variable [ $path ; Value: MBS( "Path.AddPathComponent"; MBS( "Folders.UserDesktop" ); "test.zip" ) ] 
Set Variable [ $r ; Value: MBS( "Archive.Create"; "zip"; "deflate"; $path) ] 
If [ MBS("IsError") ] 
	Show Custom Dialog [ "Failed to create zip archive." ; $r ] 
	Exit Script [ Text Result:    ] 
End If
# 
Go to Record/Request/Page [ First ]
Set Variable [ $destPage ; Value: 1 ] 
Loop
	Set Variable [ $r ; Value: MBS( "Archive.AddContainer"; Merge PDFs::InputPDF) ] 
	Go to Record/Request/Page [ Next ; Exit after last: On ]
End Loop
Set Variable [ $r ; Value: MBS( "Archive.Close") ] 

Otherwise the script is the same and does the same functionality.

You may have noticed, that we don't use reference numbers here. That is because we keep the current archive stored in memory in relation to the current thread. This allows each script on a server to have their own current archive without using numbers.

Please try the functions and let us know your questions. The example script above is included in the Merge PDFs

4 Likes