How to get Windows file details?

前端 未结 2 1076
灰色年华
灰色年华 2020-12-19 09:35

I want to get the \"File description\" and the \"Copyright\" of exe/dll/sys files, as showed in the \"Details\" tab when you right click the file and choose the properties.

2条回答
  •  天命终不由人
    2020-12-19 10:04

    Not a definitive answer but here is an explanation of what you can try.

    Note that this requires that you use JDK 7+.

    Attributes defined by the JDK as standard (DosFileAttributeView, PosixFileAttributeView, AclFileAttributeView) don't account for user-defined metadata.

    What you want is try and see whether your filesystem has support for UserDefinedFileAttributeView. If you have some luck, it will; if you have even more luck, you will have your attributes defined in there.

    Here is a small example on my system (Ubuntu 14.04; filesystem is btrfs). I typed this line on the shell:

    setfattr -n user.comment -v "home sweet home" $HOME
    

    You can then read this using (note: Java 8 code):

    public final class Attr
    {
        public static void main(final String... args)
            throws IOException
        {
            final Path home = Paths.get(System.getProperty("user.home"));
            final UserDefinedFileAttributeView view = Files.getFileAttributeView(
                home, UserDefinedFileAttributeView.class);
    
            if (view != null)
                view.list().forEach(name -> readAttr(view, name));
        }
    
        private static void readAttr(final UserDefinedFileAttributeView view,
            final String name)
        {
            final ByteBuffer buf = ByteBuffer.allocate(1024);
            final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
            try {
                view.read(name, buf);
                buf.flip();
                System.out.println(name + ": " + decoder.decode(buf).toString());
            } catch (IOException e) {
                throw new RuntimeException("exceptions in lambdas suck", e);
            }
        }
    }
    

    This prints:

    comment: home sweet home
    

    As the javadoc says, this is HIGHLY system dependent. Note that the only method to read an attribute by name is to read it into a ByteBuffer (nothing requires that an extended file attribute be text).

提交回复
热议问题