简体   繁体   English

使用Java和Google App Engine在sp上载的zip文件中解压缩特定文件

[英]unzip a specific file in the zip file uploaded by sp using java and google app engine

I have trying to find out the solution for to unzip a particular file using java and Google app engine. 我试图找出使用Java和Google App Engine解压缩特定文件的解决方案。 I have tried using ZipInputStream but cant able to access the zip file that is uploaded in jsp. 我尝试使用ZipInputStream,但无法访问在jsp中上载的zip文件。 can any one help me to come out of this? 有人可以帮助我摆脱困境吗?

ServletFileUpload upload = new ServletFileUpload();
             resp.setContentType("text/plain");
             FileItemIterator iterator = upload.getItemIterator(req);
              while (iterator.hasNext()) {
                  FileItemStream fileItemStream = iterator.next();
                  InputStream InputStream = fileItemStream.openStream();
                  if (!fileItemStream.isFormField()) {
                      ZipInputStream zis = new ZipInputStream(new BufferedInputStream(InputStream));
                      ZipEntry entry;
                      while ((entry = zis.getNextEntry()) != null) {

                          //code to access required file in the zip file
                      }

                  } 
              }

My guess would be that ZipInputStream requires stream that is seekable. 我的猜测是ZipInputStream需要可搜索的流。 The streams returned by servlets (and networking in general) aren't seekable. servlet(通常是网络)返回的流是不可搜索的。

Try reading the whole stream, then wrapping it in ByteArrayInputStream : 尝试读取整个流,然后将其包装在ByteArrayInputStream

byte[] bytes = readBytes(fileItemStream.openStream());
InputStream bufferedStream = new ByteArrayInputStream(bytes);

public static byte[] readBytes(InputStream is) throws IOException {
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();

  int len;
  byte[] data = new byte[100000];
  while ((len = is.read(data, 0, data.length)) != -1) {
    buffer.write(data, 0, len);
  }

  buffer.flush();
  return buffer.toByteArray();
}

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

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