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

时间:2019-06-07 17:15:09

标签: java file opencv fileoutputstream zipoutputstream

我在springboot应用程序中有此方法,该应用程序在custom_users目录内生成3个CSV文件(与Employee,Customer和Building有关),并在其名称后附加了时间戳,如下所示。 以下代码对我来说很好用。

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

        }   



}

我的问题:

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

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

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

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

我的日食截图: enter image description here

我应该如何处理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

            }   



    }

1 个答案:

答案 0 :(得分:0)

ZipEntry构造函数需要一个String或另一个ZipEntry,请选中here

假设您使用的是opencsv,则CSVWriter需要一个实现Writer接口的类,请选中here

因此,您应该使用(未经测试):

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

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