简体   繁体   English

如何压缩文件包括Android中的子文件夹

[英]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. 这意味着我有1个内部目录和1个父目录。 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. 当我使用以下代码时,我在zip()方法中遇到空指针异常。 When zip() method try to read the child path and again try to read parent path it showing null pointer exception. 当zip()方法尝试读取子路径并再次尝试读取父路径时,它显示空指针异常。

Please let me any idea to solve this. 请让我有任何办法解决此问题。

index++; . You have two of them. 你有两个。 Place the second one like the first one directly after filesPath[index] = file.getPath(); 将第二个文件像第一个文件一样直接放置在filesPath[index] = file.getPath(); .

But your String array filesPath = new String[(int) parentDir.length()]; 但是您的String数组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. 您最好使用<String> ArrayList ,这样您就可以根据需要添加任意数量,而无需事先知道会有多少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM