Search code examples
javaandroidunzip

getting issue in unzipping folders - files from folder to folder


below is my method for unzipping folder to destination folder and method definition

unzip(filepath, unzipLocation);

here is my method to unzip a zip file this method works file but getting problem when my zip file comes like a.zip which have folder 2 folder (i.e abc1,abc2) and again folder abc1 have folder and it have files please help me

private void unzip(String src, String dest) {
        String _location = "";
        final int BUFFER_SIZE = 4096;
        _location = dest;

        System.out.println("_location :::  " + dest + "");
        System.out.println("src :::  " + src + "");
        BufferedOutputStream bufferedOutputStream = null;
        FileInputStream fileInputStream;
        try {
            fileInputStream = new FileInputStream(src);
            ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
            ZipEntry zipEntry;

            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                String zipEntryName = zipEntry.getName();
                String name = dest.substring(dest.lastIndexOf("/") - 1);
                //  System.out.println("NAME "+name);
//                File FileName = new File(FolderName);
                File FileName = new File(_location.toString());
                if (!FileName.isDirectory()) {
                    try {
                        if (FileName.mkdir()) {
                        } else {
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                String loc = "";
                String fname = "";
                String LOC = "";
                LOC = _location.toString();
//                File file = new File(FolderName+"/" +zipEntryName);
                System.out.println("ZIP " + zipEntryName + "");
                //   zipEntryName=zipEntryName.r
                if (zipEntryName.contains("/")) {

                    String[] file = zipEntryName.split("/");
                    System.out.println("CHECK ::: " + file.length);

                    System.out.println("CHECK ::: " + file[0] + "");
                    String test = zipEntryName.substring(zipEntryName.lastIndexOf("/"), zipEntryName.length());
                    System.out.println("TEST " + test + "");
                    if (test.length() > 1) {
                        LOC = "";
//                        zipEntryName = file[1];
                        zipEntryName = file[file.length - 1];
                        System.out.println("ZIP UPDATED " + zipEntryName + "");
                        //    _location=_location+"/"+file[0]+"/";

                        String l = "";
                        for (int i = 0; i < file.length - 1; i++) {
                            l = l + "/" + file[i];

                        }
//                        System.out.println("TESTTTTTTTT " + l + "");
//                        loc = _location + "/" + file[0];
                        //    loc = _location + "/" + l;
                        LOC = _location + "/" + l;

                        File thumb = new File(LOC);
                        if (!thumb.exists()) {
                            thumb.mkdir();
                        }

                        System.out.println("createddd dir ");

                        System.out.println("createddd dir loc " + loc);
                    } else {
                        System.out.println("create dir ");

                        System.out.println("HERE _location111: : : :  " + _location);
                        System.out.println("HERE zipEntryName1111 : : : :  /" + zipEntryName);

//                        File thumb = new File(_location+"/"+zipEntryName);
                        File thumb = new File(LOC + "/" + zipEntryName);
                        if (!thumb.exists()) {
                            thumb.mkdir();
                        }
                    }
                                  }

                System.out.println("HERE _location: : : :  " + _location);
                System.out.println("HERE zipEntryName : : : :  /" + zipEntryName);
                System.out.println("HERE loc : : : :  /" + loc);
//                File file = new File(_location + "/" + zipEntryName);
                File file = new File(LOC + "/" + zipEntryName);
                if (file.exists()) {

                } else {
                    if (zipEntry.isDirectory()) {
                        file.mkdirs();
                    } else {
                        byte buffer[] = new byte[BUFFER_SIZE];
                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
                        int count;
                        while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
                            bufferedOutputStream.write(buffer, 0, count);
                        }
                        bufferedOutputStream.flush();
                        bufferedOutputStream.close();
                    }
                }
            }
            zipInputStream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Solution

  • public static void unZipIt(String zipFile, String outputFolder) {

        byte[] buffer = new byte[1024];
        //mm    System.out.println("ZIP FILE "+zipFile+"");
        //mm   System.out.println("outputFolder FILE "+outputFolder+"");
        try {
    
            //create output directory is not exists
            File folder = new File(outputFolder);
            if (!folder.exists()) {
                folder.mkdir();
            }
    
            //get the zip file content
            ZipInputStream zis =
                    new ZipInputStream(new FileInputStream(zipFile));
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();
    
            while (ze != null) {
    
                String fileName = ze.getName();
                File newFile = new File(outputFolder + File.separator + fileName);
    
                //mm  System.out.println("file unzip : " + newFile.getAbsoluteFile());
    
                //create all non exists folders
                //else you will hit FileNotFoundException for compressed folder
                new File(newFile.getParent()).mkdirs();
    
                FileOutputStream fos = new FileOutputStream(newFile);
    
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
    
                fos.close();
                ze = zis.getNextEntry();
            }
    
            zis.closeEntry();
            zis.close();
    
            System.out.println("Done");
    
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }