I am trying to zip 3 CSV files from 3 different tables in single zip file your_files_12354627.zip
.
So, I've the following method which works fine for zip file generation with 1 csv file in it. I'm trying to do this for multiple files and hence I've modified the below code (as shown below the working code):
WORKING CODE BELOW:
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 resultSetFirst = null;
ResultSet resultSetSecond = null;
ResultSet resultSetThird = null;
String[] parts = msg.split("#");
String requestID = parts[0].trim();
String userName = parts[1].trim();
String applicationName = parts[2].trim();
try {
ds = jdbcTemplate.getDataSource();
conn = ds.getConnection();
pstmtEmployee = conn.prepareStatement(getPatientEmployeeSQL);
pstmtEmployee.setString(1, requestID);
resultSetFirst = pstmtEmployee.executeQuery();
pstmtCompany = conn.prepareStatement(getCompanySQL);
pstmtCompany.setString(1, requestID);
resultSetSecond = pstmtCompany.executeQuery();
pstmtBuilding = conn.prepareStatement(getBuildingSQL);
pstmtBuilding.setString(1, requestID);
resultSetThird = pstmtBuilding.executeQuery();
Path dir = Paths.get("/srv/custom_users", userName);
Files.createDirectories(dir);
OutputStream fos = Files.newOutputStream(dir.resolve("your_files_"+ unixTimestamp +".zip"));
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);
Path employeeFile = dir.resolve("employee_custom_file_" + unixTimestamp + ".csv");
Path companyFile = dir.resolve("company_custom_file_" + unixTimestamp + ".csv");
Path buildingFile = dir.resolve("building_custom_file_" + unixTimestamp + ".csv");
ZipEntry firstEntry = new ZipEntry(employeeFile.getFileName().toString());
zos.putNextEntry(firstEntry);
try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
writer.writeAll(resultSetFirst, true);
writer.flush();
zos.closeEntry();
}
/*ZipEntry secondEntry = new ZipEntry(companyFile.getFileName().toString());
zos.putNextEntry(secondEntry);
try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
writer.writeAll(resultSetSecond, true);
writer.flush();
zos.closeEntry();
}
ZipEntry thirdEntry = new ZipEntry(buildingFile.getFileName().toString());
zos.putNextEntry(thirdEntry);
try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
writer.writeAll(resultSetThird, true);
writer.flush();
zos.closeEntry();
}*/
zos.close();
}
catch(Throwable th) {
throw new DaoException(th.getMessage(), th);
}
finally {
//resource Closing statements
}
}
MODIFIED CODE BELOW:
How should I loop through 3 different resultsetsso that I can make use of writer.writeAll(resultSetFirst, true);
, writer.writeAll(resultSetSecond, true);
and writer.writeAll(resultSetThird, true);
respectively? I am trying to loop through the file names as shown in the code below but not sure how should I handle different result sets inside the for loop as shown below. I've commented at the location where I'm trying to figure out this loop thing.
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 resultSetFirst = null;
ResultSet resultSetSecond = null;
ResultSet resultSetThird = null;
String[] parts = msg.split("#");
String requestID = parts[0].trim();
String userName = parts[1].trim();
String applicationName = parts[2].trim();
try {
ds = jdbcTemplate.getDataSource();
conn = ds.getConnection();
pstmtEmployee = conn.prepareStatement(getPatientEmployeeSQL);
pstmtEmployee.setString(1, requestID);
resultSetFirst = pstmtEmployee.executeQuery();
pstmtCompany = conn.prepareStatement(getCompanySQL);
pstmtCompany.setString(1, requestID);
resultSetSecond = pstmtCompany.executeQuery();
pstmtBuilding = conn.prepareStatement(getBuildingSQL);
pstmtBuilding.setString(1, requestID);
resultSetThird = pstmtBuilding.executeQuery();
Path dir = Paths.get("/srv/custom_users", userName);
Files.createDirectories(dir);
OutputStream fos = Files.newOutputStream(dir.resolve("your_files_"+ unixTimestamp +".zip"));
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);
Path employeeFile = dir.resolve("employee_custom_file_" + unixTimestamp + ".csv");
Path companyFile = dir.resolve("company_custom_file_" + unixTimestamp + ".csv");
Path buildingFile = dir.resolve("building_custom_file_" + unixTimestamp + ".csv");
List<String> csvFileNames = new ArrayList<String>();
csvFileNames.add(employeeFile.getFileName().toString());
csvFileNames.add(companyFile.getFileName().toString());
csvFileNames.add(buildingFile.getFileName().toString());
for (String entries : csvFileNames) {
ZipEntry entry = new ZipEntry(entries);
zos.putNextEntry(entry);
CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8));
//How should I loop through different resultsets here so that I can make use of writer.writeAll(resultSetFirst, true);
// writer.writeAll(resultSetSecond, true); and writer.writeAll(resultSetThird, true); respectively?
System.out.println("Printing entries");
System.out.println(entries);
}
/*ZipEntry firstEntry = new ZipEntry(employeeFile.getFileName().toString());
zos.putNextEntry(firstEntry);
try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
writer.writeAll(resultSetFirst, true);
writer.flush();
zos.closeEntry();
}*/
/*ZipEntry secondEntry = new ZipEntry(companyFile.getFileName().toString());
zos.putNextEntry(secondEntry);
try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
writer.writeAll(resultSetSecond, true);
writer.flush();
zos.closeEntry();
}
ZipEntry thirdEntry = new ZipEntry(buildingFile.getFileName().toString());
zos.putNextEntry(thirdEntry);
try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
writer.writeAll(resultSetThird, true);
writer.flush();
zos.closeEntry();
}*/
zos.close();
}
catch(Throwable th) {
throw new DaoException(th.getMessage(), th);
}
finally {
//resource Closing statements
}
}