简体   繁体   English

Zip文件未生成

[英]Zip file not getting generated

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. 我在springboot应用程序中使用此方法,该应用程序在custom_users目录中生成3个CSV文件(与Employee,Customer和Building相关),并在其名称后附加时间戳,如下所示。 Right now it's only generating two CSV files (company and building related) because I am testing zip file related conversion for employee_custom_file as shown below. 现在它只生成两个CSV文件(公司和建筑相关),因为我正在测试employee_custom_file zip文件相关转换,如下所示。

Could anyone tell me what am I doing wrong while converting employee related content to zip file using CSVWriter (from opencsv )? 有人能告诉我在使用CSVWriter (来自opencsv )将员工相关内容转换为zip文件时我做错了什么? I was expecting one zip file to show up with 2 other CSV files but for some reason, only 2 CSV files are generating. 我期待一个zip文件显示其他2个CSV文件但由于某种原因,只生成2个CSV文件。

Basically this part in the code below isn't working as I expected: 基本上下面代码中的这部分不能按我的预期工作:

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

            }   



    }

Do you have a call to closeEntry which is omitted from the code example? 你有没有从代码示例中省略的closeEntry调用?

Note that writes to the zip output stream are to whatever backs the output stream. 请注意,对zip输出流的写入是对输出流的后退。 'Writing' the entry writes to the zip stream, and not to disk, unless that is how the output stream was set up. “写入”条目会写入zip流,而不是写入磁盘,除非这是输出流的设置方式。

From ZipOutputStream : 来自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 {

I got it working. 我搞定了。 My this line of code FileOutputStream fos = new FileOutputStream("your_files.zip"); 我的这行代码FileOutputStream fos = new FileOutputStream("your_files.zip"); was creating problem as I wasn't using my dir variable anywhere. 因为我没有在任何地方使用我的dir变量而产生问题。 Hence I had to use it like this to make it work: 因此我不得不像这样使用它来使它工作:

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