Here's an example to show:
Create a folder that can contain any subdirectories.
Create a console project and add a class Zipmanager to implement the action we want.
Adds a windowsbase reference to the project. (the name is not very friendly, but the class associated with compression is hidden inside)
Add a reference to the System.IO.Packaging namespace in ZipManager.cs.
Writes a constructor that receives the file path to perform the compression.
Create a method Zipfolder to perform the actual compression operation. Create an instance of package in this method.
Create a function zipdirectory to recursively traverse all subdirectories and subfolders.
For each file, create an instance of PackagePart.
Notice the generation of the relative path here: intercepts more portions than the source path, and replaces the right slash with a left slash.
To copy the contents of the source file into the package, you need to add a CopyStream method.
Zipmanager this class is done. Now we're going to call this class.
Run this program, at the location we specified, a zip folder was generated.
Double-click to open, there is a kind of model that.
Comments:
This example is just to demonstrate the functionality that. NET provides. For this example, it can be extended very well. Here are some places to expand:
1. Applying the System.IO.Packaging.PackUriHelper class makes it easy to generate PackagePart URIs, which must be generated as examples.
2. The third parameter in the Package.createpart (Uri, String, CompressionOption) method provides the compression mode.
3. More use of the skills can be seen here: http://msdn.microsoft.com/zh-cn/library/system.io.packaging.aspx