The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to convert .txt file to .zip file using script

prasanna7869
Mega Expert

Hi,

 

I have an issue while converting a .txt file to .zip file through script. Actual, requirement is "whenever .txt file is attached to a ticket, we need to convert that .txt file to .zip file."

For that , i have created a on BR on sys_attachment table with the following code:

 

 

var zipName = fileName +'.zip';

try{

var FileName = current.file_name;

var zFile = new Packages.java.io.File(zipName);

 

var outStream = new Packages.java.io.FileOutputStream(zFile);

         

var out = new Packages.java.util.zip.ZipOutputStream(outStream);

  var sa = new Packages.com.glide.ui.SysAttachment();

//var sa = new GlideSysAttachment();

  var binData = sa.getBytes(current);

addBytesToZip(out, zipName, FileName, binData);

gr.content_type = "application/octet-stream";

gr.file_name = zipName;

gr.update();

  }

    catch(Exception)

    {

          gs.log("**Exception:"+Exception);

    }

function addBytesToZip (out, dir, Filename, stream)

{

out.putNextEntry(new Packages.java.util.zip.ZipEntry(Filename));

out.write(stream, 0, stream.length);

out.closeEntry();

}

 

During the execution, i am getting an exception as "TypeError: [JavaPackage java.io.FileOutputStream] is not a function." and is for the line

var outStream = new Packages.java.io.FileOutputStream(zFile);


Please help me to resolve the error and to convert the file to .zip file.


Thanks in advance,

Prasanna




1 REPLY 1

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Prasanna,



I hope this should be helpful


» Download Attachments as a ZIP File



Thanks,


Pradeep Sharma