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

2019-01-08 06:32发布

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).

I am trying to zip the attachment files on the fly without writing them to disk first.

What is/are possible way to achieve this outcome?

标签: java zip
5条回答
等我变得足够好
2楼-- · 2019-01-08 06:43

You can use Java's java.util.zip.ZipOutputStream to create a zip file in memory. For example:

public static byte[] zipBytes(String filename, byte[] input) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    ZipEntry entry = new ZipEntry(filename);
    entry.setSize(input.length);
    zos.putNextEntry(entry);
    zos.write(input);
    zos.closeEntry();
    zos.close();
    return baos.toByteArray();
}
查看更多
三岁会撩人
3楼-- · 2019-01-08 06:46
闹够了就滚
4楼-- · 2019-01-08 07:01

I have the same problem but i needed a many files in a zip.

protected byte[] listBytesToZip(Map<String, byte[]> mapReporte, String extension) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    for (Entry<String, byte[]> reporte : mapReporte.entrySet()) {
        ZipEntry entry = new ZipEntry(reporte.getKey() + extension);
        entry.setSize(reporte.getValue().length);
        zos.putNextEntry(entry);
        zos.write(reporte.getValue());
    }
    zos.closeEntry();
    zos.close();
    return baos.toByteArray();
}
查看更多
三岁会撩人
5楼-- · 2019-01-08 07:03

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());
        }
}
查看更多
再贱就再见
6楼-- · 2019-01-08 07:07

Maybe the java.util.zip package might help you

Since you're asking about how to convert from byte array I think (not tested) you can use the ByteArrayInputStream method

int     read(byte[] b, int off, int len)
          Reads up to len bytes of data into an array of bytes from this input stream.

that you will feed to

ZipInputStream  This class implements an input stream filter for reading files in the ZIP file format.
查看更多
登录 后发表回答