我正在为"正常化"写一个小工具。文件和目录名称,使其具有可移植性,互操作性以及由于非ASCII字符,空格和其他恶意字符等文件名导致的愚蠢和痛苦错误的安全性。(请参阅this SuperUser answer以获取简明摘要几个真正安全的角色)。
为此,我想将具有不安全名称的文件/目录重命名为安全名称,包括相应地重命名所有父目录。即使我需要使用java.io
,也不应该成为一个问题;但是我似乎无法重命名包含其他目录的目录(包含文件可以正常工作)。
这是我的方法:
public static File renameSafe(File unsafe) throws IOException {
if (!unsafe.exists()) {
throw new FileNotFoundException(unsafe.toString());
}
// create full "safe" path
File safe = toSafeFile(unsafe);
File origSafe = safe;
while (unsafe != null) {
// rename the lowest unsafe level
File unsafeParent = unsafe.getParentFile();
File safeTarget = new File(unsafeParent, safe.getName());
if (!unsafe.renameTo(safeTarget)) {
throw new IOException("Could not rename " + unsafe + " to " + safe);
}
unsafe = unsafeParent;
safe = safe.getParentFile();
}
return origSafe;
}
当我运行这个主要方法时:
public static void main(String[] args) throws IOException {
File file = new File("\\test\\-bad.folder NAME ÄÖÜßéÂÌ\\.bad folder 2\\test filename-ÄÖÜ.txt");
System.out.println(file);
System.out.println(toSafeFileName(file));
renameSafe(file);
}
我得到以下输出:
\test\-bad.folder NAME ÄÖÜßéÂÌ\.bad folder 2\test filename-ÄÖÜ.txt
\test\_bad_folder_name_aou-eai\_bad_folder_2\test_filename-aou.txt
Exception in thread "main" java.io.IOException: Could not rename \test\-bad.folder NAME ÄÖÜßéÂÌ to \test\_bad_folder_name_aou-eai
at SafeFileName.renameSafe(SafeFileName.java:77)
at SafeFileName.main(SafeFileName.java:90)
前两个级别,一切都按原样重命名;到达叶子文件的祖父母,renameTo()
突然返回false
。
所以我的问题是,我做错了什么?我尝试使用其他文件名(包括100%输入文件名),但似乎File.renameTo()
似乎无法重命名包含其他目录的目录。但是,我个人的赌注是我自己的愚蠢,配上一些模糊的API细节。
好的,这是SSCCE:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class RenameTest
{
public static File toSafeFileName(File file) {
File safe = new File(file.getName().replace("bad", "good"));
for (;;) {
file = file.getParentFile();
if (file == null) {
return safe;
}
if (file.toString().equals(File.separator)) {
safe = new File("", safe.getPath());
} else {
safe = new File(
file.getName().replace("bad", "good"),
safe.getPath());
}
}
}
public static File renameSafe(File unsafe) throws IOException {
if (!unsafe.exists()) {
throw new FileNotFoundException(unsafe.toString());
}
File safe = toSafeFileName(unsafe);
File origSafe = safe;
while (unsafe != null && !unsafe.equals(safe)) {
File unsafeParent = unsafe.getParentFile();
File safeTarget = new File(unsafeParent, safe.getName());
System.out.print(unsafe + " -> " + safeTarget + "? ");
if (unsafe.renameTo(safeTarget)) {
System.out.println("OK.");
} else {
System.out.println("Error!");
throw new IOException();
}
unsafe = unsafeParent;
safe = safe.getParentFile();
}
return origSafe;
}
public static void main(String[] args) throws IOException {
File unsafePath = new File("\\test\\bad1\\bad2\\bad.txt");
if (!unsafePath.getParentFile().mkdirs()) {
throw new IOException("can't create " + unsafePath.getParentFile());
}
if (!unsafePath.createNewFile()) {
throw new IOException("can't create dummy file");
}
File safePath = renameSafe(unsafePath);
if (safePath.exists()) {
System.out.println("Renamed " + unsafePath + " to " + safePath);
} else {
throw new IOException("Safe path " + safePath + " does not exist.");
}
}
}
首次运行时,使用干净的D:\test
目录,一切运行正常:
\test\bad1\bad2\bad.txt -> \test\bad1\bad2\good.txt? OK.
\test\bad1\bad2 -> \test\bad1\good2? OK.
\test\bad1 -> \test\good1? OK.
Renamed \test\bad1\bad2\bad.txt to \test\good1\good2\good.txt
没关系,Java 可以重命名包含目录的目录。
但是,在第二次运行时(首先没有清除D:\test
),我得到以下内容:
\test\bad1\bad2\bad.txt -> \test\bad1\bad2\good.txt? OK.
\test\bad1\bad2 -> \test\bad1\good2? OK.
\test\bad1 -> \test\good1? Error!
Exception in thread "main" java.io.IOException
at funky.core.io.RenameTest2.renameSafe(RenameTest2.java:46)
at funky.core.io.RenameTest2.main(RenameTest2.java:65)
所以似乎问题确实是我没有清理我的测试目录,并且Java无法将目录重命名为已存在的目录 - 因为good1
已经是在第一次运行时创建...正如renameTo()
API中所述,这确实只是我自己的愚蠢。抱歉浪费你的时间。
答案 0 :(得分:2)
对我来说,这......
File unsafeParent = unsafe.getParentFile();
File safeTarget = new File(unsafeParent, safe.getName());
因此,使用\test\-bad.folder NAME ÄÖÜßéÂÌ\.bad folder 2\test filename-ÄÖÜ.txt
作为基本路径,unsafeParent
将等于\test\-bad.folder NAME ÄÖÜßéÂÌ\.bad folder 2
,但safeTarget
现在为unsafeParent + safe.getName()
,因此您不会尝试重命名目录,但unsafeParent
中的文件名为safe.getName()
...
而不是自下而上,自上而下。这意味着您可以重命名目录并使用重命名的引用,列出其中的文件并进行处理。