разархивируйте и прочитайте каждый файл в Google App Engine (Java)

Я пытаюсь создать сервлет, который может распаковать папку, содержащую 3 файла csv, а затем распечатать данные каждого файла csv.

Я пытался использовать ZipInputStream, но он не дает мне возможности читать/печатать содержимое каждого csv.

Поскольку я создаю это веб-приложение на GAE, я не могу использовать FileOutputStream.

Существуют ли способы использовать ZipInputStream для распаковки и чтения отдельных CSV-файлов без необходимости создания CSV-файла в GAE?

открытый класс 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);
        }
      }

}


comment
Я понимаю, что могу распечатать данные, используя: System.out.write(buf, 0, len); Но можно ли хранить эти данные прямо в строковой переменной?   -  person chuntato    schedule 08.09.2012


Ответы (1)


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
    }

   

person Peter Knego    schedule 06.09.2012