Four Stars

How do I encode a csv file to base64?

Hi I'm relatively new to talend and I want to encode a file of .csv format to base64. I am able to convert the string inside the .csv file but want to encode the entire file. Something like this in the link https://www.freeformatter.com/base64-encoder.html

1 ACCEPTED SOLUTION

Accepted Solutions
Twelve Stars TRF
Twelve Stars

Re: How do I encode a csv file to base64?

Hi,

Place this piece of Java code in a tJavaRow (in this example, the filename is in the current row):

// Get file from Document_ID + Name
String filename = input_row.Document_ID+"_"+input_row.Name;

//File file = new File((String)globalMap.get("ftpLocalDirectory")+"/"+(String)globalMap.get("ftpFilename"));
File file = new File((String)globalMap.get("ftpLocalDirectory")+"/"+filename);
FileInputStream documentInFile = null;

try {           
    // Read file
    documentInFile = new FileInputStream(file);
    byte documentData[] = new byte[(int) file.length()];
    documentInFile.read(documentData);

    // Convert bytes array to Base64 string to fill Body field
    output_row.Body = new String(Base64.encodeBase64(documentData));
    System.out.println("+++ File converted "+filename);
} catch (FileNotFoundException e) {
	output_row.conversionCode = "FILE_NOT_FOUND";
	output_row.conversionMessage = "File not found "+filename;
	System.out.println("*** File not found "+filename+"\n"+e);
} catch (IOException ioe) {
	output_row.conversionCode = "CONVERSION_ERROR";
	output_row.conversionMessage = "Error converting file "+filename;
	System.out.println("*** Error converting file "+filename+"\n"+ioe);
} finally {
	try {
		// Close and delete file after conversion
		if (documentInFile != null) {
			documentInFile.close();
			//file.delete();
		}
	} catch (IOException e) {
		output_row.conversionCode = "DELETE_ERROR";
		output_row.conversionMessage = "Error deleting file "+filename;
		System.out.println("*** Error deleting file "+filename+"\n"+e);
		e.printStackTrace();
	}
}

This is a travial solution, but it works.

Hope this helps.

 

Note: the conversion result goes to a field of the output row (Body). You just have to write the filed content to the desired file.


TRF
4 REPLIES
Twelve Stars TRF
Twelve Stars

Re: How do I encode a csv file to base64?

Hi,

Place this piece of Java code in a tJavaRow (in this example, the filename is in the current row):

// Get file from Document_ID + Name
String filename = input_row.Document_ID+"_"+input_row.Name;

//File file = new File((String)globalMap.get("ftpLocalDirectory")+"/"+(String)globalMap.get("ftpFilename"));
File file = new File((String)globalMap.get("ftpLocalDirectory")+"/"+filename);
FileInputStream documentInFile = null;

try {           
    // Read file
    documentInFile = new FileInputStream(file);
    byte documentData[] = new byte[(int) file.length()];
    documentInFile.read(documentData);

    // Convert bytes array to Base64 string to fill Body field
    output_row.Body = new String(Base64.encodeBase64(documentData));
    System.out.println("+++ File converted "+filename);
} catch (FileNotFoundException e) {
	output_row.conversionCode = "FILE_NOT_FOUND";
	output_row.conversionMessage = "File not found "+filename;
	System.out.println("*** File not found "+filename+"\n"+e);
} catch (IOException ioe) {
	output_row.conversionCode = "CONVERSION_ERROR";
	output_row.conversionMessage = "Error converting file "+filename;
	System.out.println("*** Error converting file "+filename+"\n"+ioe);
} finally {
	try {
		// Close and delete file after conversion
		if (documentInFile != null) {
			documentInFile.close();
			//file.delete();
		}
	} catch (IOException e) {
		output_row.conversionCode = "DELETE_ERROR";
		output_row.conversionMessage = "Error deleting file "+filename;
		System.out.println("*** Error deleting file "+filename+"\n"+e);
		e.printStackTrace();
	}
}

This is a travial solution, but it works.

Hope this helps.

 

Note: the conversion result goes to a field of the output row (Body). You just have to write the filed content to the desired file.


TRF
Four Stars

Re: How do I encode a csv file to base64?

How can I get Document_ID and Name? Am using tFileInputRaw to connect to the tJavaRow. I can only see content from tFileInputRaw. Thank you very much for the repsonse.

Twelve Stars TRF
Twelve Stars

Re: How do I encode a csv file to base64?

You need to adapt the example to your use case.
Forget Document_ID.
In your case you may retrieve the filename from tFileInputRaw using "(String)globalMap.get("tFileInputRaw_1.FILENAME_PATH")".
Now you just need to replace the path or the filename.
For example, replace ".csv" by "_base64.csv" to have a new file arr the same place than the original one.

TRF
Four Stars

Re: How do I encode a csv file to base64?

Thank you sir,

 

It worked, I was trying to figure out how to add import statements in tJavaRow component. Now it's all good and working.