检查文件/路径是否为别名、快捷方式或符号链接。

huangapple go评论62阅读模式
英文:

Check if File / Path is Alias, Shortcut or Symbolic link

问题

我需要遍历文件夹中的文件,并找到所有的图像。我需要包括子目录,以及制作的快捷方式(在Windows上),别名(对于Mac用户)或符号链接。

我找到了一个只能用于符号链接的工作API...我似乎无法确定一个文件是别名还是快捷方式..

我有的代码:

private List<Path> getFiles(String folder) throws IOException {

    List<Path> files = new ArrayList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(folder))) {

        for (Path p : stream) {
             
            System.out.println(p);
            if(isLink(p.toFile())) {
                
                files.addAll(getFiles(p.toFile().getCanonicalPath()));
            }
            
            String mimeType = Files.probeContentType(p);
            if (StringUtils.isNotBlank(mimeType) && mimeType.contains("image")) {
                files.add(p);
            }
        }
        return files;
    }
}

public static boolean isLink(File file) {
    try {
        if (!file.exists())
            return true;
        else {

            String cnnpath = file.getCanonicalPath();
            String abspath = file.getAbsolutePath();
            return !abspath.equals(cnnpath);
        }
    } catch (IOException ex) {
        System.err.println(ex);
        return true;
    }
}

一些帮助将不胜感激!

谢谢!

英文:

I need to traverse files in a folder, and find all images. I need to include subdirectories, as well as made shortcuts (on windows), aliases (for mac users) or symbolic links.

I found working API that only works for the symbolic links.. I can't seem to determine if a file is an Alias or a shortcut..

Code I have:

private List&lt;Path&gt; getFiles(String folder) throws IOException {

	List&lt;Path&gt; files = new ArrayList&lt;&gt;();
	try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(Paths.get(folder))) {
		
	
		
		for (Path p : stream) {
			 
			System.out.println(p);
			if(isLink(p.toFile())) {
				
				files.addAll(getFiles(p.toFile().getCanonicalPath()));
			}
			
			
			
			String mimeType = Files.probeContentType(p);
			if (StringUtils.isNotBlank(mimeType) &amp;&amp; mimeType.contains(&quot;image&quot;)) {
				files.add(p);
			}

		}
    return files;
	}

and

public static boolean isLink(File file) {
	try {
		if (!file.exists())
			return true;
		else {

			String cnnpath = file.getCanonicalPath();
			String abspath = file.getAbsolutePath();
			return !abspath.equals(cnnpath);
		}
	} catch (IOException ex) {
		System.err.println(ex);
		return true;
	}
}

Some help would be greatly appreciated!

Thanks!

答案1

得分: 1

To avoid toFile() calls you could also use Files.isSymbolicLink(Path p) and p.toRealPath() to determine the target of symbolic links for the paths you use.

Windows shortcuts are not modelled in Java File / Path APIs. An implementation which reads Windows shortcut binary format can be found here in another SO question - it's better if using drive letters, not using UNC pathnames.

I can't help for MacOS alias.

You should keep track of the directories visited in getFiles to avoid an infinite loop when following links.

英文:

To avoid toFile() calls you could also use Files.isSymbolicLink(Path p) and p.toRealPath() to determine the target of symbolic links for the paths you use.

Windows shortcuts are not modelled in Java File / Path APIs. An implementation which reads Windows shortcut binary format can be found here in another SO question - it better if using drive letters not using UNC pathnames.

I can't help for MacOS alias.

You should keep track of the directories visited in getFiles to avoid infinite loop when following links.

huangapple
  • 本文由 发表于 2020年7月23日 00:00:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63038407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定