简体   繁体   English

Java在Mac OSX上将别名(符号链接)报告为大小0。 如何获得真实的文件大小?

[英]Java reports alias (symlink) as size 0 on Mac OSX. How do I get the true file size?

File file = new File("path to file alias foo");

where "path to file alias foo" is an alias reports file size to be 0 instead of the actual file size. 其中“文件别名foo的路径”是一个别名,报告文件大小为0,而不是实际文件大小。 I found a workaround to test for aliases: 我找到了一种解决方法来测试别名:

public boolean isLink() {
        try {
            if (file.getAbsolutePath().equals(file.getCanonicalPath())) {
                return false;
            }
        } catch (IOException ex) {
            logger.severe(ex.getMessage());
        }
        return true;
    }

EDIT Actually this code does not work, as pointed out by a poster below. 编辑实际上,正如下面的张贴者所指出的,此代码不起作用。 I was trying to adapt a solution from a linux symlink example, but I didn't realize that finder aliases and symlinks were not the same. 我试图从linux symlink示例改编一种解决方案,但是我没有意识到finder别名和symlink是不一样的。

NOT! 不! this seems to work, but .... 这似乎可行,但是....

file.getCanonicalFile().length();

still reports file length to be 0. Can anyone point me in the right direction? 仍然报告文件长度为0。有人能指出我正确的方向吗?

Finder aliases are a different beast altogether from normal symbolic links. Finder别名与普通符号链接完全不同。 The *nix tools on OS X are not aware of aliases at all, because they're stored in the resource fork, I believe. 我相信OS X上的* nix工具根本不了解别名,因为它们存储在资源派生中。 If you install osxutils , you can use this shell command to get the target of an alias: 如果安装osxutils ,则可以使用以下shell命令获取别名的目标:

hfsdata -e the-alias

From Java, I don't know of a better way of doing this other than calling Runtime.exec(...) . 从Java中,除了调用Runtime.exec(...)之外,我不知道有更好的方法。

Also, I just a did a quick check, and your function for detecting aliases does not work. 另外,我只是做了一个快速检查,您的别名检测功能不起作用。 AFAICT, Java is not aware of Finder aliases. AFAICT,Java不了解Finder别名。 If you really want to support them, then you'll either need to use something like osxutils , or use some platform-specific code to read resource forks (will probably involve JNI). 如果您真的想支持它们,那么您要么需要使用osxutils类的osxutils ,要么需要使用特定于平台的代码来读取资源派生(可能涉及JNI)。 Neither option is pretty. 这两个选项都不是很漂亮。

If you go the JNI route, check out the Alias Manager Reference documentation. 如果您选择JNI路线,请查看Alias Manager参考文档。 The relevant functions are FSIsAliasFile and FSResolveAliasFile . 相关功能为FSIsAliasFileFSResolveAliasFile

You can use the FileRef Interface from the O'Reilly Java NIO API. 您可以使用O'Reilly Java NIO API中的FileRef接口。 I believe the getAttribute() method can handle symbolic links as you want, but I have not tried it on Mac OSX. 我相信getAttribute()方法可以根据需要处理符号链接,但是我还没有在Mac OSX上尝试过。 From the docs : 文档

The options array may be used to indicate how symbolic links are handled for the case that the file is a symbolic link. options数组可用于指示在文件是符号链接的情况下如何处理符号链接。 By default, symbolic links are followed and the file attribute of the final target of the link is read. 默认情况下,遵循符号链接,并且读取链接最终目标的文件属性。 If the option NOFOLLOW_LINKS is present then symbolic links are not followed and so the method returns the file attribute of the symbolic link. 如果存在选项NOFOLLOW_LINKS,则不遵循符号链接,因此该方法返回符号链接的文件属性。

大小=新File(file.getCanonicalPath())。length();

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

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