59

我的应用程序正在通过 SMTP 服务器接收电子邮件。电子邮件中有一个或多个附件,并且电子邮件附件返回为 byte[](使用 sun javamail api)。

我正在尝试即时压缩附件文件而不先将它们写入磁盘。

实现这一结果的可能方法是什么?

4

8 回答 8

151

您可以使用 Java 的 java.util.zip.ZipOutputStream 在内存中创建一个 zip 文件。例如:

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();
}
于 2008-12-10T22:48:17.693 回答
13

我有同样的问题,但我需要一个 zip 中的许多文件。

 protected byte[] listBytesToZip(Map<String, byte[]> mapReporte) throws IOException {
    String extension = ".pdf";
    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();
}
于 2018-10-30T01:42:47.613 回答
1

您可以从字节数组创建一个 zip 文件并返回到 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());
        }
}
于 2017-03-10T15:03:42.263 回答
0

也许java.util.zip包可能会帮助你

由于您询问如何从字节数组转换,我认为(未测试)您可以使用 ByteArrayInputStream 方法

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

你会喂给

ZipInputStream  This class implements an input stream filter for reading files in the ZIP file format.
于 2008-12-10T22:36:58.420 回答
0

您必须为此使用 ZipOutputStream。

http://java.sun.com/javase/6/docs/api/java/util/zip/ZipOutputStream.html

于 2008-12-10T22:42:24.663 回答
0
ByteArrayInputStream bais = new ByteArrayInputStream(retByte);
                
ZipInputStream zis = new ZipInputStream(bais);
           
zis.getNextEntry();

Scanner sc = new Scanner(zis);
while (sc.hasNextLine()) {
    System.out.println("-->:" +sc.nextLine());
}

zis.closeEntry();
zis.close();
于 2020-07-16T07:35:57.740 回答
0
   byte[] createReport() {
    try {
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
     ZipArchiveOutputStream zipOutputStream = new 
     ZipArchiveOutputStream(byteArrayOutputStream);
     
     zipOutputStream.setMethod(ZipArchiveOutputStream.STORED);
     zipOutputStream.setEncoding(ENCODING);

     String text= "text";
     byte[] textBytes = text.getBytes(StandardCharsets.UTF_8);

     ArchiveEntry zipEntryReportObject = newStoredEntry("file.txt", textBytes);
     zipOutputStream.putArchiveEntry(zipEntryReportObject);
     zipOutputStream.write(textBytes);

     zipOutputStream.closeArchiveEntry();
     zipOutputStream.close();
    
     return byteArrayOutputStream.toByteArray();
     } catch (IOException e) {
       return null;
    }

ArchiveEntry newStoredEntry(String name, byte[] data) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(name);
    zipEntry.setSize(data.length);
    zipEntry.setCompressedSize(zipEntry.getSize());
    CRC32 crc32 = new CRC32();
    crc32.update(data);
    zipEntry.setCrc(crc32.getValue());
    return zipEntry;
  }
于 2020-10-27T11:33:59.697 回答
0
public static void createZip(byte[] data) throws ZipException {
    ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(data));
    ZipParameters parameters = new ZipParameters();
    parameters.setFileNameInZip("bank.zip");
    new ZipFile("F:\\ssd\\bank.zip").addStream(new ByteArrayInputStream(data), parameters);
}
于 2021-01-03T01:56:59.567 回答