如何从ZipInputStream获取每个ZipFile条目的字节/内容而不写入outputstream? [英] How to get bytes/contents of each ZipFile entry from ZipInputStream without writing to outputstream?
问题描述
我需要返回ZipEntry内容作为输出。
感谢您的帮助。
$ b $我不想写一个文件,只是得到每个zip条目的内容。 b
public final class ArchiveUtils {
private static String TEMP_DIR =/ tmp / unzip /;
public static byte [] unZipFromByteStream(byte [] data,String fileName){
ZipEntry entry = null;
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(data));
文件tempDirectory = ensureTempDirectoryExists(TEMP_DIR); ((entry = zis.getNextEntry())!= null){
if(!entry.isDirectory()){
System。
尝试{
。 out.println( - + entry.getName());
File file = new File(tempDirectory +/+ entry.getName());
$ b $ if(!new File(file.getParent())。exists())
new File(file.getParent())。mkdirs();
OutputStream out = new FileOutputStream(entry.getName());
byte [] byteBuff = new byte [1024];
int bytesRead = 0; (byteRead = zis.read(byteBuff))!= -1)
(
out.write(byteBuff,0,bytesRead);
}
out.close();
if(entry.getName()。equals(fileName)){
return byteBuff;
$ b} catch(Exception ex){
ex.printStackTrace();
}
返回null;
private static File ensureTempDirectoryExists(String outputPath){
File outputDirectory = new File(outputPath);
if(!outputDirectory.exists()){
outputDirectory.mkdir();
}
return outputDirectory;
$ / code>
}
使用 java .io.ByteArrayOutputStream 像这样 -
ByteArrayOutputStream out = null; //在你的循环之外(对于范围)。
//你有一个FileOutputStream。
out = new ByteArrayOutputStream(); //不使用entry.getName()。
然后您可以
return(out == null)? null:out.toByteArray();
I am trying to get the bytes of a particular file from the zipped file input stream. I have the input stream data of the Zipped file. From this I am getting the ZipEntry and writing the contents of each ZipEntry to a output stream and returning the byte buffer. This will return the buffer size output stream, but not the contents of each particular file. Is there a way I can convert the FileOutputStream to bytes or directly read the bytes of each ZipEntry?
I need to return the ZipEntry contents as output. I dont want to write to a file but just get the contents of each zip entry.
Thank you for your help.
public final class ArchiveUtils {
private static String TEMP_DIR = "/tmp/unzip/";
public static byte[] unZipFromByteStream(byte[] data, String fileName) {
ZipEntry entry = null;
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(data));
File tempDirectory = ensureTempDirectoryExists(TEMP_DIR);
try {
while ((entry = zis.getNextEntry()) != null) {
if (!entry.isDirectory()) {
System.out.println("-" + entry.getName());
File file = new File(tempDirectory + "/"+ entry.getName());
if (!new File(file.getParent()).exists())
new File(file.getParent()).mkdirs();
OutputStream out = new FileOutputStream(entry.getName());
byte[] byteBuff = new byte[1024];
int bytesRead = 0;
while ((bytesRead = zis.read(byteBuff)) != -1)
{
out.write(byteBuff, 0, bytesRead);
}
out.close();
if(entry.getName().equals(fileName)){
return byteBuff;
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
private static File ensureTempDirectoryExists(String outputPath) {
File outputDirectory = new File(outputPath);
if (!outputDirectory.exists()) {
outputDirectory.mkdir();
}
return outputDirectory;
}
}
Use a java.io.ByteArrayOutputStream like so -
ByteArrayOutputStream out = null; // outside of your loop (for scope).
// where you have a FileOutputStream.
out = new ByteArrayOutputStream(); // doesn't use entry.getName().
and then you can
return (out == null) ? null : out.toByteArray();
这篇关于如何从ZipInputStream获取每个ZipFile条目的字节/内容而不写入outputstream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!