In Java: How to zip file from byte[] array?

前端 未结 7 2220
情话喂你
情话喂你 2020-11-28 03:36

My application is receiving email through SMTP server. There are one or more attachments in the email and email attachment return as byte[] (using sun javamail api).

相关标签:
7条回答
  • 2020-11-28 04:02

    You can create a zip file from byte array and return to ui streamedContent

    public StreamedContent getXMLFile() {
            try {
                byte[] blobFromDB= null;
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ZipOutputStream zos = new ZipOutputStream(baos);
                String fileName= "fileName";
                ZipEntry entry = new ZipEntry(fileName+".xml");
                entry.setSize(byteArray.length);
                zos.putNextEntry(entry);
                zos.write(byteArray);
                zos.closeEntry();
                zos.close();
                InputStream is = new ByteArrayInputStream(baos.toByteArray());
                StreamedContent zipedFile= new DefaultStreamedContent(is,   "application/zip", fileName+".zip", Charsets.UTF_8.name());
                return fileDownload;
            } catch (IOException e) {
                LOG.error("IOException e:{} ",e.getMessage());
            } catch (Exception ex) {
                LOG.error("Exception ex:{} ",ex.getMessage());
            }
    }
    
    0 讨论(0)
提交回复
热议问题