Lock a file for reading even from Operating System except a single process

五迷三道 提交于 2019-12-25 19:38:00

问题


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.



来源:https://stackoverflow.com/questions/23547224/lock-a-file-for-reading-even-from-operating-system-except-a-single-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!