I have this method in my springboot application which is generating 3 CSV files (related to Employee, Customer and Building) inside custom_users directory with timestamp appended to its name as shown below. The following code works fine for me.
public void sendMessage(String msg) throws DaoException {
DataSource ds = null;
Connection conn = null;
PreparedStatement pstmt = null;
PreparedStatement pstmtEmployee = null;
PreparedStatement pstmtCompany = null;
PreparedStatement pstmBuilding = null;
ResultSet rs = null;
ResultSet rsDemo = null;
ResultSet rsCompany = null;
ResultSet rsBuildings = null;
String[] parts = msg.split("#");
String requestID = parts[0].trim();
String userName = parts[1].trim();
String applicationName = parts[2].trim();
logger.info("Request ID "+requestID);
logger.info("User Name "+userName);
logger.info("Application Name "+applicationName);
try {
ds = jdbcTemplate.getDataSource();
conn = ds.getConnection();
/*===========================================================================*/
/* Code to generate a employee CSV file */
/*===========================================================================*/
pstmtEmployee = conn.prepareStatement(getPatientEmployeeSQL);
pstmtEmployee.setString(1, requestID);
rsDemo = pstmtEmployee.executeQuery();
ResultSetMetaData rsmd = rsDemo.getMetaData();
Path dir = Paths.get("/srv/custom_users", userName);
Files.createDirectories(dir);
Path file = dir.resolve("employee_custom_file" + unixTimestamp + ".csv");
try (CSVWriter writer = new CSVWriter(Files.newBufferedWriter(file))) {
writer.writeAll(rsDemo, true);
}
logger.info("Employee File Generated");
/*===========================================================================*/
/* Code to generate a company CSV file */
/*===========================================================================*/
pstmtCompany = conn.prepareStatement(getCompanySQL);
pstmtCompany.setString(1, requestID);
rsCompany = pstmtCompany.executeQuery();
ResultSetMetaData rsmdFacts = rsCompany.getMetaData();
Path filecompany = dir.resolve("company_custom_file_" + unixTimestamp + ".csv");
try (CSVWriter writer = new CSVWriter(Files.newBufferedWriter(filecompany))) {
writer.writeAll(rsCompany, true);
}
logger.info("Company CSV File Generated");
/*===========================================================================*/
/* Code to generate a building CSV file */
/*===========================================================================*/
pstmBuilding = conn.prepareStatement(getBuildingSQL);
pstmBuilding.setString(1, requestID);
rsBuildings = pstmBuilding.executeQuery();
ResultSetMetaData rsmdBuildings = rsBuildings.getMetaData();
Path fileBuildings = dir.resolve("building_custom_file_" + unixTimestamp + ".csv");
try (CSVWriter writer = new CSVWriter(Files.newBufferedWriter(fileBuildings))) {
writer.writeAll(rsBuildings, true);
}
logger.info("Buildings CSV File Generated");
}
catch(Throwable th) {
throw new DaoException(th.getMessage(), th);
}
finally {
//resource closing statements
}
}
My questions:
I am trying to modify the CSVWriter
's constructor in sucn a way that the CSV data should go directly into the ZIP archive as ZipEntry objects.
So I made the changes in the above code (visible after this line ResultSetMetaData rsmd = rsDemo.getMetaData();
in the code below). Right now I am just trying to put one file into ZIP archive to test but I noticed following errors:
Error on Line #173 says: The constructor ZipEntry(Path) is undefined.
Error on Line #175 says: Multiple markers at this line (one out of 2 errors is The constructor CSVWriter(ZipOutputStream) is undefined) .
How should I handle ZipEntry's and CSVWriter's constructor part ?
public void sendMessage(String msg) throws DaoException {
DataSource ds = null;
Connection conn = null;
PreparedStatement pstmt = null;
PreparedStatement pstmtEmployee = null;
PreparedStatement pstmtCompany = null;
PreparedStatement pstmBuilding = null;
ResultSet rs = null;
ResultSet rsDemo = null;
ResultSet rsCompany = null;
ResultSet rsBuildings = null;
String[] parts = msg.split("#");
String requestID = parts[0].trim();
String userName = parts[1].trim();
String applicationName = parts[2].trim();
logger.info("Request ID "+requestID);
logger.info("User Name "+userName);
logger.info("Application Name "+applicationName);
try {
ds = jdbcTemplate.getDataSource();
conn = ds.getConnection();
/*===========================================================================*/
/* Code to generate a employee CSV file */
/*===========================================================================*/
pstmtEmployee = conn.prepareStatement(getPatientEmployeeSQL);
pstmtEmployee.setString(1, requestID);
rsDemo = pstmtEmployee.executeQuery();
ResultSetMetaData rsmd = rsDemo.getMetaData();
FileOutputStream fos = new FileOutputStream("your_files.zip");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);
Path dir = Paths.get("/srv/custom_users", userName);
Files.createDirectories(dir);
Path file = dir.resolve("employee_custom_file" + unixTimestamp + ".csv");
/*try (CSVWriter writer = new CSVWriter(Files.newBufferedWriter(file))) {
writer.writeAll(rsDemo, true);
}*/
ZipEntry entry = new ZipEntry(file);
zos.putNextEntry(entry);
try (CSVWriter writer = new CSVWriter(zos)) {
writer.writeAll(rsDemo, true);
}
logger.info("Employee File Generated");
/*===========================================================================*/
/* Code to generate a company CSV file */
/*===========================================================================*/
pstmtCompany = conn.prepareStatement(getCompanySQL);
pstmtCompany.setString(1, requestID);
rsCompany = pstmtCompany.executeQuery();
ResultSetMetaData rsmdFacts = rsCompany.getMetaData();
Path filecompany = dir.resolve("company_custom_file_" + unixTimestamp + ".csv");
try (CSVWriter writer = new CSVWriter(Files.newBufferedWriter(filecompany))) {
writer.writeAll(rsCompany, true);
}
logger.info("Company CSV File Generated");
/*===========================================================================*/
/* Code to generate a building CSV file */
/*===========================================================================*/
pstmBuilding = conn.prepareStatement(getBuildingSQL);
pstmBuilding.setString(1, requestID);
rsBuildings = pstmBuilding.executeQuery();
ResultSetMetaData rsmdBuildings = rsBuildings.getMetaData();
Path fileBuildings = dir.resolve("building_custom_file_" + unixTimestamp + ".csv");
try (CSVWriter writer = new CSVWriter(Files.newBufferedWriter(fileBuildings))) {
writer.writeAll(rsBuildings, true);
}
logger.info("Buildings CSV File Generated");
}
catch(Throwable th) {
throw new DaoException(th.getMessage(), th);
}
finally {
//resource Closing statements
}
}