【. NET deep breathing "ZIP file Operations (2): Dynamically generate ZIP Documents

Source: Internet
Author: User
Tags ziparchive

Through the introduction of a bad article, you know, Ziparchive class represents a Zip document instance, in addition to using the method listed in the previous article to read and write the zip file, you can also directly through the Ziparchive class, dynamically generate a zip file.

File stream operations believe that. NET Dev is not a stranger, ziparchive can either create a zip document in memory or create a new zip file. Because the constructor of the Ziparchive class needs to pass in a Stream object to read and write, the stream can be a stream of memory, or it can be a stream of files.

After you create an ziparchive instance, you can create a compressed item in a compressed document by calling the Createentry method directly, and a Ziparchiveentry instance is returned when you create it.

The Createentry method needs to specify the name of the created compressed item entity, which is the name of the file in the zip document.

If the name is Abc.jpg, then the file abc.jpg is stored in the root directory of the zip. If you want the compression item to be hierarchical, you can specify a relative path.

For example, if you specify One\\abc.txt, a directory named one is created under the root of the zip document, and then the file Abc.txt is created in the one directory. "\ \" can be written as "/", that is, one/abc.txt.

If you enter the name A/b/d.doc, you will create a directory in the zip document, create the B directory under the A directory, and then create the file D.doc in the B directory.

After the Ziparchiveentry instance is created, the open method can be called, and the method returns a Stream object through which the data can be written to the compressed file.

When the zip document is complete, call the Dispose method of the Ziparchive instance to release the resource.

Next look at an example:

            using(FileStream fsout =NewFileStream ("Test.zip", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {                //Create a Zip document instanceZiparchive archive =Newziparchive (fsout, ziparchivemode.create); //create a file under the root directoryZiparchiveentry Entry1 = archive. Createentry ("first item. txt", Compressionlevel.optimal); //writing content to a file                using(StreamWriter wt =NewStreamWriter (Entry1. Open ())) {wt. WriteLine ("This is the first file to compress. "); }                //create a file in relative directoryZiparchiveentry Entry2 = archive. Createentry ("the second \\data.txt", Compressionlevel.optimal); //writing content to a file                using(StreamWriter wt =NewStreamWriter (Entry2. Open ())) {wt. WriteLine ("This is the second file that is compressed. "); }                //Create a third fileZiparchiveentry Entry3 = archive. Createentry ("the third/document.txt", Compressionlevel.optimal); //writing content to a file                using(StreamWriter wt =NewStreamWriter (Entry3. Open ())) {wt. WriteLine ("This is the third file that is compressed. "); }                //to release the zip Document object after the operation is completeArchive.            Dispose (); }


The above code creates a zip document with the following structure:

Isn't it simple? Well, because of the serious shortage of money on the cast, this episode is over.

Sample source code Download

【. NET deep breathing "ZIP file Operations (2): Dynamically generate ZIP Documents

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.