C # How to compress files and troubleshoot them,
C # The third-party dll library ICSharpCode. SharpZipLib. dll can be used to compress files;
The following code compresses folders and multiple files simultaneously. (For example, compress three folders and five files into one zip file)
Directly add the code,Code from: Http://blog.csdn.net/jk007/article/details/8115825
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. IO; 6 using System. diagnostics; 7 using ICSharpCode. sharpZipLib; 8 using ICSharpCode. sharpZipLib. zip; 9 using ICSharpCode. sharpZipLib. checksums; 10 using ICSharpCode. sharpZipLib. core; 11 12 namespace TestForm13 14 {15 public class ZipHelper16 {17 /// <summary> 18 // ZIP file 19 /// </summary> 20 /// <Param name = "sourceFilePath"> </param> 21 // <param name = "destinationZipFilePath"> </param> 22 public static void CreateZip (string sourceFilePath, string destinationZipFilePath) 23 {24 if (sourceFilePath [sourceFilePath. length-1]! = System. IO. path. directorySeparatorChar) 25 sourceFilePath + = System. IO. path. directorySeparatorChar; 26 ZipOutputStream zipStream = new ZipOutputStream (File. create (destinationZipFilePath); 27 zipStream. setLevel (6); // compression level 0-928 CreateZipFiles (sourceFilePath, zipStream); 29 zipStream. finish (); 30 zipStream. close (); 31} 32 // <summary> 33 // recursive compressed file 34 // </summary> 35 // <param name = "sourceFilePath"> files to be compressed or the folder path </param> 36 // <param name = "zipStream"> zip file path of the packaging result (similar to D: \ WorkSpace \ a.zipfiles, full package name and. Zip extension </param> 37 // <param name = "staticFile"> </param> 38 private static void CreateZipFiles (string sourceFilePath, ZipOutputStream zipStream) 39 {40 Crc32 crc = new Crc32 (); 41 string [] filesArray = Directory. getFileSystemEntries (sourceFilePath); 42 foreach (string file in filesArray) 43 {44 if (Directory. exists (file) // if it is a folder, recursion 45 {46 CreateZipFiles (file, zipStream); 47} 48 else // if it is a file, start compressing 49 {50 FileStream fileStream = File. openRead (file); 51 byte [] buffer = new byte [fileStream. length]; 52 fileStream. read (buffer, 0, buffer. length); 53 string tempFile = file. substring (sourceFilePath. lastIndexOf ("\") + 1); 54 ZipEntry entry = new ZipEntry (tempFile); 55 entry. dateTime = DateTime. now; 56 entry. size = fileStream. length; 57 fileStream. close (); 58 crc. reset (); 59 crc. update (buffer); 60 entry. crc = crc. value; 61 zipStream. putNextEntry (entry); 62 zipStream. write (buffer, 0, buffer. length); 63} 64} 65} 66} 67}
An error may occur during runtime. the breakpoint cannot enter the function in this class, and the fault information is that the Assembly cannot be loaded.
Fault analysis:
1. After downloading the dll file, you cannot directly reference the dll file outside the project and put it in the bin directory of your project.
2. Note that the dll version may be 32-bit or 64-bit. Therefore, you must set the target platform in VS generation. 32 bits correspond to X86 and 64 bits correspond to X64.