简体   繁体   English

在jar文件夹中创建文件

[英]Create File inside jar folder

I have a jar file abc.jar 我有一个jar文件abc.jar
which has some certain code to create a file inside a folder named temp of same jar file. 它具有一些确定的代码,可在名为同一jar文件的temp的文件夹内创建文件。
like 喜欢

createFile()

Now i want to include abc.jar into my other web project. 现在,我想将abc.jar包含到我的另一个Web项目中。 Which should call this function createFile() and create file accordingly. 哪个应调用此函数createFile()并相应地创建文件。
So is there any way to create file inside jar file. 有什么办法可以在jar文件中创建文件。
I'm aware about jar which is always be compressed and read only. 我知道jar总是压缩和只读的。
But still is there a work around for the same, will be great help for me. 但是仍然有相同的解决方法,这将对我有很大的帮助。
:) :)

For reading a file that is in abc.jar , you can use ClassLoader.getResource() , but I don't think Java provides a similar facility for adding a file to a jar (for security purposes). 为了读取 abc.jar的文件,可以使用ClassLoader.getResource() ,但是我不认为Java提供了类似的功能来将文件添加到jar中(出于安全目的)。

A jar file is simply a zip file, so if you really needed to, you could un-zip it to a temporary directory, add the file, and then re-create the jar with your new file in it. jar文件只是一个zip文件,因此,如果确实需要,可以将其解压缩到一个临时目录中,添加该文件,然后使用其中的新文件重新创建jar。 However, I think this would not achieve the effect you're looking for - whatever file you added to the jar file in this manner would not be available to your running code, because the original jar (without the added file) was already loaded. 但是,我认为这不会达到您想要的效果-以这种方式添加到jar文件中的任何文件都无法用于您的运行代码,因为原始jar(未添加文件)已经加载。

You could extract the complete Jar, add the file to the extracted directory structure and recreate the jar out of the directory structure. 您可以提取完整的Jar,将文件添加到提取的目录结构中,然后从目录结构中重新创建jar。

You can use ZipInputStream and ZipOutputStream for example to achieve this goal. 例如,您可以使用ZipInputStreamZipOutputStream来实现此目标。 Furthermore classes related to ZipFile and ZipEntry can be of help. 此外,与ZipFileZipEntry相关的类可能会有所帮助。

Directly adding a file is not possible as far as I know. 据我所知,不可能直接添加文件。

Take a look at TrueVFS: http://truevfs.net/ (or its predecessor TrueZip https://christian-schlichtherle.bitbucket.io/truezip/ ). 看一下TrueVFS: http ://truevfs.net/(或其前身TrueZip https://christian-schlichtherle.bitbucket.io/truezip/ )。 It provides a virtual file system access to archive files. 它提供了对归档文件的虚拟文件系统访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM