C # compressing and decompressing files (SharpZipLib ),

Source: Internet
Author: User
Tags decompress file

C # compressing and decompressing files (SharpZipLib ),

First download the ICSharpCode. SharpZipLib. dll class library from the Internet

Zip the file or folder. The function is as follows:

1 /// <summary> 2 /// compressed file 3 /// </summary> 4 /// <param name = "fileName"> compressed file path </param> 5 // <param name = "zipName"> compressed file name </param> 6 // <param name = "error"> returned error message </param> 7 /// <returns> </returns> 8 public bool FileToZip (string fileName, string zipName, out string error) 9 {10 error = string. empty; 11 try12 {13 ZipOutputStream s = new ZipOutputStream (File. create (zipName); 14 s. setLevel (6); // 0- Store only to 9-means best compression15 zip (fileName, s); 16 s. finish (); 17 s. close (); 18 return true; 19} 20 catch (Exception ex) 21 {22 error = ex. message; 23 return false; 24} 25} 26 27 28 private void zip (string fileName, ZipOutputStream s) 29 {30 if (fileName [fileName. length-1]! = Path. directorySeparatorChar) 31 fileName + = Path. directorySeparatorChar; 32 Crc32 crc = new Crc32 (); 33 string [] filenames = Directory. getFileSystemEntries (fileName); 34 foreach (string file in filenames) 35 {36 if (Directory. exists (file) 37 {38 zip (file, s); 39} 40 else // otherwise, directly compress the File 41 {42 // open the compressed file 43 FileStream fs = file. openRead (file); 44 45 byte [] buffer = new byte [fs. length]; 46 fs. read (buffer, 0, buffer. length); 47 string tempfile = Path. getFileName (file); 48 ZipEntry entry = new ZipEntry (tempfile); 49 50 entry. dateTime = DateTime. now; 51 entry. size = fs. length; 52 fs. close (); 53 crc. reset (); 54 crc. update (buffer); 55 entry. crc = crc. value; 56 s. putNextEntry (entry); 57 58 s. write (buffer, 0, buffer. length); 59} 60} 61}

 

Decompress the zip file into a file or folder. The function code is as follows:

1 /// <summary> 2 /// decompress file 3 /// </summary> 4 /// <param name = "zipName"> decompress file path </param> 5 /// <param name = "fileDirName"> decompress the package to the folder name </param> 6 /// <param name = "error"> returned error message </param> 7 /// <returns> </returns> 8 public bool ZipToFile (string zipName, string fileDirName, out string error) 9 {10 try11 {12 error = string. empty; 13 // read the compressed File (zip File) and prepare to decompress 14 ZipInputStream s = new ZipInputStream (File. open (zipNam E. trim (), FileMode. open, FileAccess. read); 15 ZipEntry theEntry; 16 17 string rootDir = ""; 18 while (theEntry = s. getNextEntry ())! = Null) 19 {20 string path = fileDirName; 21 // obtain the file in the zip directory 22 rootDir = Path. getDirectoryName (theEntry. name); 23 // get the file Name 24 string fileName = Path. getFileName (theEntry. name); 25 if (string. isNullOrEmpty (fileName) 26 continue; 27 // determines whether it is a top-level file. Yes, put the file directly under fileDirName. No, create directory 28 if (string. isNullOrEmpty (rootDir) 29 {30 if (! Directory. Exists (path) 31 Directory. CreateDirectory (path); 32} 33 else34 {35 path + = "\" + rootDir; 36 if (! Directory. Exists (path) 37 Directory. CreateDirectory (path); 38} 39 40 // write the file stream to 41 if (fileName! = String. empty) 42 {43 FileStream streamWriter = File. create (path + "\" + fileName); 44 45 int size = 2048; 46 byte [] data = new byte [2048]; 47 while (true) 48 {49 if (theEntry. size = 0) 50 break; 51 52 size = s. read (data, 0, data. length); 53 if (size> 0) 54 {55 streamWriter. write (data, 0, size); 56} 57 else58 {59 break; 60} 61} 62 streamWriter. close (); 63} 64} 65 s. close (); 66 return true; 67} 68 catch (Exception ex) 69 {70 error = ex. message; 71 return false; 72} 73}

Call example

1 string error; 2 if (FileToZip (@ "E: \ Document", "document .zip", out error) 3 MessageBox. show ("Succee"); 4 else5 MessageBox. show (error );Compression Example 1 string error; 2 if (ZipToFile (@ "E: \ document .zip", "document", out error) 3 MessageBox. show ("Succee"); 4 else5 MessageBox. show (error );Decompression example

 


In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

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.