C#实现Zip压缩目录中所有文件的方法
作者:DTC2 时间:2021-05-29 15:41:47
本文实例讲述了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#,Zip
0
投稿
猜你喜欢
Java基础学习之构造方法详解
2021-06-16 16:25:53
防止未登录用户操作—基于struts2拦截器的简单实现
2021-06-11 13:21:00
springboot @Valid注解对嵌套类型的校验功能
2023-07-01 13:02:18
springcloud Zuul动态路由的实现
2021-10-07 06:15:40
C# 图片与Base64码的相互转化问题(代码详解)
2021-11-30 22:56:16
SpringBoot RestTemplate 简单包装解析
2023-02-01 17:02:55
springboot常用注释的讲解
2023-11-03 02:53:15
Java并发编程之ReadWriteLock读写锁的操作方法
2023-12-07 20:08:37
android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)
2023-07-27 19:59:38
Android自定义View实现竖直跑马灯效果案例解析
2021-06-25 04:04:30
实例分析java中重载与重写的区别
2021-09-11 01:49:38
老生常谈反射之Class类的使用(必看篇)
2022-07-20 16:32:30
Java超详细透彻讲解static
2021-10-13 12:18:34
Java数组的基本学习教程
2021-07-14 05:08:00
java写卷积神经网络(CupCnn简介)
2022-12-11 09:33:09
Intellij IDEA根据maven依赖名查找它是哪个pom.xml引入的(图文详解)
2023-07-20 07:49:35
springMVC的生命周期详解
2022-10-29 22:27:40
java 实现定时的方法及实例代码
2023-03-31 22:48:41
SpringBoot如何使用ApplicationContext获取bean对象
2023-06-28 20:36:55
Java 线程池原理深入分析
2023-01-30 19:59:43