Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中:如何从byte[]数组压缩文件?_Java_Zip - Fatal编程技术网

在Java中:如何从byte[]数组压缩文件?

在Java中:如何从byte[]数组压缩文件?,java,zip,Java,Zip,我的应用程序正在通过SMTP服务器接收电子邮件。电子邮件中有一个或多个附件,电子邮件附件返回字节[](使用sun javamail api) 我正在尝试动态压缩附件文件,而不先将其写入磁盘 实现这一结果的可能方法是什么?也许该软件包可以帮助您 由于您询问如何从字节数组转换,我认为(未测试)可以使用ByteArrayInputStream方法 int read(byte[] b, int off, int len) Reads up to len bytes of da

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

我正在尝试动态压缩附件文件,而不先将其写入磁盘

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

也许该软件包可以帮助您

由于您询问如何从字节数组转换,我认为(未测试)可以使用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.

为此,您必须使用ZipoutStream


您可以使用Java的Java.util.zip.ZipoutStream在内存中创建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文件并返回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());
        }
}

我也有同样的问题,但我需要在一个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();
}
protectedbyte[]listBytesToZip(映射映射报告)引发IOException{
字符串扩展名=“.pdf”;
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
ZipOutputStream zos=新ZipOutputStream(BAS);
对于(条目报告e:mapReporte.entrySet()){
ZipEntry条目=新的ZipEntry(report.getKey()+扩展名);
entry.setSize(reporte.getValue().length);
zos.putNextEntry(条目);
write(reporte.getValue());
}
zos.closeEntry();
zos.close();
返回baos.toByteArray();
}


先生,你救了我的命@Dave-ZipoutStream可以作为jax-rs发送吗output@Deve我有同样的箱子,但我必须压缩5个文件。有什么建议吗?@Deva您可以将ZipEntry部分重复为您喜欢的manny文件。@AshishBurnwal ZipOutputStream执行实际的压缩和编码。它从数组中获取字节,并生成一个压缩的字节流,这些字节流被馈送到ByteArrayOutputStream。然后,我们访问并返回来自底层流的压缩编码字节以返回它们
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);
}