国产探花免费观看_亚洲丰满少妇自慰呻吟_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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安图县| 加查县| 临潭县| 江安县| 含山县| 区。| 丹棱县| 曲阜市| 拜泉县| 甘德县| 安徽省| 驻马店市| 锡林郭勒盟| 蕲春县| 五原县| 永定县| 乌审旗| 蒙自县| 商丘市| 邵阳县| 临清市| 原平市| 蒲江县| 临朐县| 兴文县| 泌阳县| 呼和浩特市| 海伦市| 吴桥县| 镇远县| 左云县| 通山县| 嘉荫县| 浦城县| 固镇县| 大同县| 漳平市| 雷波县| 翼城县| 长乐市| 开原市|