簡體   English   中英

在 Java 中:如何從 byte[] 數組壓縮文件?

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

我的應用程序正在通過 SMTP 服務器接收電子郵件。 電子郵件中有一個或多個附件,電子郵件附件以 byte[] 形式返回(使用 sun javamail api)。

我試圖在不先將它們寫入磁盤的情況下動態壓縮附件文件。

實現這一結果的可能方法是什么?

您可以使用 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();
}

我有同樣的問題,但我需要一個 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();
}

您可以從字節數組創建一個 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());
        }
}

也許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.
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();
   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;
  }
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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM