Saviynt unveils its cutting-edge Intelligence Suite products to revolutionize Identity Security!
Click HERE to see how Saviynt Intelligence is transforming the industry.
Saviynt Copilot Icon

HOW TO EXTRACT THE CSV AND SEND IT TO SFTP FROM SAVIYNT

Leofrancis
New Contributor
New Contributor

I've set up the connector to transfer a file to the SFTP server. The connector can transfer the ZIP file, but not the CSV file. I wrote some Java code to extract the CSV from the ZIP, converted it into a JAR, and then imported it into Saviynt. I ran the JAR job, but I'm still unable to transfer the CSV file to the SFTP server.

I am pasting the Java code below to extract the CSV file.

I need assistance to overcome the problem.

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.util.logging.*;

public class ZipExtractor {
private static final Logger LOGGER = Logger.getLogger(ZipExtractor.class.getName());

private Map<String, String> jobInputs;

public ZipExtractor(Map<String, String> jobInputs) {
this.jobInputs = jobInputs;
}

public void run() {
LOGGER.info("--Starting ZipExtractor process--");

if (jobInputs == null || jobInputs.isEmpty()) {
LOGGER.info("--Entered arguments through job config are not correct or empty");
return;
}

String sourcePath = jobInputs.get("savReportsSource");
String targetPath = jobInputs.get("savReportsTarget");

LOGGER.info("--zipSourcFolderPath--" + sourcePath);
LOGGER.info("--csvDestFolderPath--" + targetPath);

File sourceDir = new File(sourcePath);
File targetDir = new File(targetPath);

if (!sourceDir.exists() || !targetDir.exists()) {
LOGGER.warning("--zipSourcFolderPath: " + sourcePath + " doesn't exist or csvDestFolderPath: " + targetPath + " doesn't exists--");
return;
}

try {
File latestZipFile = getLatestZipFile(sourceDir);

if (latestZipFile == null) {
LOGGER.warning("--No zip files found in the source directory--");
return;
}

String zipFilePath = latestZipFile.getAbsolutePath();
String csvFileName = getCsvFileNameFromZip(latestZipFile.getName());

DateFormat dateFormat = new SimpleDateFormat("MMddyyyy");
String currentDateStr = dateFormat.format(new Date());
String targetCsvFileName = "SFTPFile" + currentDateStr + ".csv";

extractCsvFromZip(zipFilePath, csvFileName, targetPath + File.separator + targetCsvFileName);
} catch (Exception e) {
LOGGER.severe("--Error occurred while extracting CSV from ZIP--: " + e.getMessage());
e.printStackTrace();
}
}

private static File getLatestZipFile(File directory) {
File[] files = directory.listFiles((dir, name) -> name.endsWith(".zip"));
if (files == null || files.length == 0) {
return null;
}

File latestFile = files[0];
for (File file : files) {
if (file.lastModified() > latestFile.lastModified()) {
latestFile = file;
}
}

LOGGER.info("Found latest zip file: " + latestFile.getName());
return latestFile;
}

private static String getCsvFileNameFromZip(String zipFileName) {
return zipFileName.replace(".zip", ".csv");
}

private static void extractCsvFromZip(String zipFilePath, String csvFileName, String targetFilePath) throws Exception {
try (ZipFile zipFile = new ZipFile(zipFilePath)) {
ZipEntry entry = zipFile.getEntry(csvFileName);
if (entry == null) {
LOGGER.warning("CSV file not found in the ZIP archive: " + csvFileName);
return;
}

try (InputStream inputStream = zipFile.getInputStream(entry);
OutputStream outputStream = new FileOutputStream(new File(targetFilePath))) {

byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}

LOGGER.info("CSV file: " + targetFilePath + " extracted and placed in: " + targetFilePath);
}
}
}
}

 

import java.util.HashMap;
import java.util.Map;

public class ZipExtractorTest {
public static void main(String[] args) {
Map<String, String> jobInputs = new HashMap<>();
jobInputs.put("savReportsSource", "path/to/the/source/folder");
jobInputs.put("savReportsTarget", "path/to/the/destination/folder");

ZipExtractor extractor = new ZipExtractor(jobInputs);
extractor.run();
}
}

7 REPLIES 7

NM
Honored Contributor III
Honored Contributor III

Hi @Leofrancis , where are you storing the file after extracting? and what arguments are you passing in jar job?


If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'

Leofrancis
New Contributor
New Contributor

@NM 
In the same folder where the ZIP file is present and i am not passing any arguments in the JOB

  1. Store csv file in datafiles and then move from datafiles to SFTP

Regards,
Rushikesh Vartak
If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'.

@rushikeshvartak can you help me out in specifying the path for datafile?

UNZIP_FILE_PATH=/saviynt_shared/saviynt/Import/Datafiles/


Regards,
Rushikesh Vartak
If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'.

@rushikeshvartak 
after the successful run of the JAR job i can't see the CSV file in the Datafiles, so uploaded a CSV file manually into the datafile and tried to export to SFTP but it throws an error saying 

Error in putFilesInTarget ::  JSONObject["SourceDirectory"] not found.

If its not visible then its not extracted 


Regards,
Rushikesh Vartak
If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'.