Create (sane/safe) filename from any (unsafe) string

前端 未结 11 1377
醉酒成梦
醉酒成梦 2020-12-28 12:45

I want to create a sane/safe filename (i.e. somewhat readable, no \"strange\" characters, etc.) from some random Unicode string (mich might contain just anything).

(

11条回答
  •  半阙折子戏
    2020-12-28 13:39

    No solutions here, only problems that you must consider:

    • what is your minimum maximum filename length? (e.g. DOS supporting only 8-11 characters; most OS don't support >256 characters)

    • what filenames are forbidden in some context? (Windows still doesn't support saving a file as CON.TXT -- see https://blogs.msdn.microsoft.com/oldnewthing/20031022-00/?p=42073)

    • remember that . and .. have specific meanings (current/parent directory) and are therefore unsafe.

    • is there a risk that filenames will collide -- either due to removal of characters or the same filename being used multiple times?

    Consider just hashing the data and using the hexdump of that as a filename?

提交回复
热议问题