How to zip the files include sub folders in android

I want to zip more then one directory. That mean I have 1 inner directory and 1 parent directory. Now I want to zip the parent directory.

I am using following codes:

My file Path :

/data/data/com/app/1430159400000/32640/Images/capture_Image_20150427_115541.png"
/data/data/com/app/1430159400000/32640/Images/capture_Image_20150427_115542.png"
/data/data/com/app/1430159400000/32640/Images/ChildImages/capture_Image_20150427_115543.png"
/data/data/com/app/1430159400000/32640/Images/ChildImages/capture_Image_20150427_115544.png"
/data/data/com/app/1430159400000/32640/Images/capture_Image_20150427_115545.png"
/data/data/com/app/1430159400000/32640/Images/capture_Image_20150427_115546.png"

To get the files from directory:-

public void getListFilesForCreatingZip(File parentDir) {
        String[] filesPath = null;
        File[] files = parentDir.listFiles();
        filesPath = new String[(int) parentDir.length()];
        int index = 0;
        int index1=0;
        for (File file : files) {
            if (file.isDirectory()) {
                if(file.getName().equals("ChildImages"))
                {
                    File[] files1 = file.listFiles();
                    for(File ss:files1)
                    {
                        filesPath[index]=ss.getPath();
                        index++;
                    }
                }
            } else {
                filesPath[index] = file.getPath();
            }
            index++;
        }
        zip(filesPath, "PathName";
    }

To Zip files:-

public void zip(String[] _files, String zipFileName) {
        try {
            BufferedInputStream origin = null;
            /*File root = Application.getInstance().getDir("bol", Context.MODE_PRIVATE);
            File customDir = new File(root + File.separator + File.separator + PreferenceManagerForBol.getInstance().getBolSelectedDate() + File.separator + PreferenceManagerForBol.getInstance().getBolOrderNumber());
            if (customDir.exists() == false) {
                customDir.mkdirs();
            }  */   
            File file = new File(BolDetailsHandler.getInstance().createBasePath(),  zipFileName + ".zip");
            FileOutputStream dest = new FileOutputStream(file);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
            byte data[] = new byte[BUFFER];

            for (int i = 0; i < _files.length; i++) {
                if(!_files[i].contains(".zip"))
                {
                Logger.v("Compress", "Adding: " + _files[i]);
                FileInputStream fi = new FileInputStream(_files[i]);
                origin = new BufferedInputStream(fi, BUFFER);

                ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;

                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
                }
            }

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

When I use this following codes I am facing null pointer exception in zip() method. When zip() method try to read the child path and again try to read parent path it showing null pointer exception.

Please let me any idea to solve this.

Upvotes: 0

Views: 216

Answers (1)

greenapps
greenapps

Reputation: 11214

index++;. You have two of them. Place the second one like the first one directly after filesPath[index] = file.getPath();.

But your String array filesPath = new String[(int) parentDir.length()]; will not have the right size in this way. There is no room for the files of the sub directory.

You could much better use a <String> ArrayList as then you can add as much as you want without having to know at forehand how much there will be.

Upvotes: 1

Related Questions