C#实现Zip压缩目录中所有文件的方法

 更新时间:2015年07月15日 16:13:00   作者:DTC2  
这篇文章主要介绍了C#实现Zip压缩目录中所有文件的方法,涉及C#针对文件的读写与zip压缩相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#实现Zip压缩目录中所有文件的方法。分享给大家供大家参考。具体实现方法如下:

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#程序设计有所帮助。

相关文章

  • winform 使用Anchor属性进行界面布局的方法详解

    winform 使用Anchor属性进行界面布局的方法详解

    这篇文章主要介绍了winform 使用Anchor属性进行界面布局的方法,有需要的朋友可以参考一下
    2013-12-12
  • C#实现语音播报功能的示例详解

    C#实现语音播报功能的示例详解

    这篇文章主要为大家详细介绍了如何使用C#实现语音播报功能,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下
    2024-02-02
  • C#中的Linq To XML讲解

    C#中的Linq To XML讲解

    本文详细讲解了C#中的Linq To XML,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • c# 使用OpenCV识别硬币

    c# 使用OpenCV识别硬币

    这篇文章主要介绍了c# 使用OpenCV识别硬币的方法,帮助大家更好的利用c#进行深度学习,感兴趣的朋友可以了解下
    2020-12-12
  • C# 操作符之三元操作符浅析

    C# 操作符之三元操作符浅析

    C# 操作符之三元操作符“?:”是如何使用的呢?C# 操作符之三元操作符“?:”需要注意的是什么呢?那么本文就向你简单介绍C# 操作符之三元操作符“?:”的基本情况。
    2011-02-02
  • C#全局热键设置与窗体热键设置实例

    C#全局热键设置与窗体热键设置实例

    这篇文章主要介绍了C#全局热键设置与窗体热键设置实例,对C#全局热键设置与窗体热键设置的实现方法与具体代码进行了详细的介绍,需要的朋友可以参考下
    2014-10-10
  • Unity3D实现批量下载图片功能

    Unity3D实现批量下载图片功能

    这篇文章主要为大家详细介绍了Unity3D实现批量下载图片功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • c#数据库与TXT导入导出的实例

    c#数据库与TXT导入导出的实例

    最近刚学完ADO.NET,做了个数据导入导出的题目,是将txt中的数据导入数据库,然后将数据库中的数据导出到txt中,这里说的数据的格式是“tom|23”,tom指名字,23指年龄。废话也不多说了,大家直接看代码。
    2013-04-04
  • C#调用带结构体指针Dll的方法

    C#调用带结构体指针Dll的方法

    在C#到底该如何安全的调用这样的DLL接口函数呢?本文将详细介绍如何调用各种参数的方法,对C#结构体指针DLL相关知识感兴趣的朋友一起看看吧
    2021-07-07
  • C# 泛型集合类List<T>使用总结

    C# 泛型集合类List<T>使用总结

    本文主要主要介绍了C# 泛型集合类List<T>使用总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧<BR>
    2022-05-05

最新评论