我的应用程序通过SMTP服务器接收电子邮件。电子邮件和电子邮件附件中有一个或多个附件以byte []的形式返回(使用sun javamail api)。
我正在尝试将附件文件暂时压缩而不先将它们写入磁盘。
实现这一结果的可行方法是什么?
答案 0 :(得分:103)
您可以使用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();
}
答案 1 :(得分:1)
也许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.
答案 2 :(得分:1)
我有同样的问题,但是我需要一个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();
}
答案 3 :(得分:0)
你必须使用ZipOutputStream。
http://java.sun.com/javase/6/docs/api/java/util/zip/ZipOutputStream.html
答案 4 :(得分:0)
您可以从字节数组创建一个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());
}
}
答案 5 :(得分: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();
答案 6 :(得分: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;
}
答案 7 :(得分: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);
}