dromara / hutool

🍬A set of tools that keep Java sweet.
https://hutool.cn
Other
29.24k stars 7.53k forks source link

zip压缩包中子文件包含特殊字符 #3748

Closed LXC-9349 closed 2 months ago

LXC-9349 commented 2 months ago

版本情况

hutool版本: 5.8.32

问题描述(包括截图)

解压zip压缩包,包含特殊字符文件名会创建失败 cn.hutool.core.compress.ZipReader#readTo(java.io.File, cn.hutool.core.lang.Filter) 可否加个函数参数来处理zipEntry名称的特殊字符 图片

public File readTo(File outFile, Filter<ZipEntry> entryFilter,java.util.function.Function<String, String> nameProcessor) throws IORuntimeException {
        read((zipEntry) -> {
            if (null == entryFilter || entryFilter.accept(zipEntry)) {
                //gitee issue #I4ZDQI
                String path = zipEntry.getName();
                if (FileUtil.isWindows()) {
                    // Win系统下
                    path = StrUtil.replace(path, "*", "_");
                }
                path=nameProcessor.apply(path);
                // FileUtil.file会检查slip漏洞,漏洞说明见http://blog.nsfocus.net/zip-slip-2/
                final File outItemFile = FileUtil.file(outFile, path);
                if (zipEntry.isDirectory()) {
                    // 目录
                    //noinspection ResultOfMethodCallIgnored
                    outItemFile.mkdirs();
                } else {
                    InputStream in;
                    if (null != this.zipFile) {
                        in = ZipUtil.getStream(this.zipFile, zipEntry);
                    } else {
                        in = this.in;
                    }
                    // 文件
                    FileUtil.writeFromStream(in, outItemFile, false);
                }
            }
        });
        return outFile;
    }
  1. 堆栈信息 java.nio.file.InvalidPathException: Malformed input or input contains unmappable characters: /temp/(Ver2.4)-0726.pdf at java.base/sun.nio.fs.UnixPath.encode(UnixPath.java:145) at java.base/sun.nio.fs.UnixPath.(UnixPath.java:69) at java.base/sun.nio.fs.UnixFileSystem.getPath(UnixFileSystem.java:275) at java.base/java.io.File.toPath(File.java:2310) at cn.hutool.core.io.FileUtil.isSub(FileUtil.java:3589) at cn.hutool.core.io.FileUtil.checkSlip(FileUtil.java:3524) at cn.hutool.core.io.FileUtil.file(FileUtil.java:317) at cn.hutool.core.compress.ZipReader.lambda$readTo$0(ZipReader.java:172) at cn.hutool.core.compress.ZipReader.readFromZipFile(ZipReader.java:225) at cn.hutool.core.compress.ZipReader.read(ZipReader.java:201) at cn.hutool.core.compress.ZipReader.readTo(ZipReader.java:163) at cn.hutool.core.compress.ZipReader.readTo(ZipReader.java:150) at cn.hutool.core.util.ZipUtil.unzip(ZipUtil.java:588) at cn.hutool.core.util.ZipUtil.unzip(ZipUtil.java:555) at cn.hutool.core.util.ZipUtil.unzip(ZipUtil.java:542) at cn.hutool.core.util.ZipUtil.unzip(ZipUtil.java:517)