Android download a zip file and unpack it
Make sure the INTERNET and WRITE_EXTERNAL_STORAGE permissions are granted in the manifest file.
This method will download the file from the provided url and save it to destinationFilePath. This method should be run in a background thread.
public void downloadZipFile(String urlStr, String destinationFilePath) { InputStream input = null; OutputStream output = null; HttpURLConnection connection = null; try { URL url = new URL(urlStr); connection = (HttpURLConnection) url.openConnection(); connection.connect(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { Log.d("downloadZipFile", "Server ResponseCode=" + connection.getResponseCode() + " ResponseMessage=" + connection.getResponseMessage()); } // download the file input = connection.getInputStream(); Log.d("downloadZipFile", "destinationFilePath=" + destinationFilePath); new File(destinationFilePath).createNewFile(); output = new FileOutputStream(destinationFilePath); byte data[] = new byte[4096]; int count; while ((count = input.read(data)) != -1) { output.write(data, 0, count); } } catch (Exception e) { e.printStackTrace(); return; } finally { try { if (output != null) output.close(); if (input != null) input.close(); } catch (IOException e) { e.printStackTrace(); } if (connection != null) connection.disconnect(); } File f = new File(destinationFilePath); Log.d("downloadZipFile", "f.getParentFile().getPath()=" + f.getParentFile().getPath()); Log.d("downloadZipFile", "f.getName()=" + f.getName().replace(".zip", "")); }
Sample usage of the above method.
File gameDir = new File("/data/data/" + getActivity().getPackageName() + "/games"); gameDir.mkdirs(); downloadZipFile("https://github.com/gabrielecirulli/2048/archive/master.zip", "/data/data/" + getActivity().getPackageName() + "/games/2048.zip");
This method will unzip/unpack the zip file into the current folder, given the absolute path of the zip file. Ex: /var/example/game.zip -> /var/example/game
public boolean unpackZip(String filePath) { InputStream is; ZipInputStream zis; try { File zipfile = new File(filePath); String parentFolder = zipfile.getParentFile().getPath(); String filename; is = new FileInputStream(filePath); zis = new ZipInputStream(new BufferedInputStream(is)); ZipEntry ze; byte[] buffer = new byte[1024]; int count; while ((ze = zis.getNextEntry()) != null) { filename = ze.getName(); if (ze.isDirectory()) { File fmd = new File(parentFolder + "/" + filename); fmd.mkdirs(); continue; } FileOutputStream fout = new FileOutputStream(parentFolder + "/" + filename); while ((count = zis.read(buffer)) != -1) { fout.write(buffer, 0, count); } fout.close(); zis.closeEntry(); } zis.close(); } catch(IOException e) { e.printStackTrace(); return false; } return true; }
Sample usage of the above method for unpacking a zip file. This method is better off to run in a background task.
unpackZip("/var/example/game.zip");
Put the above together in an AsyncTask embedded in a Fragment.
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts