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

前端 未结 7 2219
情话喂你
情话喂你 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 03:39

    I have the same problem but i needed a many files in a 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();
    }
    
    0 讨论(0)
  • 2020-11-28 03:39

    You have to use a ZipOutputStream for that.

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

    0 讨论(0)
  • 2020-11-28 03:45
    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();
    
    0 讨论(0)
  • 2020-11-28 03:53

    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.
    
    0 讨论(0)
  • 2020-11-28 03:55
       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;
        }
    

    and

    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;
      }
    
    0 讨论(0)
  • 2020-11-28 03:59

    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();
    }
    
    0 讨论(0)
提交回复
热议问题