如何从ZipInputStream获取每个ZipFile条目的字节/内容而不写入outputstream? [英] How to get bytes/contents of each ZipFile entry from ZipInputStream without writing to outputstream?

查看:222
本文介绍了如何从ZipInputStream获取每个ZipFile条目的字节/内容而不写入outputstream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从压缩文件输入流中获取特定文件的字节。我有压缩文件的输入流数据。从这我得到ZipEntry和写入每个ZipEntry的内容到输出流并返回字节缓冲区。这将返回缓冲区大小输出流,但不是每个特定文件的内容。有没有办法可以将FileOutputStream转换为字节或直接读取每个ZipEntry的字节?

我需要返回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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆