检测文件格式并检查格式兼容性
有时有必要在打开之前确定文档的格式,因为文件扩展名不能保证文件的内容是适当的。 例如,已知Crystal Reports经常以RTF格式输出文档,但给出了。doc扩展。
Aspose.Words提供了获取有关文件类型的信息的功能,以便在不确定文件的实际内容时避免异常。
无一例外地检测文件格式
当您处理各种文件格式的多个文档时,您可能需要将可以由Aspose.Words处理的文件与不能处理的文件分开。 您可能还想知道为什么某些文档无法处理。
如果您尝试将文件加载到Document对象中,并且Aspose.Words无法识别文件格式或不支持该格式,Aspose.Words将引发异常。 您可以捕获这些异常并对其进行分析,但Aspose.Words还提供了DetectFileFormat方法,使我们能够快速确定文件格式,而无需加载带有可能异常的文档。 此方法返回一个FileFormatInfo对象,其中包含检测到的有关文件类型的信息。
检查文件格式兼容性
我们可以检查所选文件夹中所有文件的格式兼容性,并按格式将它们排序到相应的子文件夹中。
由于我们正在处理文件夹中的内容,因此我们需要做的第一件事是使用Directory
类的GetFiles方法(来自System.IO
命名空间)获取此文件夹中所有文件的集合。
下面的代码示例演示如何获取文件夹中所有文件的列表:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::ArrayPtr<System::String> fileList = System::IO::Directory::GetFiles(dataDir); |
当收集到所有文件时,其余的工作由DetectFileFormat方法完成,该方法检查文件格式。
下面的代码示例演示如何遍历收集的文件列表,检查每个文件的格式,并将每个文件移动到相应的文件夹:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// The path to the documents directory. | |
System::String dataDir = GetInputDataDir_LoadingAndSaving(); | |
System::String supportedDir = dataDir + u"Supported_out"; | |
System::String unknownDir = dataDir + u"Unknown_out"; | |
System::String encryptedDir = dataDir + u"Encrypted_out"; | |
System::String pre97Dir = dataDir + u"Pre97_out"; | |
// Create the directories if they do not already exist | |
if (!System::IO::Directory::Exists(supportedDir)) | |
{ | |
System::IO::Directory::CreateDirectory_(supportedDir); | |
} | |
if (!System::IO::Directory::Exists(unknownDir)) | |
{ | |
System::IO::Directory::CreateDirectory_(unknownDir); | |
} | |
if (!System::IO::Directory::Exists(encryptedDir)) | |
{ | |
System::IO::Directory::CreateDirectory_(encryptedDir); | |
} | |
if (!System::IO::Directory::Exists(pre97Dir)) | |
{ | |
System::IO::Directory::CreateDirectory_(pre97Dir); | |
} | |
System::ArrayPtr<System::String> fileList = System::IO::Directory::GetFiles(dataDir); | |
// Loop through all found files. | |
for (System::String const &fileName: fileList) | |
{ | |
// Extract and display the file name without the path. | |
System::String nameOnly = System::IO::Path::GetFileName(fileName); | |
std::cout << nameOnly.ToUtf8String(); | |
// Check the file format and move the file to the appropriate folder. | |
System::SharedPtr<FileFormatInfo> info = FileFormatUtil::DetectFileFormat(fileName); | |
// Display the document type. | |
switch (info->get_LoadFormat()) | |
{ | |
case LoadFormat::Doc: | |
std::cout << "\tMicrosoft Word 97-2003 document." << std::endl; | |
break; | |
case LoadFormat::Dot: | |
std::cout << "\tMicrosoft Word 97-2003 template." << std::endl; | |
break; | |
case LoadFormat::Docx: | |
std::cout << "\tOffice Open XML WordprocessingML Macro-Free Document." << std::endl; | |
break; | |
case LoadFormat::Docm: | |
std::cout << "\tOffice Open XML WordprocessingML Macro-Enabled Document." << std::endl; | |
break; | |
case LoadFormat::Dotx: | |
std::cout << "\tOffice Open XML WordprocessingML Macro-Free Template." << std::endl; | |
break; | |
case LoadFormat::Dotm: | |
std::cout << "\tOffice Open XML WordprocessingML Macro-Enabled Template." << std::endl; | |
break; | |
case LoadFormat::FlatOpc: | |
std::cout << "\tFlat OPC document." << std::endl; | |
break; | |
case LoadFormat::Rtf: | |
std::cout << "\tRTF format." << std::endl; | |
break; | |
case LoadFormat::WordML: | |
std::cout << "\tMicrosoft Word 2003 WordprocessingML format." << std::endl; | |
break; | |
case LoadFormat::Html: | |
std::cout << "\tHTML format." << std::endl; | |
break; | |
case LoadFormat::Mhtml: | |
std::cout << "\tMHTML (Web archive) format." << std::endl; | |
break; | |
case LoadFormat::Odt: | |
std::cout << "\tOpenDocument Text." << std::endl; | |
break; | |
case LoadFormat::Ott: | |
std::cout << "\tOpenDocument Text Template." << std::endl; | |
break; | |
case LoadFormat::DocPreWord60: | |
std::cout << "\tMS Word 6 or Word 95 format." << std::endl; | |
break; | |
case LoadFormat::Unknown: | |
default: | |
std::cout << "\tUnknown format." << std::endl; | |
break; | |
} | |
// Now copy the document into the appropriate folder. | |
if (info->get_IsEncrypted()) | |
{ | |
std::cout << "\tAn encrypted document." << std::endl; | |
System::IO::File::Copy(fileName, System::IO::Path::Combine(encryptedDir, nameOnly), true); | |
} | |
else | |
{ | |
switch (info->get_LoadFormat()) | |
{ | |
case LoadFormat::DocPreWord60: | |
System::IO::File::Copy(fileName, System::IO::Path::Combine(pre97Dir, nameOnly), true); | |
break; | |
case LoadFormat::Unknown: | |
System::IO::File::Copy(fileName, System::IO::Path::Combine(unknownDir, nameOnly), true); | |
break; | |
default: | |
System::IO::File::Copy(fileName, System::IO::Path::Combine(supportedDir, nameOnly), true); | |
break; | |
} | |
} | |
} | |
使用File
类的Move
方法,从相同的System.IO
命名空间将文件移动到适当的子文件夹中。
上面的示例中使用了以下文件。 文件名在左边,其描述在右边:
文件组 | 输入文件 | 类型 |
---|---|---|
支持的文件格式 | Test File (DOC).doc | Microsoft Word95/6.0或Microsoft Word97-2003文件。 |
Test File (DOT).dot | Microsoft Word95/6.0或Microsoft Word97-2003模板。 | |
Test File (DOCX).docx | Office打开XMLWordprocessingML文档没有宏。 | |
Test File (DOCM).docm | Office使用宏打开XMLWordprocessingML文档。 | |
Test File (DOTX).dotx | Office OpenXMLWordprocessingML模板。 | |
Test File (DOTM).dotm | Office使用宏打开XMLWordprocessingML模板。 | |
Test File (XML).xml | FlatOPCOOXML文档。 | |
Test File (RTF).rtf | 富文本格式文档。 | |
Test File (WordML).xml | Microsoft Word2003WordprocessingML文档。 | |
Test File (HTML).html | HTML文件。 | |
Test File (MHTML).mhtml | MHTML(Web存档)文档。 | |
Test File (ODT).odt | OpenDocument文本(OpenOffice作家)。 | |
Test File (OTT).ott | OpenDocument文档模板。 | |
测试文件(DocPreWord60)。医生 | Microsoft Word2.0文件。 | |
加密文档 | Test File (Enc).doc | 加密的Microsoft Word95/6.0或Microsoft Word97–2003文档。 |
Test File (Enc).docx | 加密的Office OpenXMLWordprocessingML文档。 | |
不支持的文件格式 | Test File (JPG).jpg | JPEG图像文件。 |