C#实现Zip压缩目录中所有文件的方法
作者:DTC2 发布时间:2021-05-29 15:41:47
标签: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#程序设计有所帮助。


猜你喜欢
- 本篇分析ArrayList的源码,在分析之前先跟大家谈一谈数组。数组可能是我们最早接触到的数据结构之一,它是在内存中划分出一块连续的地址空间
- @Value注解读取yml中的map配置网上查了好多资料,都是.properties文件中读取,而且又是几个人抄来抄去,找了半天功夫不负有心
- 本文实例总结了C#常见应用函数。分享给大家供大家参考,具体如下:1、页面写CS代码(代码内嵌)<%@ Import Namespace
- 本文实现android系统照相机的调用来拍照项目的布局相当简单,只有一个Button:<RelativeLayout xmlns:an
- BigDecimal类对于不需要任何准确计算精度的数字可以直接使用float或double,但是如果需要精确计算的结果,则必须使用BigDe
- 背景介绍在一些需求中,可能存在某些场景,比如先加载自己的bean,然后自己的bean做一些DB操作,初始化配置问题,然后后面的bean基于这
- 1.CAS1)CAS概念CAS时Compare And Swap缩写,即比较与交换是用于实现多线程同步的原子指令,它将内存位置的内容与给定值
- 引言上一节讲面试中被问到分布式系统概念相关的,讲完了分布式系统的概念,优点缺点和 RPC 后,我以为这个问题就到此结束了,没想到成功给自己挖
- 本文实例总结了C#遍历DataSet控件的方法。分享给大家供大家参考。具体方法如下:DataSet控件在.net主要是用来存储数据的,它更像
- 1、文件上传1.1 后端部分1.1.1 引入Apache Commons FIleUpload组件依赖<!--文件上传与下载相关的依赖
- 本文实例为大家分享了使用C#写一个时钟,供大家参考,具体内容如下时钟是这样的一共使用四个控件即可:WinFrom窗体应用程序代码:using
- 1.Mybatis概述 MyBatis 是一款
- Android虚拟键盘的弹起会遮挡住部分ui,虽然通过在清单文件中设置,可以随着虚拟键盘的弹出,布局往上推,但是面对登陆界面时,并没有太大的
- 目录一. 先简单总结一下比较常见的几个解决方案的弊端:1. IMEI2. Android ID3. MAC地址二. uuid + 本地文件,
- 通过Canvas的平移与旋转简化绘图逻辑是一个非常有用的技巧,下面的时钟view就是利用这个方法完成的,省去了使用三角函数计算坐标的麻烦。p
- 一、问题描述平时我们在完成某些数据的入库后,发布了一个事件,此时使用的是@EventListener,然后在这个事件中,又去对刚才入库的数据
- C#操作注册表全攻略相信每个人对注册表并不陌生,在运行里面输入“regedit”就可以打开注册表编辑器了。这东西对Windows系统来说可是
- 我们有很多 Coding Style 或 代码规范。 但这一条可能会经常被我们所遗忘,就是我们 经常会在函数的参数里使用bool参数,这会大
- ActivityThread功能它管理应用进程的主线程的执行(相当于普通Java程序的main入口函数),并根据AMS的要求(通过IAppl
- 前言我通常是不太关心代码的具体实现的,因为我的开发语言很杂,倾向于一些最简单通用的方式去解决。今儿不小心在群里看到一位朋友发了下面的java