Reading from a ZipInputStream into a ByteArrayOutputStream

后端 未结 10 1524
旧时难觅i
旧时难觅i 2021-02-05 08:14

I am trying to read a single file from a java.util.zip.ZipInputStream, and copy it into a java.io.ByteArrayOutputStream (so that I can then create a

10条回答
  •  清歌不尽
    2021-02-05 08:39

    Please try code bellow

    private static byte[] getZipArchiveContent(File zipName) throws WorkflowServiceBusinessException {
    
      BufferedInputStream buffer = null;
      FileInputStream fileStream = null;
      ByteArrayOutputStream byteOut = null;
      byte data[] = new byte[BUFFER];
    
      try {
       try {
        fileStream = new FileInputStream(zipName);
        buffer = new BufferedInputStream(fileStream);
        byteOut = new ByteArrayOutputStream();
    
        int count;
        while((count = buffer.read(data, 0, BUFFER)) != -1) {
         byteOut.write(data, 0, count);
        }
       } catch(Exception e) {
        throw new WorkflowServiceBusinessException(e.getMessage(), e);
       } finally {
        if(null != fileStream) {
         fileStream.close();
        }
        if(null != buffer) {
         buffer.close();
        }
        if(null != byteOut) {
         byteOut.close();
        }
       }
      } catch(Exception e) {
       throw new WorkflowServiceBusinessException(e.getMessage(), e);
      }
      return byteOut.toByteArray();
    
     }
    

提交回复
热议问题