麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > C# > 正文

C#實(shí)現(xiàn)Zip壓縮目錄中所有文件的方法

2019-10-29 21:41:02
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

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

 

 
  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(); 

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 露脸各种姿势啪啪的清纯美女 | 国产亚洲精品久久久久婷婷瑜伽 | 亚洲xxx在线观看 | 99欧美精品| 7777在线观看 | 欧美巨乳在线观看 | 色综合精品 | 欧美一级理论 | 一级做a爱视频 | 黄色99视频| www.17c亚洲蜜桃| 农村少妇吞精夜夜爽视频 | 日韩欧美高清片 | 亚洲无马在线观看 | 福利在线国产 | 久久久久久久免费看 | av电影院在线观看 | 久久精品a一级国产免视看成人 | 欧美老外a级毛片 | 精品欧美一区二区精品久久 | 久久久一区二区三区四区 | 免费毛片电影 | 成人国产精品一区 | 国产精品欧美久久久久一区二区 | 一本色道久久综合亚洲精品小说 | 91精品国产91久久久久久蜜臀 | av在线免费观看不卡 | 性高跟鞋xxxxhd4kvideos | 成人免费网站在线观看视频 | 黑色丝袜美美女被躁视频 | 一级毛片在线视频 | 国产免费久久久久 | 男女一边摸一边做羞羞视频免费 | av免费不卡国产观看 | 成人免费在线网 | 欧美视频一区二区 | 黄网免费看 | 日韩视频在线免费 | 99re热视频这里只精品 | 特一级毛片 | 在线中文字幕亚洲 |