锁定一个文件,甚至从操作系统读取,除了一个进程 [英] Lock a file for reading even from Operating System except a single process
问题描述
我想解密所述文件,并使用解密文件的路径作为新的java程序的输入。
我不想让任何人阅读我的解密文件,也不要做任何其他事情。
我发现最好的解决方案是有几个不同的进程来监视我解压缩zip的文件夹,检查是否有人正在阅读文件或复制到另一个地方。
如果我使用 http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileLock.html ,锁只能在JVM上受到尊重吗?如果我使用操作系统剪贴板将文件复制到另一个地方,它会绕过我的锁?
除此之外,我有一个解压文件的java程序,之后调用命令实际上会读取解压缩的文件。这意味着我无法在JVM上锁定文件,我必须锁定它以供第三方命令使用。
什么是最好的方法或什么主题我应该google?
Java FileLock
可能足以满足您的需求,但不适用于所有平台。从javadoc:
平台依赖关系
此文件锁定API是直接映射到底层操作系统的本机锁定工具。 因此,无论使用这些程序的语言如何,所有持有文件的锁都应该可以访问所有可以访问该文件的程序。
是否实际上阻止另一个程序访问锁定区域的内容是系统依赖的,因此未指定。
该文件有多大?您应该将文件解密到内存中,以便只有您的进程可以访问该文件。
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?
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.
这篇关于锁定一个文件,甚至从操作系统读取,除了一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!