繁体   English   中英

Zip文件未生成

[英]Zip file not getting generated

我在springboot应用程序中使用此方法,该应用程序在custom_users目录中生成3个CSV文件(与Employee,Customer和Building相关),并在其名称后附加时间戳,如下所示。 现在它只生成两个CSV文件(公司和建筑相关),因为我正在测试employee_custom_file zip文件相关转换,如下所示。

有人能告诉我在使用CSVWriter (来自opencsv )将员工相关内容转换为zip文件时我做错了什么? 我期待一个zip文件显示其他2个CSV文件但由于某种原因,只生成2个CSV文件。

基本上下面代码中的这部分不能按我的预期工作:

ZipEntry entry = new ZipEntry(file.getFileName().toString());
zos.putNextEntry(entry);
try {
    CSVWriter writer = new CSVWriter( new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
        writer.writeAll(rsDemo, true);
    }

================================================== ===================

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.getFileName().toString());
                 zos.putNextEntry(entry);
                 try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
                     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

            }   



    }

你有没有从代码示例中省略的closeEntry调用?

请注意,对zip输出流的写入是对输出流的后退。 “写入”条目会写入zip流,而不是写入磁盘,除非这是输出流的设置方式。

来自ZipOutputStream

/**
 * Closes the current ZIP entry and positions the stream for writing
 * the next entry.
 * @exception ZipException if a ZIP format error has occurred
 * @exception IOException if an I/O error has occurred
 */
public void closeEntry() throws IOException {

我搞定了。 我的这行代码FileOutputStream fos = new FileOutputStream("your_files.zip"); 因为我没有在任何地方使用我的dir变量而产生问题。 因此我不得不像这样使用它来使它工作:

OutputStream fos = Files.newOutputStream(dir.resolve("your_files.zip"));
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM