Contenidors CFB
Contenidors CFB
Compound File Binary (CFB) és el format de contenidor utilitzat pels fitxers Outlook .msg.
Aspose.Email FOSS per a .NET ofereix CfbReader per a la lectura i CfbWriter / CfbDocument per a l’escriptura directa de contenidors CFB.
Lectura de contenidors CFB
Obrir un fitxer CFB
using Aspose.Email.Foss.Cfb;
// From file
using var reader = CfbReader.FromFile("sample.msg");
// From stream
using var stream = File.OpenRead("sample.msg");
using var reader2 = CfbReader.FromStream(stream);
// From byte array
var data = File.ReadAllBytes("sample.msg");
using var reader3 = new CfbReader(data);Inspecciona la capçalera i l’estructura
Console.WriteLine($"Version: {reader.MajorVersion}");
Console.WriteLine($"Sector size: {reader.SectorSize}");
Console.WriteLine($"Mini-sector size: {reader.MiniSectorSize}");
Console.WriteLine($"Directory entries: {reader.DirectoryEntryCount}");
Console.WriteLine($"Streams materialized: {reader.MaterializedStreamCount}");
Console.WriteLine($"File size: {reader.FileSize}");Recórrer l’arbre de directoris
// Full recursive traversal with depth
foreach (var (depth, entry) in reader.IterTree())
{
var indent = new string(' ', depth * 2);
Console.WriteLine($"{indent}{entry.Name} [{entry.ObjectType}]");
}
// Iterate only storages
foreach (var entry in reader.IterStorages())
Console.WriteLine($"Storage: {entry.Name}");
// Iterate only streams
foreach (var entry in reader.IterStreams())
Console.WriteLine($"Stream: {entry.Name} ({entry.StreamSize} bytes)");Navega entre els fills i resol camins
// Direct children of the root
foreach (var entry in reader.IterChildren(CfbConstants.RootStreamId))
Console.WriteLine(entry.Name);
// Resolve a nested path
var target = reader.ResolvePath(new[] { "__substg1.0_0037001F" });
if (target != null)
{
var streamData = reader.GetStreamData(target.Value);
Console.WriteLine($"Stream data: {streamData.Length} bytes");
}Cerca un fill per nom
var child = reader.FindChildByName(CfbConstants.RootStreamId, "__properties_version1.0");
if (child != null)
{
var bytes = reader.GetStreamData(child.Value);
Console.WriteLine($"Property stream: {bytes.Length} bytes");
}Escrivint contenidors CFB
Crea un document CFB
using Aspose.Email.Foss.Cfb;
var root = new CfbStorage("Root Entry");
root.AddStream(new CfbStream("data.bin", new byte[] { 0x01, 0x02, 0x03 }));
var subStorage = new CfbStorage("SubFolder");
subStorage.AddStream(new CfbStream("nested.txt", System.Text.Encoding.UTF8.GetBytes("hello")));
root.AddStorage(subStorage);
var document = new CfbDocument(root);Serialitzar a bytes o fitxer
// To byte array
byte[] cfbBytes = CfbWriter.ToBytes(document);
// To file
CfbWriter.WriteFile(document, "output.cfb");Verificació d’anada i tornada
// Write and reload
CfbWriter.WriteFile(document, "test.cfb");
using var reloaded = CfbReader.FromFile("test.cfb");
foreach (var entry in reloaded.IterChildren(CfbConstants.RootStreamId))
Console.WriteLine(entry.Name);Carrega CFB existent com a CfbDocument
CfbDocument proporciona un model en memòria per a un fitxer CFB existent:
var doc = CfbDocument.FromFile("sample.msg");
Console.WriteLine($"Root: {doc.Root.Name}");
Console.WriteLine($"Version: {doc.MajorVersion}.{doc.MinorVersion}");Vegeu també
- Lectura de fitxers MSG — Accés MSG de nivell alt mitjançant MapiMessage
- Propietats MAPI — Accés als fluxos de propietats MAPI
- Funcionalitats — Referència completa de funcionalitats