java - how to zip multiple selected files with different directories in android -
i'm creating app has list of music in phone storage checkbox on it. have button there in if click it, zip selected files on list. searched in internet how can zip files in android , i've got zipunzipfile in android studio. try implement in app i'm having error. think because selected items have different directories.
this have done far. filehelper.java
public class filehelper { private static final int buffer_size = 8192 ;//2048; private static string tag= filehelper.class.getname().tostring(); private static string parentpath =""; public static boolean zip( string sourcepath, string destinationpath, string destinationfilename, boolean includeparentfolder) { new file(destinationpath ).mkdirs(); fileoutputstream fileoutputstream ; zipoutputstream zipoutputstream = null; try{ if (!destinationpath.endswith("/")) destinationpath+="/"; string destination = destinationpath + destinationfilename; file file = new file(destination); if (!file.exists()) file.createnewfile(); fileoutputstream = new fileoutputstream(file); zipoutputstream = new zipoutputstream(new bufferedoutputstream(fileoutputstream)); if (includeparentfolder) parentpath=new file(sourcepath).getparent() + "/"; else parentpath=sourcepath; zipfile(zipoutputstream, sourcepath); } catch (ioexception ioe){ log.d(tag,ioe.getmessage()); return false; }finally { if(zipoutputstream!=null) try { zipoutputstream.close(); } catch(ioexception e) { } } return true; } private static void zipfile(zipoutputstream zipoutputstream, string sourcepath) throws ioexception{ java.io.file files = new java.io.file(sourcepath); java.io.file[] filelist = files.listfiles(); string entrypath=""; bufferedinputstream input; (java.io.file file : filelist) { if (file.isdirectory()) { zipfile(zipoutputstream, file.getpath()); } else { byte data[] = new byte[buffer_size]; fileinputstream fileinputstream = new fileinputstream(file.getpath()); input = new bufferedinputstream(fileinputstream, buffer_size); entrypath=file.getabsolutepath().replace( parentpath,""); zipentry entry = new zipentry(entrypath); zipoutputstream.putnextentry(entry); int count; while ((count = input.read(data, 0, buffer_size)) != -1) { zipoutputstream.write(data, 0, count); } input.close(); } } }}
music.java (mainactivity)
public class music extends appcompatactivity{ listview listview; songadapter songadapter; button getchoice; arraylist<songinfo> _songs = new arraylist<songinfo>(); private string sdpath = environment.getexternalstoragedirectory().getabsolutepath(); private string zippath = sdpath + "/try/zipunzip/zip/" ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_music); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); listview = (listview) findviewbyid(r.id.lv); getchoice = (button)findviewbyid(r.id.btnmusic); getsupportactionbar().setdisplayhomeasupenabled(true); uri uri = mediastore.audio.media.external_content_uri; string selection = mediastore.audio.media.is_music + "!=0"; cursor cursor = getcontentresolver().query(uri, null, selection, null, null); if (cursor != null) { if (cursor.movetofirst()) { { string name = cursor.getstring(cursor.getcolumnindex(mediastore.audio.media.display_name)); string artist = cursor.getstring(cursor.getcolumnindex(mediastore.audio.media.artist)); string url = cursor.getstring(cursor.getcolumnindex(mediastore.audio.media.data)); songinfo s = new songinfo(name, artist, url); _songs.add(s); } while (cursor.movetonext()); } cursor.close(); songadapter = new songadapter(music.this, _songs); } listview.setadapter(new songadapter(this, _songs)); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { songadapter.setselected(position); } }); getchoice.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string songs = ""; for(songinfo hold: songadapter.getalldata()){ if(hold.isselected()){ songs += "\n" + hold.getsongurl(); if (filehelper.zip(songs, zippath, "leoj.zip", false)){ toast.maketext(music.this,"zip successfully.",toast.length_long).show(); } } } toast.maketext(music.this,"names: " + songs,toast.length_short).show(); } }); }//oncreate public arraylist<songinfo> getalldata(){ return _songs; } public void setselected(int position){ //update status of checkbox songinfo items = _songs.get(position); items.setselected(!items.isselected()); notifydatasetchanged(); }
can point out did wrong , if possible can give advice or solution can correct it. thank in advance! appreciate it.
Comments
Post a Comment