在 Java 中:如何从 byte[] 数组压缩文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/357851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Java: How to zip file from byte[] array?
提问by netic
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).
我的应用程序正在通过 SMTP 服务器接收电子邮件。电子邮件中有一个或多个附件,电子邮件附件以 byte[] 形式返回(使用 sun javamail api)。
I am trying to zip the attachment files on the fly without writing them to disk first.
我试图在不先将它们写入磁盘的情况下动态压缩附件文件。
What is/are possible way to achieve this outcome?
实现这一结果的可能方法是什么?
采纳答案by Dave L.
You can use Java's java.util.zip.ZipOutputStream to create a zip file in memory. For example:
您可以使用 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();
}
回答by Eric
Maybe the java.util.zippackage might help you
也许java.util.zip包可以帮助你
Since you're asking about how to convert from byte array I think (not tested) you can use the ByteArrayInputStream method
由于您问的是如何从字节数组转换,我认为(未测试)您可以使用 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.
that you will feed to
你会喂给
ZipInputStream This class implements an input stream filter for reading files in the ZIP file format.
回答by OscarRyz
You have to use a ZipOutputStream for that.
为此,您必须使用 ZipOutputStream。
http://java.sun.com/javase/6/docs/api/java/util/zip/ZipOutputStream.html
http://java.sun.com/javase/6/docs/api/java/util/zip/ZipOutputStream.html
回答by Maciej
You can create a zip file from byte array and return to ui streamedContent
您可以从字节数组创建一个 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());
}
}
回答by Jesús Sánchez
I have the same problem but i needed a many files in a zip.
我有同样的问题,但我需要一个 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();
}