The CreatorCon Call for Content is officially open! Get started here.

we are created folder in MID Server , in that folder create Zip files or PDF files, after sending files we want delete that files in MID Server?

ram2497
Tera Contributor

we are created folder in MID Server , in that folder  create Zip files or PDF files, after sending files we want delete that files in MID Server?

Plese find below i am shaaring code:

Note:Below code comment  Zip_file.delete()-- we are enabled that time it's showing please check once and help me sir.

CODE MID Server Script Include:

var FileZipUtil = Class.create();

FileZipUtil.prototype = {

File: Packages.java.io.File,
FileInputStream: Packages.java.io.FileInputStream,
FileOutputStream: Packages.java.io.FileOutputStream,
ZipEntry: Packages.java.util.zip.ZipEntry,
ZipInputStream: Packages.java.util.zip.ZipInputStream,
JavaString: Packages.java.lang.String,
Base64: Packages.org.apache.commons.codec.binary.Base64,
FileUtils: Packages.org.apache.commons.io.FileUtils,

initialize: function() {

//gs.log("log 1 Initialize");
this.verbose = probe.getParameter("verbose");
this.file_name = probe.getParameter("file_name");
this.encoded_data = probe.getParameter("encoded_data");
this.request_type = probe.getParameter("request_type");
this.requested_file_path = probe.getParameter("requested_file_path");
this.result = {
messages: [],
files: [],
bd_transaction_sys_id: probe.getParameter("bd_transaction_sys_id"),
request_type: this.request_type
};

},

execute: function() {
try {
this.debug("here is the debug message " + this.request_type);

if (this.request_type == "initiate_file_transfer") {
//log
var destDir = new this.File("." + this.File.separator + "BD_processor");
//gs.log("output" +destDir);
if (!destDir.exists())
destDir.mkdir();

var data = this.Base64.decodeBase64(this.encoded_data);

var stream = new this.FileOutputStream(destDir.getCanonicalPath() + this.File.separator + this.file_name);
stream.write(data);
stream.close();

var fileZip = destDir.getCanonicalPath() + this.File.separator + this.file_name;
var empty_str = new Packages.java.lang.String("test");
var buffer = empty_str.getBytes();
var zis = new this.ZipInputStream(new this.FileInputStream(fileZip));
var zipEntry = zis.getNextEntry();
this.debug('before Zip entry');
while (zipEntry != null) {

this.debug('zipEntry is not null'+zipEntry.getName());

if (zipEntry.getName().indexOf(".xlsx") > -1) {
var newFile = this.newFile(destDir, zipEntry);
var fos = new this.FileOutputStream(newFile);
var len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();

var zip_file = new this.File(destDir, zipEntry.getName());
var fileContent = this.FileUtils.readFileToByteArray(zip_file);
var encodedString = this.Base64.encodeBase64String(fileContent);
this.result.files.push({
file_name: zipEntry.getName(),
file_path: zip_file.getCanonicalPath()
});
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();

// var downloaded_zip = new this.File(destDir.getCanonicalPath() + this.File.separator + this.file_name);
// downloaded_zip.delete();

} else if (this.request_type == "get_file_content") {
var zip_file = new this.File(this.requested_file_path);
var fileContent = this.FileUtils.readFileToByteArray(zip_file);
var encodedString1 = this.Base64.encodeBase64String(fileContent);
this.result.files.push({
file_name: this.file_name,
file_content: encodedString1
});

// zip_file.delete();

}

 

} catch (e) {
this.debug(e.toString());
}
return JSON.stringify(this.result);
},

newFile: function(destinationDir, zipEntry) {
var destFile = new this.File(destinationDir, zipEntry.getName());

return destFile;
},


debug: function(m) {
if (this.verbose == "true") {
ms.log(m);
this.result.messages.push(m);
}
},

type: 'FileZipUtil'
};

 

Thanks

Ram

6 REPLIES 6

Hi Ram,

the zip_file object should store the path

the below code worked previously for me

find_real_file.png

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ram,

Let me know if that answered your question.

If so, please mark my response as correct & 👍 helpful so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader