获取文件扩展名 [英] getting extension of file

查看:111
本文介绍了获取文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某个文件说"New Doc.docx"重命名为"New Doc.txt"

如何确定文件的扩展名是docx

If a file let''s say ''New Doc.docx'' is renamed to ''New Doc.txt''

How can I determine that the file was of extension docx

推荐答案

您只能确定当前的扩展名,而不是过去的扩展名.
You can only determine what the extension currently is, not what it used to be.


在尝试使用文件流之前,请先对其进行查看. .docx 文件只是重命名了 .zip 文件,所以我认为,如果文件流的前两个字节是PK,那么您可以肯定有人将其重命名为 .docx 文件,您可以避免打开它.
Look at the file stream before you try to use it. .docx files are just renamed .zip files, so I think if the first two bytes of the file stream are PK then you can be fairly certain that someone renamed a .docx file and you can avoid opening it.


这很有趣,您的问题在于文件的格式,如果您只是打开Word 2003文件,则您的应用程序不知道如何处理2007年文件,而重命名它不会改变这一事实.

我将向您展示我的意思,将docx文件的扩展名更改为zip并使用可以读取压缩文件的任何应用程序将其打开...现在尝试对doc文件进行相同的操作...看到问题了吗?

顺便说一句,无法知道文件的扩展名之前是否已更改
it''s funny, your problem is in the format of the file, if you''re only opening Word 2003 files then your app doesn''t know how to handle the 2007 files and renaming it won''t change that fact.

I''ll show you what I mean, change the extension of the docx file to zip and open it using any application that can read zipped files... now try the same with a doc file... see the problem?

and btw, there''s no way to know if the extension of a file was changed previously


这篇关于获取文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆