yangys
2024-04-04 2cf2921440e7473ae50796c2cb688f609b3a7995
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.qianwen.core.tool.utils;
 
import java.io.File;
import java.net.URL;
import org.springframework.lang.Nullable;
 
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/utils/PathUtil.class */
public class PathUtil {
    public static final String FILE_PROTOCOL = "file";
    public static final String JAR_PROTOCOL = "jar";
    public static final String ZIP_PROTOCOL = "zip";
    public static final String FILE_PROTOCOL_PREFIX = "file:";
    public static final String JAR_FILE_SEPARATOR = "!/";
 
    @Nullable
    public static String getJarPath() {
        try {
            URL url = PathUtil.class.getResource(StringPool.SLASH).toURI().toURL();
            return toFilePath(url);
        } catch (Exception e) {
            String path = PathUtil.class.getResource(StringPool.EMPTY).getPath();
            return new File(path).getParentFile().getParentFile().getAbsolutePath();
        }
    }
 
    @Nullable
    public static String toFilePath(@Nullable URL url) {
        if (url == null) {
            return null;
        }
        String protocol = url.getProtocol();
        String file = UrlUtil.decode(url.getPath(), Charsets.UTF_8);
        if ("file".equals(protocol)) {
            return new File(file).getParentFile().getParentFile().getAbsolutePath();
        }
        if (JAR_PROTOCOL.equals(protocol) || ZIP_PROTOCOL.equals(protocol)) {
            int ipos = file.indexOf(JAR_FILE_SEPARATOR);
            if (ipos > 0) {
                file = file.substring(0, ipos);
            }
            if (file.startsWith(FILE_PROTOCOL_PREFIX)) {
                file = file.substring(FILE_PROTOCOL_PREFIX.length());
            }
            return new File(file).getParentFile().getAbsolutePath();
        }
        return file;
    }
}