Android Download Zip (That can change name on the server)
<p>I'm trying to make android download a zip file from my web server using a php page to download it.</p>

<p>If I download using a static link to the zip file, it works great, but i'm trying to download using a php file with this code:</p>

<pre><code>function file_list($d,$x){
   foreach(array_diff(scandir($d,1),array('.','..')) as $f)if(is_file($d.'/'.$f)&amp;&amp;(($x)?ereg($x.'$',$f):1))$l[]=$f;
   return $l;
}

$arr = file_list("../download/",".zip");

$filename = $arr[0];
$filepath = "../download/".$arr[0];

if(!file_exists($filepath)){
   die('Error: File not found.');
} else {
   // Set headers
   header("Cache-Control: public");
   header("Content-Description: File Transfer");
   header("Content-Disposition: attachment; filename=$filename");
   header("Content-Type: application/zip");
   header("Content-Transfer-Encoding: binary");

   readfile($filepath);
}
</code></pre>

<p>If I access the php file in my browser, it downloads the zip just great!</p>

<p>Now, in the Android side, it downloads like it's supposed but the name of the zip is getZip.php (php file name) and the file size is 22.</p>

<p>This is the code that does the downloading stuff on the Android</p>

<pre><code>        int count;

        try {
            downloadCoords();
            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lengthOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Length of file: " + lengthOfFile);

            File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
            if(!f.isDirectory()){
                f.mkdirs();
            }

            Uri u = Uri.parse(url.toString());
            File uf = new File(""+u);
            zipname = uf.getName();
            Log.d("ANDRO_ASYNC", "Zipname: " + zipname);

            File zipSDCARD = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+zipname);
            if(!zipSDCARD.isFile()){
                Log.d("zipSDCARD.isFile()","false");

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream("/sdcard/" + zipname);

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress(""+(int)((total*100)/lengthOfFile));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            }
            successDownload = true;
        } catch (Exception e) {
            successDownload = false;
            Log.e("Error","DownloadZip",e);
        }
</code></pre>

<p>What needs to be done, is getting the zipname correctly and the ziplength too.</p>

<p>Thanks in advance.</p>
