C # ICSharpCode. SharpZipLib. dll File compression and decompression function sorting. It is often used to upload or download files,
In our work, we often need to compress the file. The commonly used dll is ICSharpCode. sharpZipLib. dll is not much nonsense, and there is a lot of information on the Internet. I sorted out the two most commonly used functions and provided a common class, this allows you to quickly compress and decompress data at work.
Official Website: http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
1. Add a reference to ICSharpCode. SharpZipLib. dll in the project;
2. Import the class (Imports) on the encoding interface defined in ICSharpCode. SharpZipLib)
1 using ICSharpCode. sharpZipLib. zip; 2 using System; 3 using System. IO; 4 5 namespace ZTO. wayBill. utilities 6 {7 /// <summary> 8 // compression Class 9 /// http://www.cnblogs.com/kissdodog/p/3525295.html 10 11 /// </summary> 12 public class ZipHelper13 {14 /// <summary> 15 // ZIP folder 16 /// </summary> 17 // <param name = "source"> source directory </param> 18 // <param name = "s"> ZipOutputStream object </param> 19 public static void Compress (string source, zipOutputStream s) 20 {21 string [] filenames = Directory. getFileSystemEntries (source); 22 foreach (string file in filenames) 23 {24 if (Directory. exists (file) 25 {26 // recursively compress subfolders 27 Compress (file, s); 28} 29 else30 {31 using (FileStream fs = File. openRead (file) 32 {33 byte [] buffer = new byte [4*1024]; 34 // remove the drive letter here, for example, D: \ 123 \ 1.txt remove D: 35 ZipEntry entry = new ZipEntry (file. replace (Path. getPathRoot (file), ""); 36 entry. dateTime = DateTime. now; 37 s. putNextEntry (entry); 38 int sourceBytes; 39 do40 {41 sourceBytes = fs. read (buffer, 0, buffer. length); 42 s. write (buffer, 0, sourceBytes ); 43} while (sourceBytes> 0 ); 44} 45} 46} 47} 48 49 // <summary> 50 // extract 51 /// </summary> 52 /// <param name = "sourceFile"> full path of the compressed package </param> 53 // <param name = "targetPath"> where is the decompression path </param> 54 // <returns> </returns> 55 public static bool Decompress (string sourceFile, string targetPath) 56 {57 if (! File. Exists (sourceFile) 58 {59 throw new FileNotFoundException (string. Format ("File '{0}'", sourceFile) not found); 60} 61 if (! Directory. exists (targetPath) 62 {63 Directory. createDirectory (targetPath); 64} 65 using (var s = new ZipInputStream (File. openRead (sourceFile) 66 {67 ZipEntry theEntry; 68 while (theEntry = s. getNextEntry ())! = Null) 69 {70 if (theEntry. isDirectory) 71 {72 continue; 73} 74 string directorName = Path. combine (targetPath, Path. getDirectoryName (theEntry. name); 75 string fileName = Path. combine (directorName, Path. getFileName (theEntry. name); 76 if (! Directory. Exists (directorName) 77 {78 Directory. CreateDirectory (directorName); 79} 80 if (! String. isNullOrEmpty (fileName) 81 {82 using (FileStream streamWriter = File. create (fileName) 83 {84 int size = 4096; 85 byte [] data = new byte [size]; 86 while (size> 0) 87 {88 streamWriter. write (data, 0, size); 89 size = s. read (data, 0, data. length); 90} 91} 92} 93} 94} 95 return true; 96} 97} 98}