Know if a folder has already finished copying

I am running a java application to unzip files via an ftp. I pass the compressed folders to the input folder which is where the files are taken to decompress and the application runs all the time. The problem is that it does not determine whether the folder has already finished copying or is still in process. I need a class that validates first if the copying process has already finished and then runs the unzipped one.

 3
Author: Marc Lemien, 2016-09-21

5 answers

There is no 100% exact and safe way to know if the transfer has already been completed, but I suggest you do a calculation by relating the transfer speed, against the size of the file. In addition, you can generate a Checksum before starting the transfer, and compare it with the checksum of the transferred file, after the estimated time of the process.

Here I leave you this link to StackOverflow in English, Where You can check an interesting proposal to solve this problem

 1
Author: Ricardo J. Chamorro, 2017-05-23 12:39:23

Calculating copy or transfer time and putting a sleep greetings!

I calculate the transfer time or copy of a file, to then determine how long I should wait to do the following action, example move, change name, unzip in the case of this question. - Gaston Barahona 34 seconds ago edit

long TiempoEspera = valTiempo("miarchivo.txt");
System.out.println("Archivo: " + "miarchivo.txt"+ " Tiempo de transferencia: " + TiempoEspera + " Segundos");
Thread.sleep(1000 * TiempoEspera );
System.out.println("Archivo copiado: c:/miarchivo.txt");


private static long valTiempo(String filepath) throws InterruptedException {

        File fichero = new File(filepath);
        long pesoActual = fichero.length();
        //peso en kb
        long pesoKB = pesoActual / 1024; 
        // mega
        long pesoMB = pesoKB / 1024;  

        //Calculo de tiempo FACTOR_OSI_RED es la ocilacion de la red
        long tasaTransferencia=20;
        double FACTOR_OSI_RED =1.1;
        long tiempo = Math.round((pesoMB / tasaTransferencia ) * FACTOR_OSI_RED); 
        return tiempo;
    }
 1
Author: Gaston Barahona, 2018-10-26 01:00:14

As @Ricardo J. Chamorro mentioned in a previous answer, you can not make assumptions about the transfer speed of files, so using a timeout and once you finish giving up the transfer is incorrect.

The right thing to do would be to do at least one of these two things:

  1. Get the size of the file and then compare it with the size with the size "transferred" every X time, using a timer or as you prefer
  2. calculate a checksum of the file and also check every X time the checksum of the file in the output folder.

Actually 2. it is the one that will assure you with a 100% success that the file has been transferred and has also done it correctly. In environments with high error rates, you might have a file of the same size but corrupt.

 0
Author: Oscar Cuadrado, 2016-10-14 08:08:27

When a file has not increased its size during X time tests to unzip. If decompression worked all OK. If not, test 2X, 4X... some time later. After n attempts you declare the file failed and forget about it.

You can also look at the FTP log, the processes that have the file open, etc.

 -1
Author: mcosta, 2016-09-22 12:20:22

I fix it by pausing the app with Thread.sleep(x); for 3 minutes which guarantees me that the decompression is done complete

 -1
Author: Jeferson Martinez, 2016-09-22 22:24:47