利用ICSharpCode.SharpZipLib来实现的压缩与解压缩类

news/2024/7/24 10:18:37

最近,在项目中经常需要处理压缩和解压缩文件的操作。在网上找到了相关资料,自己整理了下,写了两个类:一个压缩类;一个解压缩类。

当然是利用了ICSharpCode.SharpZipLib提供的方法来对文件压缩和解压缩。

ContractedBlock.gif ExpandedBlockStart.gif 压缩类
/// <summary>
    
/// 压缩类
    
/// </summary>
    class ZipClass
    {
        
private int compressionLevel = 9;
        
private byte[] buffer = new byte[2048];

        
/// <summary>
        
/// 默认构造函数
        
/// </summary>
        public ZipClass()
        {
        }
        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="bufferSize">缓冲区大小</param>
        
/// <param name="compressionLevel">压缩率:0-9</param>
        public ZipClass(int bufferSize, int compressionLevel)
        {
            buffer 
= new byte[bufferSize];
            
this.compressionLevel = compressionLevel;
        }

        
/// <summary>
        
/// 压缩文件
        
/// </summary>
        
/// <param name="fileToZip">要压缩的文件路径</param>
        
/// <param name="zipedFile">压缩后的文件路径</param>
        public void ZipFile(string fileToZip, string zipedFile)
        {
            
if (!File.Exists(fileToZip))
            {
                
throw new FileNotFoundException("The specified file " + fileToZip + " could not be found.");
            }

            
using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile)))
            {
                
string fileName = Path.GetFileName(fileToZip);
                ZipEntry zipEntry 
= new ZipEntry(fileName);
                zipStream.PutNextEntry(zipEntry);
                zipStream.SetLevel(compressionLevel);

                
using (FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
                {
                    
int size = streamToZip.Read(buffer, 0, buffer.Length);
                    zipStream.Write(buffer, 
0, size);

                    
while (size < streamToZip.Length)
                    {
                        
int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                        zipStream.Write(buffer, 
0, sizeRead);
                        size 
+= sizeRead;
                    }
                }
            }
        }

        
/// <summary>
        
/// 得到文件下的所有文件
        
/// </summary>
        
/// <param name="directory">文件夹路径</param>
        
/// <returns></returns>
        public ArrayList GetFileList(string directory)
        {
            ArrayList fileList 
= new ArrayList();
            
bool isEmpty = true;
            
foreach (string file in Directory.GetFiles(directory))
            {
                fileList.Add(file);
                isEmpty 
= false;
            }
            
if (isEmpty)
            {
                
if (Directory.GetDirectories(directory).Length == 0)
                {
                    fileList.Add(directory 
+ @"/");
                }
            }
            
foreach (string dirs in Directory.GetDirectories(directory))
            {
                
foreach (object obj in GetFileList(dirs))
                {
                    fileList.Add(obj);
                }
            }
            
return fileList;
        }

        
/// <summary>
        
/// 压缩文件夹
        
/// </summary>
        
/// <param name="directoryToZip">要压缩的文件夹路径</param>
        
/// <param name="zipedDirectory">压缩或的文件夹路径</param>
        public void ZipDerctory(string directoryToZip, string zipedDirectory)
        {
            
using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedDirectory)))
            {
                ArrayList fileList 
= GetFileList(directoryToZip);
                
int directoryNameLength = (Directory.GetParent(directoryToZip)).ToString().Length;

                zipStream.SetLevel(compressionLevel);
                ZipEntry zipEntry 
= null;
                FileStream fileStream 
= null;

                
foreach (string fileName in fileList)
                {
                    zipEntry 
= new ZipEntry(fileName.Remove(0, directoryNameLength));
                    zipStream.PutNextEntry(zipEntry);

                    
if (!fileName.EndsWith(@"/"))
                    {
                        fileStream 
= File.OpenRead(fileName);
                        fileStream.Read(buffer, 
0, buffer.Length);
                        zipStream.Write(buffer, 
0, buffer.Length);
                    }
                }
            }
        }
    }

 

ContractedBlock.gif ExpandedBlockStart.gif 解压缩类
/// <summary>
    
/// 解压缩类
    
/// </summary>
    class UnZipClass
    {
        
private byte[] buffer = new byte[2048];

        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="bufferSize">缓冲区大小</param>
        public UnZipClass(int bufferSize)
        {
            buffer 
= new byte[bufferSize];
        }

        
/// <summary>
        
/// 默认构造函数
        
/// </summary>
        public UnZipClass()
        {
        }

        
/// <summary>
        
/// 解压缩文件
        
/// </summary>
        
/// <param name="zipFilePath">压缩文件路径</param>
        
/// <param name="unZipFilePatah">解压缩文件路径</param>
        public void UnZipFile(string zipFilePath, string unZipFilePatah)
        {
            
using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                ZipEntry zipEntry 
= null;
                
while ((zipEntry=zipStream.GetNextEntry()) != null)
                {
                    
string fileName = Path.GetFileName(zipEntry.Name);
                    
if (!string.IsNullOrEmpty(fileName))
                    {
                        
if (zipEntry.CompressedSize == 0)
                            
break;
                        
using (FileStream stream = File.Create(unZipFilePatah + fileName))
                        {
                            
while (true)
                            {
                                
int size = zipStream.Read(buffer, 0, buffer.Length);
                                
if (size > 0)
                                {
                                    stream.Write(buffer, 
0, size);
                                }
                                
else
                                {
                                    
break;
                                }
                            }
                        }
                    }
                }
            }
        }

        
/// <summary>
        
/// 解压缩目录
        
/// </summary>
        
/// <param name="zipDirectoryPath">压缩目录路径</param>
        
/// <param name="unZipDirecotyPath">解压缩目录路径</param>
        public void UnZipDirectory(string zipDirectoryPath, string unZipDirecotyPath)
        {
            
using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipDirectoryPath)))
            {
                ZipEntry zipEntry 
= null;
                
while ((zipEntry = zipStream.GetNextEntry())!= null)
                {
                    
string directoryName = Path.GetDirectoryName(zipEntry.Name);
                    
string fileName = Path.GetFileName(zipEntry.Name);

                    
if (!string.IsNullOrEmpty(directoryName))
                    {
                        Directory.CreateDirectory(directoryName);
                    }

                    
if (!string.IsNullOrEmpty(fileName))
                    {
                        
if (zipEntry.CompressedSize == 0)
                            
break;
                        
if (zipEntry.IsDirectory)
                        {
                            directoryName 
= Path.GetDirectoryName(unZipDirecotyPath + zipEntry.Name);
                            Directory.CreateDirectory(directoryName);
                        }

                        
using (FileStream stream = File.Create(unZipDirecotyPath + zipEntry.Name))
                        {
                            
while (true)
                            {
                                
int size = zipStream.Read(buffer, 0, buffer.Length);
                                
if (size > 0)
                                {
                                    stream.Write(buffer, 
0, size);
                                }
                                
else
                                {
                                    
break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

 

 

转载于:https://www.cnblogs.com/ssqjd/archive/2009/02/16/1391278.html


http://www.niftyadmin.cn/n/1864459.html

相关文章

如何使用cURL获得请求和响应时间?

✎ 码甲说 hello&#xff0c;老伙计们&#xff0c;又有半个多月没见了&#xff0c;今天给大家分享一个干货编程小技巧&#xff0c;上至架构师、下至开发者、运维男、QA&#xff0c; 得此利器&#xff0c;事半功倍。cURL在我的眼里&#xff0c;就是一个httpClient手办&#xff…

Membership、MembershipUser和Roles类

Membership、MembershipUser和Roles类用户与角色管理在asp.net2.0中是通过Membership和Roles两个类来实现的。Membership&#xff1a;用户成员账号管理&#xff0c;用户名、密码、邮箱等Roles&#xff1a;负责用户和群组之间关系管理。l Membership类&#xff1a;主要是…

程序员的十层楼(6~7层)

作者&#xff1a; 周伟明 (14 篇文章) 日期&#xff1a; 二月 4, 2009 在 2:58 下午 第1&#xff5e;3层 看这里&#xff1a;http://software.intel.com/zh-cn/blogs/2009/02/04/1071/ 第4&#xff5e;5层 看这里&#xff1a;http://software.intel.com/zh-cn/blogs/2009/02/0…

童鞋,[HttpClient发送文件的技术实践]请查收

昨天有童鞋在群里面问&#xff1a;怎么使用HttpClient发送文件&#xff1f;01荒腔走板之前我写了一个《ABP小试牛刀之上传文件》&#xff0c;主要体现的是服务端&#xff0c;上传文件的动作是由前端小姐姐完成的&#xff0c; 我还真没有用HttpClient编程方式发送过文件。不过Ht…

浅谈MemoryCache的原生插值方式

.NET运行时内置了常用的缓存模块&#xff1a;MemoryCache标准的MemoryCache暴露了如下几个属性和方法&#xff1a;public int Count { get; } public void Compact(double percentage); public ICacheEntry CreateEntry(object key); public void Dispose(); public void Remov…

sql得到时间

sql得到当前系统时间得 日期部分 CONVERT(varchar(10),getDate(),120) 昨天 select convert(varchar(10),getdate() - 1,120) 明天 select convert(varchar(10),getdate() 1,120) 最近七天 select * from tb where 时间字段 > convert(varchar(10),getdate() - 7,120) 随后…

[导入]从架构设计到系统实施——基于.NET 3.0的全新企业应用系列课程(5):设计基于WPF的客户端.zip(6.98 MB)...

讲座内容&#xff1a; 本次系列课程将采用案例教学的方法&#xff0c;深度剖析微软的基于.NET 3.0的分布式应用设计方法和实施过程。在第五次课程中&#xff0c;我们将详细介绍如何设计基于.NET 3.0 WPF的全新的企业应用的具有相当好的客户体验的客户端。 课程讲师&#xff1a;…

一条nginx命令引发的对于容器的思考

去年的时候写了一篇原创《前后端分离&#xff0c;如何在前端项目中动态插入后端API基地址&#xff1f;&#xff08;in docker&#xff09;》&#xff0c; 我自认为这篇生产实践是对大前端、 容器化、CI/CD的得意之作。对于前后端分离的web项目&#xff0c;在容器启动的瞬间&…