et's say you've been tasked with writing an application that stores some related information, such as an album of pictures. You also need to store some metadata about each picture.
One traditional way to store is to use a directory of files. This approach is simple, but opens the door to the files getting out of sync. For example, someone might inadvertently delete either a picture without deleting the associated metadata file, or vice versa. Also, when sharing this data, all the data needs to be put together (often in a zip file). Finally, such an application would be completely custom—you would be able to share the pictures and associated metadata only by publishing the exact data format so others could write programs that read and write the same format.
Another approach is to use a database. This is a proven method, and reduces the possibility of the files getting out of sync, but it requires a database engine (such as SQL Server Express, SQL Server compact, etc.). Using a database also requires you to write database access code. Using a database, you'll still need a way to import/export the data in such a way that it can be shared with other people—in other words, you still have custom format problems.
Enter the Open Packaging Convention
One attractive method is to use the same technology that is used to store Office 2007 files—the Open Packaging Convention (OPC). OPC is based on storing related data in a zip file.
To see the OPC in action, create a document using Word 2007 (or use Word 2003 with the Office Compatibility Pack
). Save the document as something like Document.docx
. Close the file and rename the file, changing the extension to .zip
After doing that, you can view the contents of the package (see Figure 1
) using your favorite ZIP utility (WinZip, WinRAR, Windows, etc.).
|Figure 1. Zipped Contents: After renaming a .docx file so it has a .zip extension, you can examine the contents with any ZIP file utility.|
|Author's Note: While it's possible to view the contents of a package / extract files with virtually any zip utility, making changes to the package file might make the package unreadable by the packaging classes. One utility that lets you modify the package without such problems is 7-Zip.
Creating a Package in .NET
With a little effort, you don't actually need Word to create a package; creating one in .NET is easy. You'll find most of the classes you need in the System.IO.Packaging namespace.
Dim package As Package = _
|Author's Note: Whenever you open a package programmatically, it's important to remember to close the package by either calling package.Close() or Dispose(). Failure to do this will leave the package file locked for the lifetime of your application.
Our Data Model
Here's an example that creates the photo album application described earlier, using OPC. The application adds a number of image files (picture
) and stores some additional information about each picture (pictureInfo
), so each picture will have an associated XML file. Therefore the package you need to create has two parts, as shown in Figure 2
|Figure 2. Sample Data Model: Here's the data model for the picture album program example.|
Add an Image
Adding a part requires a few steps. First, create the package part:
Dim uri As New Uri("/pictures/Bee.jpg", _
Dim picturePart As PackagePart = _
Note that the preceding code doesn't actually put any actual data in the package. To put the image in the PackagePart, you need to open a stream (via the PackagePart.GetStream
method), and then write the image data to that stream:
Using stream As IO.Stream = _
Dim bytes As Byte() = IO.File.ReadAllBytes("c:\Bee.jpg")
stream.Write(bytes, 0, bytes.Length)