Use icsharpcode. sharpziplib. dll in. Net to compress and decompress files.

Source: Internet
Author: User

Add reference icsharpcode. sharpziplib. dll

 

View plaincopy to clipboardprint?
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. IO;
  4. Using system. Threading;
  5. Using icsharpcode. sharpziplib. Zip;
  6. Namespace lib
  7. {
  8. /// <Summary>
  9. /// File compression and decompression
  10. /// </Summary>
  11. Public class filecompression
  12. {
  13. /// <Summary>
  14. /// Constructor
  15. /// </Summary>
  16. Public filecompression ()
  17. {
  18. }
  19. # Region encryption and File compression
  20. /// <Summary>
  21. /// Compressed file
  22. /// </Summary>
  23. /// <Param name = "filenames"> list of files to be packaged </param>
  24. /// <Param name = "gzipfilename"> target file name </param>
  25. /// <Param name = "compressionlevel"> compression quality level (0 ~ 9) </param>
  26. /// <Param name = "sleeptimer"> sleep time (unit: milliseconds) </param>
  27. Public static void compress (list <fileinfo> filenames, string gzipfilename, int compressionlevel, int sleeptimer)
  28. {
  29. Zipoutputstream S = new zipoutputstream (file. Create (gzipfilename ));
  30. Try
  31. {
  32. S. setlevel (compressionlevel); // 0-store only to 9-means best compression
  33. Foreach (fileinfo file in filenames)
  34. {
  35. Filestream FS = NULL;
  36. Try
  37. {
  38. FS = file. Open (filemode. Open, fileaccess. readwrite );
  39. }
  40. Catch
  41. {Continue ;}
  42. // Method 2: Read files into the buffer in batches
  43. Byte [] DATA = new byte [2048];
  44. Int size = 2048;
  45. Zipentry entry = new zipentry (path. getfilename (file. Name ));
  46. Entry. datetime = (file. creationtime> file. lastwritetime? File. lastwritetime: file. creationtime );
  47. S. putnextentry (entry );
  48. While (true)
  49. {
  50. Size = FS. Read (data, 0, size );
  51. If (size <= 0) break;
  52. S. Write (data, 0, size );
  53. }
  54. FS. Close ();
  55. File. Delete ();
  56. Thread. Sleep (sleeptimer );
  57. }
  58. }
  59. Finally
  60. {
  61. S. Finish ();
  62. S. Close ();
  63. }
  64. }
  65. # Endregion
  66. # Region decryption and decompression of Files
  67. /// <Summary>
  68. /// Decompress the file
  69. /// </Summary>
  70. /// <Param name = "gzipfile"> compressed package file name </param>
  71. /// <Param name = "targetpath"> decompress the target path </param>
  72. Public static void decompress (string gzipfile, string targetpath)
  73. {
  74. // String directoryname = path. getdirectoryname (targetpath + "\") + "\\";
  75. String directoryname = targetpath;
  76. If (! Directory. exists (directoryname) directory. createdirectory (directoryname); // generate the decompressed directory
  77. String currentdirectory = directoryname;
  78. Byte [] DATA = new byte [2048];
  79. Int size = 2048;
  80. Zipentry theentry = NULL;
  81. Using (zipinputstream S = new zipinputstream (file. openread (gzipfile )))
  82. {
  83. While (theentry = S. getnextentry ())! = NULL)
  84. {
  85. If (theentry. isdirectory)
  86. {// The node is a directory
  87. If (! Directory. exists (currentdirectory + theentry. Name) directory. createdirectory (currentdirectory + theentry. Name );
  88. }
  89. Else
  90. {
  91. If (theentry. Name! = String. Empty)
  92. {
  93. // Extract the file to the specified directory
  94. Using (filestream streamwriter = file. Create (currentdirectory + theentry. Name ))
  95. {
  96. While (true)
  97. {
  98. Size = S. Read (data, 0, Data. Length );
  99. If (size <= 0) break;
  100. Streamwriter. Write (data, 0, size );
  101. }
  102. Streamwriter. Close ();
  103. }
  104. }
  105. }
  106. }
  107. S. Close ();
  108. }
  109. }
  110. # Endregion
  111. }
  112. }

Foreground call:

/// <Summary>
/// Compression
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void btncompress_click (Object sender, eventargs E)
{
String Path = server. mappath ("~ /Files /");
// Check whether the file exists
If (file. exists (path + "Chapter 2nd using data warehouse "))
{
Fileinfo fi1 = new fileinfo (path + "Chapter 2nd using data warehouse ");
Fileinfo fi2 = new fileinfo (path + "Chapter 3rd use data binding and datasettings ");
Fileinfo fi3 = new fileinfo (path + "Chapter 4th SQL Server xmlfunction ");
Fileinfo fi4 = new fileinfo (path + "Chapter 5th xmlprogramming example ");
List <fileinfo> filelist = new list <fileinfo> ();
Filelist. Add (fi1 );
Filelist. Add (fi2 );
Filelist. Add (fi3 );
Filelist. Add (fi4 );
// Call Method
String targetzipfilepath = server. mappath ("~ /Zipfile/") +" book.zip "; // random Extension
Lib. filecompression. Compress (filelist, targetzipfilepath, 5, 5 );
Response. Write ("the file is compressed successfully. Please check it in the zipfile folder. ");
}
Else
{
Response. Write ("the compressed file does not exist. Extract the file first. ");
}

}

/// <Summary>
/// Extract
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void btndecompress_click (Object sender, eventargs E)
{
String zipfilepath = server. mappath ("~ /Zipfile/") +" book.zip ";//
If (file. exists (zipfilepath ))
{
String targetpath = server. mappath ("~ /Files /");
Lib. filecompression. Decompress (zipfilepath, targetpath );
//// Delete the ZIP file after decompression
// File. Delete (zipfilepath );
Response. Write ("the file is decompressed successfully. Please check it in the Files folder. ");
}
Else
{
Response. Write ("the compressed file does not exist. compress the file first. ");
}
}
}

 

 

 

 

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.