利用ICSharpCode压缩打包文件

本文介绍使用ICSharpCode.SharpZipLib库进行文件和目录的压缩打包方法,并提供了一个简单的示例代码,演示如何实现多文件及目录的压缩。

因为项目需要打包文件,就在同事的建议下用ICSharpCode写了个打包函数.ICSharpCode的介绍就不说了.具体请到官方网站 http://www.icsharpcode.net/ 上了解.

 

首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/KenBlove/ICSharpCode.SharpZipLib.rar

 

代码实现多文件,自定义文件,整目录打包等功能.好了..奉上代码:

 

 

ContractedBlock.gif ExpandedBlockStart.gif 压缩打包代码
    /// <summary>
    
/// Zips the specified zip path.
    
/// </summary>
    
/// <param name="strZipPath">The zip path.</param>
    
/// <param name="strZipTopDirectoryPath">The zip top directory path.</param>
    
/// <param name="intZipLevel">The zip level.</param>
    
/// <param name="strPassword">The password.</param>
    
/// <param name="filesOrDirectoriesPaths">The files or directories paths.</param>
    
/// <returns></returns>
    private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths)
    {
        
try
        {
            List
<string> AllFilesPath = new List<string>();
            
if (filesOrDirectoriesPaths.Length > 0// get all files path
            {
                
for (int i = 0; i < filesOrDirectoriesPaths.Length; i++)
                {
                    
if (File.Exists(filesOrDirectoriesPaths[i]))
                    {
                        AllFilesPath.Add(filesOrDirectoriesPaths[i]);
                    }
                    
else if (Directory.Exists(filesOrDirectoriesPaths[i]))
                    {
                        GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);
                    }
                }
            }

            
if (AllFilesPath.Count > 0)
            {

                ZipOutputStream zipOutputStream 
= new ZipOutputStream(File.Create(strZipPath));
                zipOutputStream.SetLevel(intZipLevel);
                zipOutputStream.Password 
= strPassword;

                
for (int i = 0; i < AllFilesPath.Count; i++)
                {
                    
string strFile = AllFilesPath[i].ToString();
                    
try
                    {
                        
if (strFile.Substring(strFile.Length - 1== "\\"//folder
                        {
                            
string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
                            
if (strFileName.StartsWith("\\"))
                            {
                                strFileName 
= strFileName.Substring(1);
                            }
                            ZipEntry entry 
= new ZipEntry(strFileName);
                            entry.DateTime 
= DateTime.Now;
                            zipOutputStream.PutNextEntry(entry);
                        }
                        
else //file
                        {
                            FileStream fs 
= File.OpenRead(strFile);

                            
byte[] buffer = new byte[fs.Length];
                            fs.Read(buffer, 
0, buffer.Length);

                            
string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
                            
if (strFileName.StartsWith("\\"))
                            {
                                strFileName 
= strFileName.Substring(1);
                            }
                            ZipEntry entry 
= new ZipEntry(strFileName);
                            entry.DateTime 
= DateTime.Now;
                            zipOutputStream.PutNextEntry(entry);
                            zipOutputStream.Write(buffer, 
0, buffer.Length);

                            fs.Close();
                            fs.Dispose();
                        }
                    }
                    
catch
                    {
                        
continue;
                    }
                }

                zipOutputStream.Finish();
                zipOutputStream.Close();

                
return true;
            }
            
else
            {
                
return false;
            }
        }
        
catch
        {
            
return false;
        }
    }

    
/// <summary>
    
/// Gets the directory files.
    
/// </summary>
    
/// <param name="strParentDirectoryPath">The parent directory path.</param>
    
/// <param name="AllFilesPath">All files path.</param>
    private void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath)
    {
        
string[] files = Directory.GetFiles(strParentDirectoryPath);
        
for (int i = 0; i < files.Length; i++)
        {
            AllFilesPath.Add(files[i]);
        }
        
string[] directorys = Directory.GetDirectories(strParentDirectoryPath);
        
for (int i = 0; i < directorys.Length; i++)
        {
            GetDirectoryFiles(directorys[i], AllFilesPath);
        }
        
if (files.Length == 0 && directorys.Length == 0//empty folder
        {
            AllFilesPath.Add(strParentDirectoryPath);
        }
    }

 

调用也很简单:

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
        string strZipPath = @"C:\Documents and Settings\ken\Desktop\Task1\ZipTest.zip";
        
string strZipTopDirectoryPath = @"C:\Documents and Settings\ken\Desktop\Task1\";
        
int intZipLevel = 6;
        
string strPassword = "";
        
string[] filesOrDirectoriesPaths = new string[] { @"C:\Documents and Settings\ken\Desktop\Task1\zipdemo\11\"
            
@"C:\Documents and Settings\ken\Desktop\Task1\zipdemo\Bin\ICSharpCode.SharpZipLib.dll" };
        Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);

 

就这样.如果需要尽管拿走.

(代码参考过网络上的资源,如有冒犯,莫怪莫怪~)

 

由于上边代码对于大文件是一次读入,所以遇到大文件的时候是占用资源比较紧张。所以改为分段读取,每次读取64K数据:

 

 

                             string  fileName  =  file.Replace(zipTopDirectory,  "" );
                            
if  (fileName.StartsWith( " \\ " ))
                                fileName 
=  fileName.Substring( 1 );
                            ZipEntry entry 
=   new  ZipEntry(fileName);
                            entry.DateTime 
=  DateTime.Now;
                            zipedStream.PutNextEntry(entry);
                            
for  ( long  j  =   0 ; j  <  fs.Length; j  +=   65536 )
                            {
                                
int  byteLength  =   65536 ;
                                
if  ((fs.Length  -  j)  <   65536 )
                                {
                                    byteLength 
=  ( int )(fs.Length  -  j);
                                }

                                
byte [] buffer  =   new   byte [byteLength];
                                fs.Read(buffer, 
0 , byteLength);
                                zipedStream.Write(buffer, 
0 , byteLength);
                            }
                            fs.Close();

 

 

 

转载于:https://www.cnblogs.com/KenBlove/archive/2008/08/18/1270407.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值