[C#]文件夹压缩到zip以及解压

需要用到SharpLibZip。代码如下:

ExpandedBlockStart.gif Zip.cs
     public   class  Zip
    {
        
///   <summary>
        
///  压缩文件夹
        
///   </summary>
        
///   <param name="dirPath"> 压缩文件夹的路径 </param>
        
///   <param name="fileName"> 生成的zip文件路径 </param>
        
///   <param name="level"> 压缩级别 0 - 9 0是存储级别 9是最大压缩 </param>
        
///   <param name="bufferSize"> 读取文件的缓冲区大小 </param>
         public   void  CompressDirectory( string  dirPath, string  fileName, int  level, int  bufferSize)
        {
            
byte [] buffer  =   new   byte [bufferSize];
            
using  (ZipOutputStream s  =   new  ZipOutputStream(File.Create(fileName)))
            {
                s.SetLevel(level);
                CompressDirectory(dirPath, dirPath, s, buffer);
                s.Finish();
                s.Close();
            }
        }

        
///   <summary>
        
///  压缩文件夹
        
///   </summary>
        
///   <param name="root"> 压缩文件夹路径 </param>
        
///   <param name="path"> 压缩文件夹内当前要压缩的文件夹路径 </param>
        
///   <param name="s"></param>
        
///   <param name="buffer"> 读取文件的缓冲区大小 </param>
         private   void  CompressDirectory( string  root,  string  path, ZipOutputStream s,  byte [] buffer)
        {
            root 
=  root.TrimEnd( ' \\ ' +   " \\ " ;
            
string [] fileNames  =  Directory.GetFiles(path);
            
string [] dirNames  =  Directory.GetDirectories(path);
            
string  relativePath  =  path.Replace(root,  "" );
            
if  (relativePath  !=   "" )
            {
                relativePath 
=  relativePath.Replace( " \\ " " / " +   " / " ;
            }
            
int  sourceBytes;
            
foreach  ( string  file  in  fileNames)
            {

                ZipEntry entry 
=   new  ZipEntry(relativePath  +  Path.GetFileName(file));
                entry.DateTime 
=  DateTime.Now;
                s.PutNextEntry(entry);
                
using  (FileStream fs  =  File.OpenRead(file))
                {
                    
do
                    {
                        sourceBytes 
=  fs.Read(buffer,  0 , buffer.Length);
                        s.Write(buffer, 
0 , sourceBytes);
                    } 
while  (sourceBytes  >   0 );
                }
            }

            
foreach  ( string  dirName  in  dirNames)
            {
                
string  relativeDirPath  =  dirName.Replace(root,  "" );
                ZipEntry entry 
=   new  ZipEntry(relativeDirPath.Replace( " \\ " " / " +   " / " );
                s.PutNextEntry(entry);
                CompressDirectory(root, dirName, s, buffer);
            }
        }

        
///   <summary>
        
///  解压缩zip文件
        
///   </summary>
        
///   <param name="zipFilePath"> 解压的zip文件路径 </param>
        
///   <param name="extractPath"> 解压到的文件夹路径 </param>
        
///   <param name="bufferSize"> 读取文件的缓冲区大小 </param>
         public   void  Extract( string  zipFilePath,  string  extractPath,  int  bufferSize)
        {
            extractPath 
=  extractPath.TrimEnd( ' \\ ' +   " \\ " ;
            
byte [] data  =   new   byte [bufferSize];
            
int  size;
            
using  (ZipInputStream s  =   new  ZipInputStream(File.OpenRead(zipFilePath)))
            {
                ZipEntry entry;
                
while  ((entry  =  s.GetNextEntry())  !=   null )
                {
                    
string  directoryName  =  Path.GetDirectoryName(entry.Name);
                    
string  fileName  =  Path.GetFileName(entry.Name);

                    
// 先创建目录
                     if  (directoryName.Length  >   0 )
                    {
                        Directory.CreateDirectory(extractPath 
+  directoryName);
                    }

                    
if  (fileName  !=  String.Empty)
                    {
                        
using  (FileStream streamWriter  =  File.Create(extractPath  +  entry.Name.Replace( " / " " \\ " )))
                        {
                            
while  ( true )
                            {
                                size 
=  s.Read(data,  0 , data.Length);
                                
if  (size  >   0 )
                                {
                                    streamWriter.Write(data, 
0 , size);
                                }
                                
else
                                {
                                    
break ;
                                }
                            }
                        }
                    }
                }
            }
        }

    }

 

网上找了很久 包括老外的site也找了 没找到现成的 自己写了一个

使用方法:

ExpandedBlockStart.gif 使用方法
Zip z  =   new  Zip();

z.Extract(
" c:\\cc.zip " , " c:\\haha " 4096 ); z.Extract( " cc.zip " , " c:\\haha " 4096 ); z.Extract( " c:\\cc.zip " , " haha " 4096 );

// 上面的解压缩没啥大问题

z.CompressDirectory(
" E:\\DotNet_Library\\SharpZipLib\\SharpZipLib_0860_SourceSamples " " c:\\aa.zip " 9 4096 );

这个代码有一点点小问题 没有异常处理 压缩过程中 有文件被编辑或者被删除 那就无法压缩了

如果这样那也会异常:

z.CompressDirectory("C:\\CC", "C:\\CC\\1.zip", 9, 4096);

[源代码下载] 

转载于:https://www.cnblogs.com/yuyingjian/archive/2010/12/14/zip_compress_extract_csharp.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值