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

相关文章

  • C#接口归纳总结实例详解

    C#接口归纳总结实例详解

    本篇文章通过实例代码对接口做了详解,需要的朋友可以参考下
    2017-04-04
  • C#和Java有什么区别和联系

    C#和Java有什么区别和联系

    这篇文章主要介绍了C#和Java有什么区别和联系的相关资料,本文介绍的非常详细,涉及到rsa语法,c#和java互转方面的知识点,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-07-07
  • C#关键字之覆写overwrite介绍

    C#关键字之覆写overwrite介绍

    这篇文章介绍了C#关键字之覆写overwrite,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

    C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

    本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepSeek R1模型,客户端 ChatBox AI 接入 DeepSeek 的步骤,以及 C# 调用 DeepSeek API 的示例代码,并总结了其在实际项目中的应用价值,需要的朋友可以参考下
    2025-03-03
  • C# Base64编码函数

    C# Base64编码函数

    Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码。它将需要编码的数据拆分成字节数组。
    2009-06-06
  • C#使用SQL Dataset数据集代码实例

    C#使用SQL Dataset数据集代码实例

    今天小编就为大家分享一篇关于的文章,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • C#实现获取电脑硬件显卡核心代号信息

    C#实现获取电脑硬件显卡核心代号信息

    这篇文章主要为大家详细介绍了如何利用C#实现获取电脑硬件显卡核心代号信息,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • C#如何通过RFC连接sap系统

    C#如何通过RFC连接sap系统

    这篇文章主要为大家详细介绍了C#如何通过RFC连接sap系统的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • C# 根据表格偶数、奇数加载不同颜色

    C# 根据表格偶数、奇数加载不同颜色

    这篇文章主要介绍了C# 根据表格偶数、奇数加载不同颜色,需要的朋友可以参考下
    2017-09-09
  • C#调用Rar文件及获取Rar返回值的方法

    C#调用Rar文件及获取Rar返回值的方法

    这篇文章主要介绍了C#调用Rar文件及获取Rar返回值的方法,实例分析了C#调用rar文件实现文件的压缩与解压相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07

最新评论