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

首頁 > 編程 > C# > 正文

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

2020-01-24 01:37:34
字體:
供稿:網(wǎng)友

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

using System;using System.IO;using System.Collections;using System.IO.Compression;using System.Collections.Generic;class FolderZip{private const long BUFFER_SIZE = 20480;static void main(string[] args){string sourcepath=@"C:/tmp";Queue<FileSystemInfo> Folders = new Queue<FileSystemInfo>(new DirectoryInfo(sourcepath).GetFileSystemInfos());string copytopath = @"D:/temp";copytopath = (copytopath.LastIndexOf(Path.DirectorySeparatorChar) == copytopath.Length - 1) ? copytopath : copytopath+Path.DirectorySeparatorChar + Path.GetFileName(sourcepath);Directory.CreateDirectory(copytopath);while (Folders.Count > 0){ FileSystemInfo atom = Folders.Dequeue(); FileInfo sourcefile = atom as FileInfo; if (sourcefile == null) {  DirectoryInfo directory = atom as DirectoryInfo;  Directory.CreateDirectory(directory.FullName.Replace(sourcepath,copytopath));  foreach (FileSystemInfo nextatom in directory.GetFileSystemInfos())  Folders.Enqueue(nextatom); } else {  string sourcefilename = sourcefile.FullName;  string zipfilename = sourcefile.FullName.Replace(sourcepath,copytopath) + ".zip";  if (!File.Exists(zipfilename))  {   FileStream sourceStream = null;   FileStream destinationStream = null;   GZipStream compressedStream = null;   try   {    // Read the bytes from the source file into a byte array    sourceStream = new FileStream(sourcefilename, FileMode.Open, FileAccess.Read, FileShare.Read);    // Open the FileStream to write to    destinationStream = new FileStream(zipfilename, FileMode.OpenOrCreate, FileAccess.Write);    // Create a compression stream pointing to the destiantion stream    compressedStream = new DeflateStream(destinationStream, CompressionMode.Compress, true);    long bufferSize = sourceStream.Length < BUFFER_SIZE ? sourceStream.Length : BUFFER_SIZE;    byte[] buffer = new byte[bufferSize];    int bytesRead = 0;    long bytesWritten = 0;    while ((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) != 0)    {     compressedStream.Write(buffer, 0, bytesRead);     bytesWritten += bufferSize;    }   }   catch (ApplicationException)   {    continue;   }   finally   {    // Make sure we allways close all streams    if (sourceStream != null) sourceStream.Close();    if (compressedStream != null) compressedStream.Close();    if (destinationStream != null) destinationStream.Close();   }  } }}}}

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宾阳县| 汉中市| 沛县| 马龙县| 上栗县| 宾川县| 嵊泗县| 长阳| 陆河县| 昭苏县| 马公市| 江口县| 同江市| 胶州市| 兰州市| 江孜县| 江门市| 黎城县| 吉木萨尔县| 平谷区| 平远县| 罗定市| 霍林郭勒市| 建德市| 宜州市| 阳泉市| 句容市| 保康县| 八宿县| 洪雅县| 高密市| 佛冈县| 诏安县| 清水河县| 凌源市| 乌苏市| 宁陕县| 纳雍县| 南部县| 湛江市| 吉林市|