Opening a file that has colons in the filename wit

2019-07-20 12:58发布

问题:

This issue is for Linux/Mac; I don't even think the files could exist with slashes if they were unzipped onto a Windows box.

Hi, I have a requirement to open log files which were output by the popular BRO IDS system. The files are read only and I need to process their contents without changing the filenames.

For some reason the Bro filenames are in this format: "conn.19/00/00-22/00/00.log". (also .gz versions of this, with slashes) I tried running the latest instance of Bro on my Mac with default settings and it produced log files with the same forward slash pattern, so this was a conscious decision someone else made, which I will have to work around.

In loading a file as a Java unit test, either through

File("conn.19/00/00-22/00/00.log")

or by URL

getClass().getClassLoader().getResource("conn.19/00/00-22/00/00.log")

I get the expected problem of the forward slash being interpreted as a non-existing directory.

**I've found a bunch of other downvoted questions about slashes in filenames, mostly by people that wanted to create such problems. I'm looking for a solution to deal with these bad filenames, within Java.

If I can get the file data to an InputStream, I'll be golden.

Update 1: This works better as an update than as a comment, since it changes the nature of the problem.

I discovered that in MacOS, and possibly Linux, the Finder GUI application shows the filenames with forward slashes, like this:

However in the terminal I see this:

Finally, the File.list() command also interprets it this way:

So the solution is to use the colon ':' character when reading in the file. Turns out this is one heck of a rabbit hole which may involve encoding the ':' to "%3a" or double encoding to "%253a". Neither of those have worked yet in my permutations.

See: JDK-7037120 : Identity violation in java.net.URI

Accessing an archive file with a colon in its file name throws a NullPointerException or an AssertionError

标签: java file bro