0

is it possible to split a file before download in android like we can do in linux through a cURL command?

curl --range 200000000-399999999 -o ubuntu-iso.part2 http://mirror.pnl.gov/releases/15.04/ubuntu-15.04-desktop-amd64.iso
curl --range 400000000-599999999 -o ubuntu-iso.part3 http://mirror.pnl.gov/releases/15.04/ubuntu-15.04-desktop-amd64.iso
curl --range 600000000-799999999 -o ubuntu-iso.part4 http://mirror.pnl.gov/releases/15.04/ubuntu-15.04-desktop-amd64.iso
  • 1
    I think that you can fild an appropriate answer here: http://stackoverflow.com/questions/4004675/reading-the-first-part-of-a-file-using-http - but i don't think that you really need it. – Jehy May 11 '16 at 09:27
  • @Jehy all i need is to download file in parts and i will concate the parts later – Rachit Bhargava May 11 '16 at 09:44
  • 1
    Another good sample here: http://stackoverflow.com/questions/16788437/how-to-download-a-part-of-a-file-from-url-in-android – Jehy May 11 '16 at 09:49

1 Answers1

0

Once you have the filesize, divide it into roughly equal sized chunks and spawn download thread for each chunk. Once all are done, write the file chunks in the correct order.

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
            File file=new File(DESTINATION_PATH);
            if(file.exists()){
                 downloaded = (int) file.length();
                 connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
            }
        }else{
            connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
        }
        connection.setDoInput(true);
        connection.setDoOutput(true);
        progressBar.setMax(connection.getContentLength());
         in = new BufferedInputStream(connection.getInputStream());
         fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
         bout = new BufferedOutputStream(fos, 1024);
        byte[] data = new byte[1024];
        int x = 0;
        while ((x = in.read(data, 0, 1024)) >= 0) {
            bout.write(data, 0, x);
             downloaded += x;
             progressBar.setProgress(downloaded);
        }

more anwers: https://stackoverflow.com/a/6323043/4014891

Community
  • 1
  • 1
  • 1
    Can you please tell what the line if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING) means and from where bothe values are coming from @Zilvins7 – Rachit Bhargava May 12 '16 at 06:49