Zip compression of file uploads prior to emailing them
Moderators: Jon, Steve, Ian, Dave
-
- Ebase User
- Posts: 4
- Joined: Fri Sep 05, 2014 12:01 pm
- Location: Wandsworth, London
- Contact:
Zip compression of file uploads prior to emailing them
Is there a way in Ebase of compressing files (in a zip compatible way) that have been uploaded via the uploadFileFromBrowser() method prior to emailing the resultant zip file as an attachment? I am aware that Java has its java.util.zip package but didn't want to explore that route if there was already some default functionality for doing the same thing built into Ebase.[/url]
0 x
-
- Moderator
- Posts: 1342
- Joined: Wed Sep 12, 2007 12:49 pm
-
- Ebase User
- Posts: 331
- Joined: Mon Mar 10, 2014 8:34 am
Thanks both, I'd had a vague conversation with a colleague about creating a routine that attaches a number of files and emails them. We hadn't got to the point of developing it but this gave me a focus.
For info and Jon to comment whether this all makes sense (it works).
The principle was lifted from http://examples.javacodegeeks.com/core- ... putstream/
Thanks
For info and Jon to comment whether this all makes sense (it works).
Code: Select all
importPackage(com.ebasetech.xi.api);
importPackage(com.ebasetech.xi.services);
importPackage(java.io);
importPackage(java.util.zip);
var zipFile = "D:/archive.zip";
var srcFiles = [];
srcFiles.push("D:/srcfiles1.txt");
srcFiles.push("D:/srcfiles2.txt");
srcFiles.push("D:/srcfiles3.txt");
try
{
// create byte buffer
var buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
var fos = new FileOutputStream(zipFile);
var zos = new ZipOutputStream(fos);
for (i=0; i <srcFiles> 0)
{
zos.write(buffer, 0, length);
}
zos.closeEntry();
// close the InputStream
fis.close();
}
// close the ZipOutputStream
zos.close();
log("done");
event.owner.addErrorMessage('zip file created.');
}
catch (e)
{
log("Error creating zip file: " + e);
event.owner.addErrorMessage("Error creating zip file: " + e);
}
Thanks
0 x
-
- Moderator
- Posts: 1342
- Joined: Wed Sep 12, 2007 12:49 pm
How about something like this (also taken from the same example);
Code: Select all
importPackage(com.ebasetech.xi.api);
importPackage(com.ebasetech.xi.services);
importPackage(java.io);
importPackage(java.util.zip);
var zipFile = "C:/temp/ziptest1.zip";
var srcFiles = [ "C:/temp/file1", "C:/temp/file2", "C:/temp/file3" ];
zipit(srcFiles, zipFile);
event.owner.addWarningMessage("zipped");
function zipit (filenames, targetPath)
{
try
{
var buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024)
var fos = new FileOutputStream(zipFile);
var zos = new ZipOutputStream(fos);
for each (var filename in filenames)
{
var srcfile = new File(filename);
var fis = new FileInputStream(srcfile);
zos.putNextEntry(new ZipEntry(srcfile.getName()));
while (true)
{
var length = fis.read(buffer);
if (length <= 0)
break;
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}
}
catch (e)
{
event.owner.addErrorMessage("Error creating zip file: " + e, false);
}
finally
{
if (zos)
zos.close();
}
}
Last edited by Jon on Wed Nov 11, 2015 9:13 am, edited 1 time in total.
0 x
-
- Ebase User
- Posts: 331
- Joined: Mon Mar 10, 2014 8:34 am
-
- Moderator
- Posts: 1342
- Joined: Wed Sep 12, 2007 12:49 pm
-
- Ebase User
- Posts: 331
- Joined: Mon Mar 10, 2014 8:34 am
-
- Moderator
- Posts: 1342
- Joined: Wed Sep 12, 2007 12:49 pm
Who is online
Users browsing this forum: No registered users and 12 guests