Click HERE to see how Saviynt Intelligence is transforming the industry. |
07/08/2024 11:06 PM
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();
}
}
07/09/2024 12:06 AM - edited 07/09/2024 12:10 AM
Hi @Leofrancis , where are you storing the file after extracting? and what arguments are you passing in jar job?
07/09/2024 12:30 AM - edited 07/09/2024 12:31 AM
@NM
In the same folder where the ZIP file is present and i am not passing any arguments in the JOB
07/09/2024 11:13 PM
07/10/2024 12:05 AM - edited 07/10/2024 12:06 AM
@rushikeshvartak can you help me out in specifying the path for datafile?
07/10/2024 12:09 AM
UNZIP_FILE_PATH=/saviynt_shared/saviynt/Import/Datafiles/
07/10/2024 01:52 AM - edited 07/10/2024 02:29 AM
@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.
07/10/2024 06:23 AM
If its not visible then its not extracted