1

我正在尝试创建一个 servlet,它能够解压缩包含 3 个 csv 文件的文件夹,然后打印出每个 csv 文件的数据。

我一直在尝试使用 ZipInputStream,但它没有为我提供读取/打印每个 csv 内容的能力。

当我在 GAE 上构建这个网络应用程序时,我无法使用 FileOutputStream。

有没有办法使用 ZipInputStream 解压缩和读取单个 csv 而无需在 GAE 上创建 csv?

公共类 AdminBootStrap 扩展 HttpServlet {

public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    resp.setContentType("text/plain");

    PrintWriter out = resp.getWriter();

     try {
          ServletFileUpload upload = new ServletFileUpload();
          resp.setContentType("text/plain");

          FileItemIterator iterator = upload.getItemIterator(req);
          while (iterator.hasNext()) {
            FileItemStream item = iterator.next();
            InputStream in = item.openStream();

            if (item.isFormField()) {
              out.println("Got a form field: " + item.getFieldName());
            } else {
              out.println("Got an uploaded file: " + item.getFieldName() +
                          ", name = " + item.getName());


            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(in));

            ZipEntry entry;

            // Read each entry from the ZipInputStream until no
            // more entry found indicated by a null return value
            // of the getNextEntry() method.
            //
            while ((entry = zis.getNextEntry()) != null) {

                out.println("Unzipping: " + entry.getName());
                //until this point, i'm only available to print each csv name.
                //What I want to do is to print out the data inside each csv file.

            }

            }
          }
        } catch (Exception ex) {
         ex.printStackTrace();
            // throw new ServletException(ex);
        }
      }

}

4

1 回答 1

0

ZipInputStream是一个InputStream,所以你可以正常读取它:

while ((entry = zis.getNextEntry()) {

    byte[] buf = new byte[1024];
    int len;
    while ((len = zis.read(buf)) > 0) {
        // here do something with data in buf
    }

   

于 2012-09-06T18:47:20.903 回答