How to zip the files include sub folders in android -
i want zip more 1 directory. mean have 1 inner directory , 1 parent directory. want zip parent directory.
i 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 files 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; (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]; (int = 0; < _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 use following codes facing null pointer exception in zip() method. when zip() method try read child path , again try read parent path showing null pointer exception.
please let me idea solve this.
index++;
. have 2 of them. place second 1 first 1 directly after filespath[index] = file.getpath();
.
but string array filespath = new string[(int) parentdir.length()]; not have right size in way. there no room files of sub directory.
you better use <string> arraylist
can add as want without having know @ forehand how there be.
Comments
Post a Comment