将 csv 数据作为 zipentry 对象移动到 zip 存档 - 构造函数 ZipEntry(Path) 未定义

move csv data to zip archive as zipentry objects- constructor ZipEntry(Path) is undefined

我的 springboot 应用程序中有这个方法,它在 custom_users 目录中生成 3 个 CSV 文件(与员工、客户和建筑物相关),其名称附加了时间戳,如下所示。 以下代码对我来说工作正常。

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

        }   



}

我的问题:

我正在尝试以 CSV 数据作为 ZipEntry 对象直接进入 ZIP 存档的方式修改 CSVWriter 的构造函数。

所以我对上面的代码进行了更改(在下面的代码中这一行 ResultSetMetaData rsmd = rsDemo.getMetaData(); 之后可见)。现在我正试图将一个文件放入 ZIP 存档进行测试,但我注意到以下错误:

第 173 行错误说: 构造函数 ZipEntry(Path) 未定义。

第 175 行错误说: 此行有多个标记(2 个错误中有一个是构造函数 CSVWriter(ZipOutputStream)未定义)。

我的日食截图:

我应该如何处理 ZipEntry 和 CSVWriter 的构造函数部分?

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

            }   



    }

ZipEntry 构造函数需要一个 String 或另一个 ZipEntry,检查 here

CSVWriter,假设您使用的是 opencsv,需要一个实现 Writer 接口的 class,检查 here

所以你应该使用(未测试):

ZipEntry entry = new ZipEntry(file.toString());

try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos))) {
    ...
}