Ejemplo n.º 1
0
  @Override
  public void run() {
    try {
      if (listener != null)
        listener.onDownloadStarted(lastFile.getServerName(), lastFile.getServerPath());

      URL url = new URL(updateFilesFolderURL + lastFile.getServerPath().replace('\\', '/'));

      URLConnection conn = url.openConnection();

      InputStream is = conn.getInputStream();

      lastFileSize = conn.getContentLength();

      File destinationFile =
          new File(downloadPath + lastFile.getServerPath()); // File name is included

      Files.createDirectories(
          destinationFile.toPath().getParent()); // Create any sub folder if not exists

      BufferedOutputStream bufferedOutputStream =
          new BufferedOutputStream(new FileOutputStream(destinationFile));

      byte[] buffer = new byte[8192];
      int bytesRead;

      while ((bytesRead = is.read(buffer)) != -1) {
        lastReceivedBytes += bytesRead;
        bufferedOutputStream.write(buffer, 0, bytesRead);

        if (listener != null) listener.onDownloadProgressChanged(lastReceivedBytes, lastFileSize);
      }
      bufferedOutputStream.flush();
      bufferedOutputStream.close();

      GZIP.decompress(
          destinationFile, new File(downloadPath + lastFile.getPath())); // File name is included

      destinationFile.delete();

      if (listener != null)
        listener.onDownloadFinished(lastFile.getServerName(), lastFile.getServerPath());

      nextDownload();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }