how to zip multiple selected files with different directories in android

I’m creating an app that has a list of all music in my phone storage with checkbox on it. I also have a button there where in if click it, it will zip all the selected files on that list. So i searched in the internet how can I zip files in android and this is what i’ve got ZipUnzipFile in android studio. I try to implement this in my app but I’m having an error. I think this is because the selected items have different directories.

this is what i have done so far.

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;
            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() + "/";

            zipFile(zipOutputStream, sourcePath);

        catch (IOException ioe){
            return false;
        }finally {
                try {
                } catch(IOException e) {


        return true;


    private static void zipFile(ZipOutputStream zipOutputStream, String sourcePath) throws  IOException{ files = new;[] fileList = files.listFiles();

        String entryPath="";
        BufferedInputStream input;
        for ( 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);

                int count;
                while ((count =, 0, BUFFER_SIZE)) != -1) {
                    zipOutputStream.write(data, 0, count);

    }} (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/" ;

protected void onCreate(Bundle savedInstanceState) {
    Toolbar toolbar = (Toolbar) findViewById(;

    listView = (ListView) findViewById(;
    getChoice = (Button)findViewById(;

    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()) {
            do {
                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);

            } while (cursor.moveToNext());


        songAdapter = new SongAdapter(Music.this, _songs);

    listView.setAdapter(new SongAdapter(this, _songs));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    getChoice.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String songs = "";
            for(SongInfo hold: songAdapter.getAllData()){
                    songs += "/n"  + hold.getSongUrl();
                    if (, zipPath, "", false)){
                        Toast.makeText(Music.this,"Zip successfully.",Toast.LENGTH_LONG).show();


            Toast.makeText(Music.this,"Names: " + songs,Toast.LENGTH_SHORT).show();

public ArrayList<SongInfo> getAllData(){
        return _songs;

    public void setSelected(int position){
        //Update status of checkbox
        SongInfo items = _songs.get(position);

Can you point out where did I do wrong and if possible can you give some advice or solution so that i can correct it. Thank you in advance! I appreciate it.