【. NET deep breathing "ZIP file operations (1): Create and read zip documents

Source: Internet
Author: User
Tags ziparchive

. NET IO operations support the creation, reading, and updating of zip files. The use is also relatively simple,. NET has always been the style, everything is ready, as to how to use, please look at the office.

To operate a zip file, the following three classes are used mainly:

1, ZipFile. Represents a class related to a zip file operation. You can use this class to create a zip file, or open a zip file.

2, Ziparchive. Represents a Zip document instance that can be used to create a zip document directly from a Stream object. The ZipFile class is primarily for disk files, and ziparchive represents a zip package, not necessarily a. zip file that resides on your hard disk, because it can be created based on a stream, so it can be in memory or used for network transmission.

3, Ziparchiveentry. The document item entity. Represents a file entity in a ZIP archive package. The zip document takes a file as an item entity, and if there is a directory structure, it is the root of the current document and a relative path is used to describe the file entity. For example: \ Directory 1\ directory 2\ file 1.

First look at how to create a new. zip file from an existing directory that contains all of the content in that directory. The ZipFile class exposes the Createfromdirectory method, which has several overloads, and if other factors are not considered, simply create a. zip file that can use the following overloads:

static void Createfromdirectory (String sourcedirectoryname, String destinationarchivefilename)

method does not return content, the Sourcedirectoryname parameter specifies the source directory to use to create the compressed document, and Destinationarchivefilename is the full path to the newly created compressed file, such as C:\abc.zip.

If you want to control the compression level, you can use the following overloads:

void Createfromdirectory (stringstringbool includebasedirectory)

The CompressionLevel parameter represents the level of compression, and the Includebasedirectory parameter indicates whether the directory name in Sourcedirectoryname is also placed in the root of the. zip file, or False if it is true.

Take a look at the example:

            Try             {                zipfile.createfromdirectory (TextBox1.Text, TextBox2.Text);                 " The operation was successful. ";            }             Catch (Exception ex)            {                = ex. Message;            }             " Tips ", MessageBoxButtons.OK);

is not very simple.

========================================================

Let's take a look at how to decompress. In contrast to creating a compressed file, the ZipFile class also exposes the Extracttodirectory method, which is used to extract the contents of the specified. zip file into the specified directory. One of the more simple overloads is as follows:

Static void Extracttodirectory (stringstring destinationdirectoryname)

The Sourcearchivefilename parameter specifies the full path or relative path of the zip file to unzip, and the Destinationdirectoryname parameter specifies a directory where the contents of the compression are placed.

Take a look at the example:

            Try             {                zipfile.extracttodirectory (txtzipfile.text, txtdestdir.text);                 " unzip successfully. ";            }             Catch (Exception ex)            {                = ex. Message;            }             " Tips ", MessageBoxButtons.OK);

The code should be placed in a try statement block, after all, it is difficult to ensure that no surprises occur.

======================================================

Finally, take a look at how to scan a list of files in a. zip file. The ZipFile class has two methods: if you only consider read-only processing of. zip files, call the OpenRead method. Call the Open method if you want to handle the file in addition to read-only.

Whichever method you call, it returns a Ziparchive instance that represents a zip document that we can access or modify in the code. The Ziparchive object has a entries property that represents the list of files contained in the zip file, and the type only treats the files in the compressed package as an entity, and the directory is not processed as an entity . This property can return a read-only list of ziparchiveentry instances, and each Ziparchiveentry object in the list represents a file in the compressed package.

The Ziparchiveentry class has several properties to obtain file information.

Name: file name, does not contain a relative path.

FullName: Contains relative paths and file names.

Length: The file size before compression.

Compressedlength: File size after compression.

Take a look at the following example:

            Try            {                //Open zip fileZiparchive archive =Zipfile.openread (Txtinputfile.text); //get a list of files                varFiles =Archive.                Entries; //Display listLbfiles.itemssource =files; Msg="Read succeeded. "; }            Catch(Exception ex) {msg=Ex.            Message; }

The above code reads a. zip file and lists the file objects in the compressed document. The results are as shown.

In addition, the Ziparchiveentry class also defines the Extracttofile extension method, which can be used to extract a file from a compressed package.

All right, let's blow it to here today.

This article sample source download

【. NET deep breathing "ZIP file operations (1): Create and read zip documents

Related Article

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.