Example #1
0
  private boolean unpackFile(ZipFile zip, byte[] buf, ZipEntry fileEntry, String name)
      throws IOException, FileNotFoundException {
    if (fileEntry == null) fileEntry = zip.getEntry(name);
    if (fileEntry == null)
      throw new FileNotFoundException("Can't find " + name + " in " + zip.getName());

    File outFile = new File(sGREDir, name);
    if (outFile.lastModified() == fileEntry.getTime() && outFile.length() == fileEntry.getSize())
      return false;

    File dir = outFile.getParentFile();
    if (!dir.exists()) dir.mkdirs();

    InputStream fileStream;
    fileStream = zip.getInputStream(fileEntry);

    OutputStream outStream = new FileOutputStream(outFile);

    while (fileStream.available() > 0) {
      int read = fileStream.read(buf, 0, buf.length);
      outStream.write(buf, 0, read);
    }

    fileStream.close();
    outStream.close();
    outFile.setLastModified(fileEntry.getTime());
    return true;
  }
    @Override
    protected String doInBackground(String... sUrl) {

      File folders = new File(Environment.getExternalStorageDirectory() + "/pathofthefile/");
      folders.mkdirs();

      File file;
      file =
          new File(
              Environment.getExternalStorageDirectory()
                  + "/pathofthefile/"
                  + FirstSettings_two.file_video);

      if (!file.exists()) {
        try {
          file.createNewFile();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      } else {
        file.delete();
        try {
          file.createNewFile();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

      try {
        URL url = new URL(sUrl[0]);
        URLConnection connection = url.openConnection();
        connection.connect();
        // this will be useful so that you can show a typical 0-100%
        // progress bar
        int fileLength = connection.getContentLength();

        // download the file
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(file);

        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
          total += count;
          // publishing the progress....
          publishProgress((int) (total * 100 / fileLength));
          output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
        return "Downloaded";
      } catch (Exception e) {
        //   Toast.makeText(context,
        //         "exeption",Toast.LENGTH_LONG).show();

        return null;
      }
    }