0

I have a problem with .sh script that saves files to zip cyclically. Another program in java tries to read these files and sometimes there is a problem because the sh script does its job and writes files into zip, is it possible to check in java whether the zip file is being written by .sh script?

EDIT: firs, thanks for all advices, I got idea: i can copy existing zip and create new one with diffrient name than the last one f.e temporary, after changes are done, i would change it name and delete old zip. Names are dates

Maczosss
  • 23
  • 5
  • 1
    May be you need to have some time interval (time taken by your sh) and after such period you can drive the Java code – Sabareesh Muralidharan Jun 25 '20 at 11:41
  • I think i can't do that, becose in java program user can request data from those zip file, and when it is changed by bash script it returns error, Thats why i wonder if in Java there is a mechanic that allow us to check if zip file or any other file is changed by other progrems. – Maczosss Jun 25 '20 at 11:46
  • Does this answer your question? [Find out if file has been modified within the last 2 minutes](https://stackoverflow.com/questions/28337961/find-out-if-file-has-been-modified-within-the-last-2-minutes) – dǝɥɔS ʇoıןןƎ Jun 25 '20 at 11:47
  • @Maczosss: You should also specify which operating system you are using, but as a general idea, you could check the modification timestamp. However, you do not see from this whether the the file has really a changed content, or just has been touched. If you need to know, whether there really was a change in content, store a MD5 checksum and recaluclate the checksum afterwards. – user1934428 Jun 25 '20 at 13:00
  • @Maczosss : I suggest that you remove the _zip_ tag from your question, since you basically just want to know whether a file has been modified. The fact that it is a zip-file does not matter, because the general problem is the same for every type of file. – user1934428 Jun 25 '20 at 13:01

2 Answers2

0

You can lock the file while you're operating on it. Code provided from source;

try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ);
    FileLock lock = channel.lock(0, Long.MAX_VALUE, true)) {
    // read from the channel
}
Jason
  • 5,154
  • 2
  • 12
  • 22
0

You could compute a md5 or sha checksum of the zip file periodically and check if it has changed in the last n seconds.

Cedric
  • 532
  • 5
  • 7