public String zipToAttachment(String filePath, String archivePath, Container container)
throws StreamTransformationException{
/*
UDF for below functionalities:
To get current Date time
To Read 10 files from a directory of SAP-APP server (only .CSV files and should not starts with "ARC")
To zip 10 files in a single zip file and rename zip as "zipFileName_currentDateTime"
To archive 10 files to other directory
To set single zip file as attachment to HTTP response message
To set attachment header as "application/zip"
*/
//Start of :Get Current Date Time ............................................
//Current DateTime to be used in ZipFileNaming conventions
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd : HHmmssSSS");
Date date = new Date();
String cDate = dateFormat.format(date).toString();
cDate = cDate.replaceAll(" : ", "T");
//End of : Get Current Date Time .............................................
String Input_filePath = filePath;
String Archive_filePath = archivePath;
String Ouput_Zip = "zipFileName_" + cDate + ".zip";//zipFileName_20170610T112255987.zip
String resultStr = "";
Boolean csvFound = false;
String errorStr = "";
//Start of CsvFile Zipping and archinving to other folder ....................
try{
File[] fileDir = new File(Input_filePath).listFiles();
if (fileDir.length > 0) {
FileOutputStream fos = new FileOutputStream(Ouput_Zip);
ZipOutputStream zos = new ZipOutputStream(fos);
int fileCount = 0;
//Read each file in th directory
for (int i=0; i<fileDir.length; i++){
String Input_fileName = fileDir[i].getName(); //Get FileName
String Input_fileName_Path = fileDir[i].getPath(); //Get FileName with PathName
//Each file should be ".CSV" and should not startswith "ARC"
if(Input_fileName.startsWith("ARC")) {
//do nothing
}else{ // if fileName is not starting with "ARC"
if (Input_fileName.lastIndexOf(".CSV") > -1){
csvFound = true; //Here CSV file found
//start: Csv file zipping in to single zip ---------------------------
File file = new File(Input_fileName_Path);
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(file.getName());
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
//End of Csv file zipping in to single zip ---------------------------
//start: File archival -----------------------------------------------
file.renameTo(new File(Archive_filePath + file.getName()));
//end: File archival ------------------------------------------------
//Read only 10 CSV files for one Zip
fileCount = fileCount + 1;
if (fileCount == 10){
break; //break the (for...loop) if fileCount reached 10
}// end of "if case for fileCount"
}// end of "if case for CSV file Check"
} // end of "else if case for ARC file Check"
}//end of "for loop case for FileDirectiry"
if(csvFound == false){
errorStr += " | " + "No .CSV file found in directory: " + Input_filePath ;
}else{
//if atleast one .CSV file found with "ARC" prefix, only then zip file gets created
//and only then it is requried to close ZipOutputStream 'zos' and FileOutputStream 'fos'
zos.close();
fos.close();
//start: set zip as an Attachment ------------------------------------
//Return Zip file as an attachment to Soap Response Message
InputStream zipIpStrm = new FileInputStream(Ouput_Zip);
byte[] buffer = new byte[0xFFFF];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int len; (len = zipIpStrm.read(buffer)) != -1;){
baos.write(buffer, 0, len);
baos.flush();
}
GlobalContainer gblCtnr = container.getGlobalContainer();
OutputAttachments opAtch = gblCtnr.getOutputAttachments();
Attachment Atch = opAtch.create(Ouput_Zip, "application/zip", baos.toByteArray());
opAtch.setAttachment(Atch);
//end: set zip as an Attachment -----------------------------------------
}
}//end of "if (fileDir.length > 0) {"
}catch(Exception e){
errorStr += " | " + e.getMessage();
}
//End of CsvFile Zipping and archinving to other folder ...................
if (errorStr == ""){
resultStr = Ouput_Zip; //set zipFileName to return string
}else{
resultStr = errorStr; //return error
}
return resultStr;
}//end of UDF Fn "zipToAttachment"
/**
* This UDF requires input as FilePath_fileName of pdf file which is residning in SAP-R3's All directory
*
* Functionality of UDF:
* 1. Pull the pdf file from SAP-R3's AL11 directory and archive it
* 2. set this file as an attachment to wbservice reqeust
*/
//String prxIpFPathFName = "/sapFolder/xYFolder/Sample0026.PDF";
String resultStr = "";
String prxIp_FilePath = "";
String prxIp_FileName = "";
String Archive_filePath = "";
try {
if (prxIpFPathFName.indexOf("/") > -1) {
prxIpFPathFName.replaceAll("//", "/");
prxIp_FileName = prxIpFPathFName.substring(prxIpFPathFName.lastIndexOf("/") + 1);
prxIp_FilePath = prxIpFPathFName.substring(0, prxIpFPathFName.lastIndexOf("/") + 1);
Archive_filePath = prxIp_FilePath + "Archive/";
resultStr = prxIp_FileName;
}
if (prxIp_FileName.length() > 0) {
File[] fileDir = new File(prxIp_FilePath).listFiles();
if (fileDir.length > 0) {
for (int i = 0; i < fileDir.length; i++) { // Loop throughout each files in directory
String Input_fileName = fileDir[i].getName();
if (Input_fileName.lastIndexOf(prxIp_FileName) > -1) {
// start: set as Attachment -----------------------------------------------
File file = new File(prxIpFPathFName);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
baos.write(buf, 0, readNum); //no doubt here is 0
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
byte[] bytes = baos.toByteArray();
//Convert byte[] to String
resultStr = Base64.getEncoder().encodeToString(bytes);
// end: set as Attachment ------------------------------------------------
// start: File archival ----------------------------------------------------
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd : HHmmssSSS");
Date date = new Date();
String cDate = dateFormat.format(date).toString();
cDate = cDate.replaceAll(" : ", "T");
String arcFNm = Archive_filePath + cDate + "_" + prxIp_FileName;
if(file.renameTo (new File(arcFNm))){
file.delete();
}
// end: File archival ----------------------------------------------------
}
} // end of for..loop of fileDir check
}
}
} catch (Exception e) {
resultStr += " Exception while setting attachment: " + e.getMessage();
}
return resultStr;You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 27 | |
| 24 | |
| 13 | |
| 13 | |
| 12 | |
| 12 | |
| 11 | |
| 11 | |
| 11 | |
| 10 |