The first step to create a ZipPackage object. One would expect this to work just like any other object.
Dim zipFile As New ZipPackage("C:\Temp\test.zip", FileMode.Create)
ZipPackage zipFile = new ZipPackage("C:\\Temp\\test.zip", FileMode.Create);
Unfortunately that isn't the case. Instead, one has to use a factory method in the base class Package.
Dim zipFile As ZipPackage = Package.Open("C:\Temp\test.zip", FileMode.Create)
ZipPackage zipFile = (ZipPackage)Package.Open("C:\\Temp\\test.zip", FileMode.Create );
Though ZipPackage is the default return type for Package.Open, there is no way to actually specify that in any of the overloads making it somewhat difficult to create your own implementations.
Moving on, adding files to the zip package in the normal use case is exceedingly painful. One would think the code would look something like:
zipFile.AddFile("C:/temp/someFile.txt", CompressionOption.Maximum)
zipFile.AddFile("C://temp//someFile.txt", CompressionOption.Maximum);
To add files the .NET way, one has to:
- Create a new URI object that will represent the name of the file inside the ZipFile.
- Determine the correct Mime type for the file.
- Create a new PackagePart using the aforementioned information.
- Open the source file as a Stream.
- Copy said stream into the PackagePart stream.
The below code shows how to do this using a very crude stream copy loop. Note that it should be much faster to use buffers than to read the stream one byte at a time.
Dim newUri As New Uri("/someFile.txt", UriKind.Relative);
Dim part1 As ZipPackagePart = zipFile.CreatePart(newUri, _
System.Net.Mime.MediaTypeNames.Text.Plain, CompressionOption.Maximum)
Using output As Stream = part.GetStream,
input As FileStream = File.OpenRead("C:/temp/someFile.txt")
Dim value As Integer = input.ReadByte
Do Until value = -1
output.WriteByte(CByte(value))
value = input.ReadByte
Loop
End Using
While there isn't a simple way to decompress a zip file, it is far less painful than creating the file in the first place. This code lists all of the files in a zip file and dumps the text ones to the screen.
zipFile = CType(ZipPackage.Open("C:\Temp\test.zip", IO.FileMode.Open), ZipPackage)
For Each part As ZipPackagePart In zipFile.GetParts
Console.WriteLine(part.Uri)
Console.WriteLine(vbTab & "Type:" & part.ContentType)
Console.WriteLine(vbTab & "Option:" & part.CompressionOption)
If part.ContentType.ToLower.Contains ("text/") Then
Using output As New StreamReader(part.GetStream)
Console.WriteLine(output.ReadToEnd)
End Using
End If
Next
For the default ZipPackage subclass, the CreatePart method only supports two compressionOption values, NotCompressed or Normal compression. Other CompressionOption values of Maximum, Fast, or SuperFast use Normal compression.
One last warning, this method doesn't create standard zip files. While the files can be read by normal tools, the zip files will have an addition file called "[Content_Types].xml". Likewise, .NET 3.0 cannot read zip files unless the file contains "[Content_Types].xml". If the file is missing, it silently fails to find any files.