C#實(shí)現(xiàn)Zip壓縮目錄中所有文件的方法
更新時(shí)間:2015年07月15日 16:13:00 作者:DTC2
這篇文章主要介紹了C#實(shí)現(xiàn)Zip壓縮目錄中所有文件的方法,涉及C#針對文件的讀寫與zip壓縮相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#實(shí)現(xiàn)Zip壓縮目錄中所有文件的方法。分享給大家供大家參考。具體實(shí)現(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è)計(jì)有所幫助。
您可能感興趣的文章:
- C#文件流進(jìn)行壓縮和解壓縮的方法
- C#實(shí)現(xiàn)rar壓縮與解壓縮文件的方法
- asp.net C#實(shí)現(xiàn)解壓縮文件的方法
- C#使用GZipStream解壓縮數(shù)據(jù)文件的方法
- C#實(shí)現(xiàn)的文件壓縮和解壓縮類
- c#調(diào)用winrar解壓縮文件代碼分享
- C#中使用WinRAR實(shí)現(xiàn)加密壓縮及解壓縮文件
- C#使用iCSharpcode進(jìn)行文件壓縮實(shí)現(xiàn)方法
- Windows系統(tǒng)中C#調(diào)用WinRAR來壓縮和解壓縮文件的方法
- C#使用DeflateStream解壓縮數(shù)據(jù)文件的方法
- c#實(shí)現(xiàn)metro文件壓縮解壓示例
- C#實(shí)現(xiàn)文件壓縮與解壓的方法示例【ZIP格式】
相關(guān)文章
c#判斷字符是否為中文的三種方法分享(正則表達(dá)式判斷)
判斷一個(gè)字符是不是漢字通常有三種方法,第一種用 ASCII 碼判斷,第二種用漢字的UNICODE編碼范圍判斷,第三種用正則表達(dá)式判斷,以下是具體方法2014-01-01
C#實(shí)現(xiàn)根據(jù)給出的相對地址獲取網(wǎng)站絕對地址的方法
這篇文章主要介紹了C#實(shí)現(xiàn)根據(jù)給出的相對地址獲取網(wǎng)站絕對地址的方法,涉及C#URL及字符串操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
C# 實(shí)現(xiàn)在當(dāng)前目錄基礎(chǔ)上找到上一層目錄
這篇文章主要介紹了C# 實(shí)現(xiàn)在當(dāng)前目錄基礎(chǔ)上找到上一層目錄,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
C#程序中session值的保存方法以及轉(zhuǎn)為字符串的方法總結(jié)
這篇文章主要介紹了C#程序中session值的保存方法以及轉(zhuǎn)為字符串的方法總結(jié),經(jīng)常被用于ASP.NET網(wǎng)絡(luò)編程項(xiàng)目中,需要的朋友可以參考下2016-04-04

