Showing posts with label opc. Show all posts
Showing posts with label opc. Show all posts

Sunday, May 10, 2009

OPC at RVNUG: The code and the slides


First things first: Here is the code and here are the slides.

Thanks to everyone who attended (and participated in) my resent presentation on the Open Packaging Convention (OPC) at the Roanoke Valley .NET User Group (RVNUG). It was a rowdy group (my favorite kind) and it was a blast.

Besides coming up with various connotations for the words 'Package', 'Parts' and 'Stream', we discussed some handy ways to use OPC:
  • Exporting / backing up data
  • Storing a gallery of images with additional metadata (geocoding, timestamp information,etc)
  • Grouping together log files. This application seemed to generate the most interest.
OPC is a dirt simple way to store data in one place without needing a database.

Saturday, April 18, 2009

Roanoke Code Camp 2009 - The Code



Apologies for not getting this published sooner... Between getting ready to move houses and my normal level of laziness and procrastination, I haven't done it. It also took some Googling to figure out how to include code in a BlogSpot post. Enough whinging, here's the code for the OPC (Open Packaging Convention) class I did.

By the way, here is my DevX article on the same subject that got published recently.

Here is the code to create a package:

'Open the package
Using package As Package = package.Open("c:\Example.zip", IO.FileMode.OpenOrCreate)

'Create the uri for the text file
Dim uri As New Uri("/TextFiles/MyTextFile.Text", UriKind.RelativeOrAbsolute)

'Create the part for the text file
Dim part As PackagePart = package.CreatePart(uri, System.Net.Mime.MediaTypeNames.Text.Plain)

'Get the stream for the part
Using stream As Stream = part.GetStream()

'This is the text we'll put in the part
Dim myText As String = "This is an ugly picture of a flower - Roanoke!"

'We need to get the string into a byte array to make it easy to write to the part stream
Dim buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(myText)

'Write to the part stream
stream.Write(buffer, 0, buffer.Length)

End Using

'This is the uri for the flower
Dim uri2 As New Uri("/Images/Flower.jpg", UriKind.RelativeOrAbsolute)

'Create the part for the image
Dim part2 As PackagePart = package.CreatePart(uri2, _
System.Net.Mime.MediaTypeNames.Image.Jpeg)

'Get the stream for this part
Using stream As Stream = part2.GetStream()

'Read the data from the file
Dim buffer As Byte() = File.ReadAllBytes("c:\Flower.jpg")

'write the data to the part
stream.Write(buffer, 0, buffer.Length)

End Using

'Create a relationship between the parts
Dim relationship As PackageRelationship = _
part2.CreateRelationship(part.Uri, TargetMode.Internal, "flower_to_text")

End Using

And here is the code to read that package:

 'Create the package
Using package As Package = package.Open("c:\Example.zip", IO.FileMode.Open)

'We know the Uri for the flower, so let's start there
Dim flowerUri As New Uri("/Images/Flower.jpg", UriKind.RelativeOrAbsolute)

'Get the part using the uri we just set up
Dim flowerPackagePart As PackagePart = _
package.GetPart(flowerUri)

'Create a relationship from the
Dim relationships As PackageRelationshipCollection = _
flowerPackagePart.GetRelationshipsByType("flower_to_text")

'Get the first relationship (in real code, we would want some error checking here)
Dim relationship As PackageRelationship = _
relationships(0)

'Get the part from the relationship (once again, this would need some
' error protection because in a real world app this part might not exist anymore).
Dim textPart As PackagePart = package.GetPart(relationship.TargetUri)

'Get the stream
Using stream As Stream = textPart.GetStream()

'Create a buffer
Dim buffer(stream.Length) As Byte

'Read the stream
stream.Read(buffer, 0, stream.Length)

'Read the text
Dim myText As String = System.Text.ASCIIEncoding.ASCII.GetString(buffer)

'Show the text (the crowd should go wild here)
MessageBox.Show(myText)

End Using

End Using

End Sub