android unzip folder from zip file and read content from that folder

Viewed 2158

in one of my app i need to extract a zip file that have folder inside and that folder contains images it mean abc.zip=>adb(folder)=>abc.png i want to extract image file

i used below method

 private boolean extractFolder(File destination, File zipFile) throws ZipException, IOException
    {
        int BUFFER = 8192;
        File file = zipFile;
        //This can throw ZipException if file is not valid zip archive
        ZipFile zip = new ZipFile(file);
//        String newPath = destination.getAbsolutePath() + File.separator + FilenameUtils.removeExtension(zipFile.getName());
        String newPath = destination.getAbsolutePath() + File.separator + zipFile.getName();
        //Create destination directory
        new File(newPath).mkdir();
        Enumeration zipFileEntries = zip.entries();

        //Iterate overall zip file entries
        while (zipFileEntries.hasMoreElements())
        {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            File destFile = new File(newPath, currentEntry);
            File destinationParent = destFile.getParentFile();
            //If entry is directory create sub directory on file system
            destinationParent.mkdirs();

            if (!entry.isDirectory())
            {
                //Copy over data into destination file
                BufferedInputStream is = new BufferedInputStream(zip
                        .getInputStream(entry));
                int currentByte;
                byte data[] = new byte[BUFFER];
                //orthodox way of copying file data using streams
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
                while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                is.close();
            }
        }
        return true;//some error codes etc.
    }

but getting zipfolder/foldername/ (Is a directory)

1 Answers
Related