国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現Zip壓縮目錄中所有文件的方法

2019-10-29 21:41:02
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了C#實現Zip壓縮目錄中所有文件的方法,涉及C#針對文件的讀寫與zip壓縮相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#實現Zip壓縮目錄中所有文件的方法。分享給大家供大家參考。具體實現方法如下:

 

 
  1. using System; 
  2. using System.IO; 
  3. using System.Collections; 
  4. using System.IO.Compression; 
  5. using System.Collections.Generic; 
  6. class FolderZip 
  7. private const long BUFFER_SIZE = 20480; 
  8. static void main(string[] args) 
  9. string sourcepath=@"C:/tmp"
  10. Queue<FileSystemInfo> Folders = new Queue<FileSystemInfo>(new DirectoryInfo(sourcepath).GetFileSystemInfos()); 
  11. string copytopath = @"D:/temp"
  12. copytopath = (copytopath.LastIndexOf(Path.DirectorySeparatorChar) == copytopath.Length - 1) ? copytopath : copytopath+Path.DirectorySeparatorChar + Path.GetFileName(sourcepath); 
  13. Directory.CreateDirectory(copytopath); 
  14. while (Folders.Count > 0) 
  15. FileSystemInfo atom = Folders.Dequeue(); 
  16. FileInfo sourcefile = atom as FileInfo; 
  17. if (sourcefile == null
  18. DirectoryInfo directory = atom as DirectoryInfo; 
  19. Directory.CreateDirectory(directory.FullName.Replace(sourcepath,copytopath)); 
  20. foreach (FileSystemInfo nextatom in directory.GetFileSystemInfos()) 
  21. Folders.Enqueue(nextatom); 
  22. else 
  23. string sourcefilename = sourcefile.FullName; 
  24. string zipfilename = sourcefile.FullName.Replace(sourcepath,copytopath) + ".zip"
  25. if (!File.Exists(zipfilename)) 
  26. FileStream sourceStream = null
  27. FileStream destinationStream = null
  28. GZipStream compressedStream = null
  29. try 
  30. // Read the bytes from the source file into a byte array 
  31. sourceStream = new FileStream(sourcefilename, FileMode.Open, FileAccess.Read, FileShare.Read); 
  32. // Open the FileStream to write to 
  33. destinationStream = new FileStream(zipfilename, FileMode.OpenOrCreate, FileAccess.Write); 
  34. // Create a compression stream pointing to the destiantion stream 
  35. compressedStream = new DeflateStream(destinationStream, CompressionMode.Compress, true); 
  36. long bufferSize = sourceStream.Length < BUFFER_SIZE ? sourceStream.Length : BUFFER_SIZE; 
  37. byte[] buffer = new byte[bufferSize]; 
  38. int bytesRead = 0; 
  39. long bytesWritten = 0; 
  40. while ((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) != 0) 
  41. compressedStream.Write(buffer, 0, bytesRead); 
  42. bytesWritten += bufferSize; 
  43. catch (ApplicationException) 
  44. continue
  45. finally 
  46. // Make sure we allways close all streams 
  47. if (sourceStream != null) sourceStream.Close(); 
  48. if (compressedStream != null) compressedStream.Close(); 
  49. if (destinationStream != null) destinationStream.Close(); 

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 淮滨县| 桃园县| 昌江| 甘南县| 大同县| 泸定县| 炎陵县| 凤山县| 隆回县| 南靖县| 湟源县| 商都县| 汤原县| 米易县| 宜宾县| 巴马| 龙州县| 衡水市| 尤溪县| 余姚市| 平顶山市| 华池县| 孟连| 蒙阴县| 台前县| 定陶县| 彝良县| 丹寨县| 北宁市| 尤溪县| 莱西市| 南乐县| 梁河县| 屏南县| 贺州市| 甘肃省| 侯马市| 巴中市| 梁山县| 平南县| 莱阳市|