Zipping Files In Memory With Xojo
If you are generating a lot of files in Xojo to be downloaded, it makes sense to zip your files, so that the end-user has only to download one file.
You can easily achieve this with MonkeyBread’s MBS Plugin: ArchiveWriterMBS, which you can download here.
The following code is showing how to create 3 text files in-memory. These files are then getting compressed in-memory and then automatically downloaded. This project is for Xojo Web 2.0 but will work as well for Desktop Apps with a few changes.
// Array to store the created files
var files() as WebFile
// >> created as properties in the IDE (e.g. file1 as webfile)
file1 = new WebFile
file2 = new WebFile
file3 = new WebFile
zippedFile = new Webfile
// << created as properties in the IDE (e.g. file1 as webfile)
// Populating the webfiles and adding them to an array
file1.MimeType = "text/plain"
file1.ForceDownload = False
file1.Filename = "file1.txt"
file1.Data = "Lore Lipsum 1"
files.add(file1)
file2.MimeType = "text/plain"
file2.ForceDownload = False
file2.Filename = "file2.txt"
file2.Data = "Lore Lipsum 2"
files.add(file2)
file3.MimeType = "text/plain"
file3.ForceDownload = False
file3.Filename = "file3.txt"
file3.Data = "Lore Lipsum 3"
files.add(file3)
// Defining the ZIP File
zippedFile.MimeType = "application/zip"
zippedFile.ForceDownload = true
zippedFile.Filename = "myCompressedFiles.zip"
// defining the zip file in memory
Var myZip as new ArchiveWriterMBS
myZip.SetFormatZip
myZip.ZipSetCompressionDeflate
var check as boolean = myZip.CreateMemoryFile
if check then
// Looping over all the files in my array of files
for each file as webfile in files
// creating an entry in the zip file per webfile in the array
var e as new ArchiveEntryMBS
e.PathName = file.Filename
e.Size = lenb( file.Data )
e.Permissions = &o0644
e.FileType = e.kFileTypeRegular
myzip.WriteHeader e
call myZip.WriteData file.Data
myZip.FinishEntry
next
myZip.Close
end if
// copy the Zip-File from the memory into the in-memory ZIP-WebFile
zippedFile.data = myZip.MemoryData
// download the zip File
gotoUrl( zippedFile.url )
You need to create the following properties in Xojo’s IDE:

Comments
Sign in or become a blog.xojoDOCs.com member to join the conversation.
Just enter your email below to get a log in link.