. NET library to generate a Zip file method _ Practical Tips

Source: Internet
Author: User
Tags zip folder

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.