a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > 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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 激情com | 一区二区免费在线观看 | 人人草人人| 欧美日韩亚洲国产 | 中文字幕免费在线观看 | 福利久久 | 91精品一区二区 | 天天天干天天射天天天操 | 亚洲成人一区二区 | 欧美视频精品 | 羞羞视频在线观看视频 | 国产精品日韩欧美一区二区三区 | 综合久久综合久久 | 日本天堂在线观看 | 国产精品久久久久久久久久久久冷 | 国产成人久久精品麻豆二区 | 成人欧美 | 亚洲一区二区在线 | av中文字幕在线观看 | 国产免费看黄网站 | 国产精品日产欧美久久久久 | 超碰高清 | 性色视频在线观看 | 九九九九精品 | 夜夜夜久久久 | 久久综合久久久 | 美欧一级片 | 在线免费精品 | 午夜国产精品视频 | 国产99精品| 男女色啪网| 亚洲一区二区三区四区五区中文 | 中文字幕亚洲一区二区三区 | 国产欧美综合一区二区三区 | 成人精品一二三区 | 一区二区成人在线 | 在线播放日本 | 日本久久久久 | 国产色网 | 日韩欧美二区 | 久久久国产一区二区三区 |