java 无需在java中提取即可读取Zip文件内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36548755/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:35:03  来源:igfitidea点击:

Read Zip file content without extracting in java

javazipfiletruezip

提问by TNN

I have byte[] zipFileAsByteArray

我有字节 [] zipFileAsByteArray

This zip file has rootDir --|
                            | --- Folder1 - first.txt
                            | --- Folder2 - second.txt  
                            | --- PictureFolder - image.png  

What I need is to get two txt files and read them, without saving any files on disk. Just do it in memory.

我需要的是获取两个 txt 文件并读取它们,而不在磁盘上保存任何文件。只在记忆中做。

I tried something like this:

我试过这样的事情:

ByteArrayInputStream bis = new ByteArrayInputStream(processZip);
ZipInputStream zis = new ZipInputStream(bis);

Also I will need to have separate method go get picture. Something like this:

另外我需要有单独的方法去获取图片。像这样的东西:

public byte[]image getImage(byte[] zipContent);

Can someone help me with idea or good example how to do that ?

有人可以帮助我提出想法或很好的例子如何做到这一点吗?

采纳答案by dambros

Here is an example:

下面是一个例子:

public static void main(String[] args) throws IOException {
    ZipFile zip = new ZipFile("C:\Users\mofh\Desktop\test.zip");


    for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
        ZipEntry entry = (ZipEntry) e.nextElement();
        if (!entry.isDirectory()) {
            if (FilenameUtils.getExtension(entry.getName()).equals("png")) {
                byte[] image = getImage(zip.getInputStream(entry));
                //do your thing
            } else if (FilenameUtils.getExtension(entry.getName()).equals("txt")) {
                StringBuilder out = getTxtFiles(zip.getInputStream(entry));
                //do your thing
            }
        }
    }


}

private  static StringBuilder getTxtFiles(InputStream in)  {
    StringBuilder out = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String line;
    try {
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
    } catch (IOException e) {
        // do something, probably not a text file
        e.printStackTrace();
    }
    return out;
}

private static byte[] getImage(InputStream in)  {
    try {
        BufferedImage image = ImageIO.read(in); //just checking if the InputStream belongs in fact to an image
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        return baos.toByteArray();
    } catch (IOException e) {
        // do something, it is not a image
        e.printStackTrace();
    }
    return null;
}

Keep in mind though I am checking a string to diferentiate the possible types and this is error prone. Nothing stops me from sending another type of file with an expected extension.

请记住,尽管我正在检查一个字符串以区分可能的类型,但这很容易出错。没有什么能阻止我发送具有预期扩展名的另一种类型的文件。

回答by dryairship

You can do something like:

您可以执行以下操作:

public static void main(String args[]) throws Exception
{
    //bis, zis as you have
    try{
        ZipEntry file;
        while((file = zis.getNextEntry())!=null) // get next file and continue only if file is not null
        {
            byte b[] = new byte[(int)file.getSize()]; // create array to read.
            zis.read(b); // read bytes in b
            if(file.getName().endsWith(".txt")){
                // read files. You have data in `b`
            }else if(file.getName().endsWith(".png")){
                // process image
            }
        }
    }
    finally{
        zis.close();
    }
}