0

I am trying to write the class to download the file from server and store it on sd card.But I am getting one Exception as java.io.FileNotFoundException: /mnt/sdcard/files.zip (No such file or directory).

My Main class is

public class DownloadZipActivity extends Activity {
    /** Called when the activity is first created. */
    int count;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {

        URL url = new URL(
                "url to download");

       /*   URLConnection conexion = url.openConnection();
        conexion.connect();
*/
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setDoInput(true);
            conn.setConnectTimeout(1000); // timeout 10 secs
            conn.connect();

        int lenghtOfFile = conn.getContentLength();
        Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream("/mnt/sdcard/external_sd/files.zip");

        byte data[] = new byte[1024];

        long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
        //      publishProgress(""+(int)((total*100)/lenghtOfFile));

                    output.write(data, 0, count);
                }


            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        String zipFile = Environment.getExternalStorageDirectory() + "/files.zip"; 
        String unzipLocation = Environment.getExternalStorageDirectory() + "/unzipped/"; 
        Decompress d = new Decompress(zipFile, unzipLocation); 
        d.unzip(); 
    }
}

and decompress class is

public class Decompress {
     String _zipFile; 
      String _location; 
    public Decompress(String zipFile, String unzipLocation) {

        // TODO Auto-generated constructor stub
        _zipFile = zipFile; 
        _location = unzipLocation; 

        _dirChecker(""); 
    }
    public void unzip() { 
        try  { 
          FileInputStream fin = new FileInputStream(_zipFile); 
          ZipInputStream zin = new ZipInputStream(fin); 
          ZipEntry ze = null; 
          while ((ze = zin.getNextEntry()) != null) { 
            Log.v("Decompress", "Unzipping " + ze.getName()); 

            if(ze.isDirectory()) { 
              _dirChecker(ze.getName()); 
            } else { 
              FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
              for (int c = zin.read(); c != -1; c = zin.read()) { 
                fout.write(c); 
              } 

              zin.closeEntry(); 
              fout.close(); 
            } 

          } 
          zin.close(); 
        } catch(Exception e) { 
          Log.e("Decompress", "unzip", e); 
        } 

      } 

      private void _dirChecker(String dir) { 
        File f = new File(_location + dir); 

        if(!f.isDirectory()) { 
          f.mkdirs(); 
        } 
      } 
}
user1196969
  • 335
  • 2
  • 5
  • 14
  • Please see Following link of my question and also see my answer of this question [Download Zip File](http://stackoverflow.com/questions/9324103/download-and-extract-zip-file-in-android) – Dipak Keshariya May 31 '12 at 12:29

1 Answers1

0

Carefully look at your code

When downloading and saving file you are saving it in following path

OutputStream output = new FileOutputStream("/mnt/sdcard/external_sd/files.zip");

But while reading you are referring following path

String zipFile = Environment.getExternalStorageDirectory() + "/files.zip"; 

which is equivalent to

String zipFile = "/mnt/sdcard/files.zip"; 

You should write

String zipFile = Environment.getExternalStorageDirectory() + "/external_sd/files.zip";

Hope this helps

Vipul
  • 27,808
  • 7
  • 60
  • 75