Use sharpziplib to easily compress and decompress files
Sharpziplib (0.84) of the latest version adds many new features, including the fastzip class, which allows us to easily compress a directory into a compressed package, the test results show that the file contains Chinese characters and spaces.
1 /**/ /// <Summary>
2 /// Create a ZIP archive.
3 /// </Summary>
4 /// <Param name = "FILENAME"> The filename. </Param>
5 /// <Param name = "directory"> The directory to zip. </Param>
6 Public Static Void Packfiles ( String Filename, String Directory)
7 {
8 Try
9 {
10 Fastzip FZ = New Fastzip ();
11 FZ. createemptydirectories = True ;
12 FZ. createzip (filename, directory, True , "" );
13 FZ = Null ;
14 }
15 Catch (Exception)
16 {
17Throw;
18}
19 }
20
21 /**/ /// <Summary>
22 /// Unpacks the files.
23 /// </Summary>
24 /// <Param name = "file"> The file. </Param>
25 /// <Returns> If succeed return true, otherwise false. </Returns>
26 Public Static Bool Unpackfiles ( String File, String DIR)
27 {
28 Try
29 {
30 If ( ! Directory. exists (DIR ))
31 Directory. createdirectory (DIR );
32
33 Zipinputstream s = New Zipinputstream (file. openread (File ));
34
35 Zipentry theentry;
36 While (Theentry = S. getnextentry ()) ! = Null )
37 {
38
39 String Directoryname = Path. getdirectoryname (theentry. Name );
40 String Filename = Path. getfilename (theentry. Name );
41
42 If (Directoryname ! = String. Empty)
43 Directory. createdirectory (Dir + Directoryname );
44
45 If (Filename ! = String. Empty)
46 {
47 Filestream streamwriter = File. Create (Dir + Theentry. Name );
48 // Filestream streamwriter = file. Create (path. Combine (Dir, theentry. Name ));
49 Int Size = 2048 ;
50 Byte [] Data = New Byte [ 2048 ];
51 While ( True )
52 {
53 Size = S. Read (data, 0 , Data. Length );
54 If (Size > 0 )
55 {
56Streamwriter. Write (data,0, Size );
57}
58 Else
59 {
60Break;
61}
62 }
63
64 Streamwriter. Close ();
65 }
66 }
67 S. Close ();
68 Return True ;
69 }
70 Catch (Exception)
71 {
72Throw;
73}
AboveCodeTo be referenced:
Using Icsharpcode. sharpziplib. Zip;
Using System. IO;
The usage is not described.
You can download the latest version of sharpziplib from its official website:
Http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
Or directly use this link to download:
- Compiled assembly with documentation and install scripts download [488 KB]
- Source code and samples download [622 KB]
- Help file only download [485 KB]