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();
}
}
}
Carefully look at your code
When downloading and saving file you are saving it in following path
But while reading you are referring following path
which is equivalent to
You should write
Hope this helps