java - zip/compress a folder on android -


i used following code zip files , works great zip subfolders , not have root of tree show in zip file.

public boolean zipfileatpath(string sourcepath, string tolocation) { // arraylist<string> contentlist = new arraylist<string>(); file sourcefile = new file(sourcepath); try {     bufferedinputstream origin = null;     fileoutputstream dest = new fileoutputstream(tolocation);     zipoutputstream out = new zipoutputstream(new bufferedoutputstream(             dest));     if (sourcefile.isdirectory()) {         zipsubfolder(out, sourcefile, sourcefile.getparent().length());     } else {         byte data[] = new byte[buffer];         fileinputstream fi = new fileinputstream(sourcepath);         origin = new bufferedinputstream(fi, buffer);         zipentry entry = new zipentry(getlastpathcomponent(sourcepath));         out.putnextentry(entry);         int count;         while ((count = origin.read(data, 0, buffer)) != -1) {             out.write(data, 0, count);         }     }     out.close(); } catch (exception e) {     e.printstacktrace();     return false; } return true; }  private void zipsubfolder(zipoutputstream out, file folder,     int basepathlength) throws ioexception { file[] filelist = folder.listfiles(); bufferedinputstream origin = null; (file file : filelist) {     if (file.isdirectory()) {         zipsubfolder(out, file, basepathlength);     } else {         byte data[] = new byte[buffer];         string unmodifiedfilepath = file.getpath();         string relativepath = unmodifiedfilepath                 .substring(basepathlength);         log.i("zip subfolder", "relative path : " + relativepath);         fileinputstream fi = new fileinputstream(unmodifiedfilepath);         origin = new bufferedinputstream(fi, buffer);         zipentry entry = new zipentry(relativepath);         out.putnextentry(entry);         int count;         while ((count = origin.read(data, 0, buffer)) != -1) {             out.write(data, 0, count);         }         origin.close();     } } }  public string getlastpathcomponent(string filepath) { string[] segments = filepath.split("/"); string lastpathcomponent = segments[segments.length - 1]; return lastpathcomponent; } 

right if enter environment.getexternalstoragedirectory().tostring()+"/x123" sourcepath x123 included in tree.

-zipfile      -x123           -subfolder1           -subfolder2           -... 

i remove x123

 -zipfile      -subfolder1      -subfolder2      -... 

thank you

played around , ended using following code:

static public void zipfolder(string srcfolder, string destzipfile)         throws exception {     zipoutputstream zip = null;     fileoutputstream filewriter = null;     filewriter = new fileoutputstream(destzipfile);     zip = new zipoutputstream(filewriter);     addfoldertozip("", srcfolder, zip);     zip.flush();     zip.close(); }    static private void addfiletozip(string path, string srcfile,         zipoutputstream zip) throws exception {     file folder = new file(srcfile);     if (folder.isdirectory()) {         addfoldertozip(path, srcfile, zip);     } else {         byte[] buf = new byte[1024];         int len;         fileinputstream in = new fileinputstream(srcfile);         zip.putnextentry(new zipentry(path.replace("x123/", "") + "/" + folder.getname()));         //zip.putnextentry(new zipentry(path + "/" + folder.getname()));         while ((len = in.read(buf)) > 0) {             zip.write(buf, 0, len);         }     } }  static private void addfoldertozip(string path, string srcfolder,         zipoutputstream zip) throws exception {     file folder = new file(srcfolder);     (string filename : folder.list()) {         if (path.equals("")) {             addfiletozip(folder.getname(), srcfolder + "/" + filename, zip);         } else {             addfiletozip(path + "/" + folder.getname(), srcfolder + "/"                 + filename, zip);         }     } } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -