Lock a file for reading even from Operating System

2019-09-19 14:34发布

问题:

I have a encrypted zip with a file inside it. I want to decrypt said file, and use the path of the decrypted file as a input to a new java program.

I don't want anyone to read my decrypted file, nor do anything else with it.

The best solution I've found is to have several different processes monitoring the folder where I extracted my zip, to check if anyone is reading the file or copying to another place.

If I use http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileLock.html, the lock is only respected on the JVM right? If I use the operating system clipboard to copy the file to another place it circumvents my lock right?

Besides that, I have a java program that unzips the file and after that calls the command that will in fact read the unzipped file.This means I can't lock the file on the JVM, I have to lock it to be used by a third party command.

What would be the best approach or what topic I should google?

回答1:

The Java FileLock may be enough for your needs, but doesn't work on all platforms. From the javadoc:

Platform dependencies

This file-locking API is intended to map directly to the native locking facility of the underlying operating system. Thus the locks held on a file should be visible to all programs that have access to the file, regardless of the language in which those programs are written.

Whether or not a lock actually prevents another program from accessing the content of the locked region is system-dependent and therefore unspecified.

How big is the file? You should decrypt the file into memory so that only your process has access to it.