Zip compression of file uploads prior to emailing them

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

robdavidsalmon
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

#1

Postby robdavidsalmon » Mon Nov 09, 2015 3:27 pm

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

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Mon Nov 09, 2015 3:48 pm

There isn't anything built into Ebase. I would advise using java.util.zip - you can include all the code in a Javascript script so don't need to create any Java classes. If you get stuck we could probably cobble together an example.
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#3

Postby Steve James » Tue Nov 10, 2015 12:55 pm

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).

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 &#40;i=0; i <srcFiles> 0&#41;
      &#123;
      	zos.write&#40;buffer, 0, length&#41;;
      &#125;
      zos.closeEntry&#40;&#41;;
      // close the InputStream
      fis.close&#40;&#41;;
    &#125;
    // close the ZipOutputStream
    zos.close&#40;&#41;;
    log&#40;"done"&#41;;
    event.owner.addErrorMessage&#40;'zip file created.'&#41;;
&#125;
catch &#40;e&#41;
&#123;
	log&#40;"Error creating zip file&#58; " + e&#41;;
	event.owner.addErrorMessage&#40;"Error creating zip file&#58; " + e&#41;;
&#125; 
The principle was lifted from http://examples.javacodegeeks.com/core- ... putstream/


Thanks
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#4

Postby Jon » Tue Nov 10, 2015 2:46 pm

How about something like this (also taken from the same example);

Code: Select all

importPackage&#40;com.ebasetech.xi.api&#41;;
importPackage&#40;com.ebasetech.xi.services&#41;;
importPackage&#40;java.io&#41;;
importPackage&#40;java.util.zip&#41;; 

var zipFile = "C&#58;/temp/ziptest1.zip";
var srcFiles = &#91; "C&#58;/temp/file1", "C&#58;/temp/file2", "C&#58;/temp/file3" &#93;;
zipit&#40;srcFiles, zipFile&#41;;
event.owner.addWarningMessage&#40;"zipped"&#41;;

function zipit &#40;filenames, targetPath&#41;
&#123;
		try 
		&#123;
			var buffer = java.lang.reflect.Array.newInstance&#40;java.lang.Byte.TYPE, 1024&#41; 
			var fos = new FileOutputStream&#40;zipFile&#41;;
			var zos = new ZipOutputStream&#40;fos&#41;;
			for each &#40;var filename in filenames&#41; 
			&#123;
				var srcfile = new File&#40;filename&#41;;
				var fis = new FileInputStream&#40;srcfile&#41;;
				zos.putNextEntry&#40;new ZipEntry&#40;srcfile.getName&#40;&#41;&#41;&#41;;
				while &#40;true&#41;
				&#123;
					var length = fis.read&#40;buffer&#41;; 
					if &#40;length <= 0&#41;
						break;
					zos.write&#40;buffer, 0, length&#41;;
				&#125;
				zos.closeEntry&#40;&#41;;
				fis.close&#40;&#41;;
			&#125;
		&#125;
		catch &#40;e&#41; 
		&#123;
			 event.owner.addErrorMessage&#40;"Error creating zip file&#58; " + e, false&#41;; 
		&#125;
		finally
		&#123;
			if &#40;zos&#41;
				zos.close&#40;&#41;;
		&#125;
&#125;
Last edited by Jon on Wed Nov 11, 2015 9:13 am, edited 1 time in total.
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#5

Postby Steve James » Tue Nov 10, 2015 3:00 pm

Thanks Jon, I really must get into the habit of using a "finally" :D

That said the addErrorMessage would prevent the finally from running.
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#6

Postby Jon » Tue Nov 10, 2015 4:07 pm

It shouldn't. The finally clause should always run - in any circumstance.
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#7

Postby Steve James » Tue Nov 10, 2015 9:34 pm

Hi Jon, it does stop the finally from running.

Thanks
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#8

Postby Jon » Wed Nov 11, 2015 9:12 am

So it does, that's something to watch out for. I've changed the code in the example above.
0 x


Who is online

Users browsing this forum: No registered users and 12 guests