Read Zip file content without extracting in java

早过忘川 提交于 2021-02-08 13:10:53

问题


I have byte[] 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.

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 ?


回答1:


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.




回答2:


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();
    }
}


来源:https://stackoverflow.com/questions/36548755/read-zip-file-content-without-extracting-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!