將指定文件夾多個文件壓縮為一個zip

本文介绍了一个利用SharpZipLib库实现文件压缩和解压缩的.NET类。该类提供了两个主要方法:UnZip用于解压缩指定的ZIP文件到目标目录,ZipMultiFile用于将指定目录下的多个文件压缩成一个ZIP文件。文章详细展示了如何读取文件数据并将其写入压缩流。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
using  System.IO;
None.gif
using  System.Data;
None.gif
None.gif
using  ICSharpCode.SharpZipLib;
None.gif
using  ICSharpCode.SharpZipLib.Zip;
None.gif
using  ICSharpCode.SharpZipLib.Zip.Compression;
None.gif
using  ICSharpCode.SharpZipLib.Zip.Compression.Streams;
None.gif
using  ICSharpCode.SharpZipLib.Checksums;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
/// Summary description for zip
ExpandedBlockEnd.gif
/// </summary>

None.gif public   class  zip
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public zip()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//
InBlock.gif        
// TODO: Add constructor logic here
InBlock.gif        
//
ExpandedSubBlockEnd.gif
    }

InBlock.gif   
InBlock.gif    
public void UnZip( string strFile, string strDir ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif            
说明#region 说明
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////
InBlock.gif            
/// 功能:将一个压缩文件解压缩到一个目录下
InBlock.gif            
/// 
InBlock.gif            
/// 参数:
InBlock.gif            
///   strFile:压缩文件
InBlock.gif            
///   strDir:解压缩目录
InBlock.gif            
/// 
InBlock.gif            
/// 返回:
InBlock.gif            
///   无
ExpandedSubBlockEnd.gif            
///

ExpandedSubBlockEnd.gif            #endregion

InBlock.gif            
if (strDir == "")    strDir = Directory.GetCurrentDirectory();
InBlock.gif            
if (!strDir.EndsWith(@"\"))    strDir = strDir + @"\";
InBlock.gif
InBlock.gif            ZipInputStream s 
= new ZipInputStream(File.OpenRead(strFile));
InBlock.gif            ZipEntry theEntry;
InBlock.gif
InBlock.gif            
while ((theEntry = s.GetNextEntry()) != null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string directoryName = "";
InBlock.gif                
string pathToZip = "";
InBlock.gif                pathToZip 
= theEntry.Name;
InBlock.gif
InBlock.gif                
if (pathToZip != "")
InBlock.gif                    directoryName 
= Path.GetDirectoryName(pathToZip) + @"\";
InBlock.gif                
string fileName = Path.GetFileName(pathToZip);
InBlock.gif                Directory.CreateDirectory(strDir 
+ directoryName);
InBlock.gif                
if (fileName != ""
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    FileStream streamWriter 
= File.Create(strDir + directoryName + fileName);
InBlock.gif                    
int size = 2048;
InBlock.gif                    
byte[] data = new byte[2048];
InBlock.gif                    
while (true
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        size 
= s.Read(data, 0, data.Length);
InBlock.gif                        
if (size > 0
InBlock.gif                            streamWriter.Write(data, 
0, size);
InBlock.gif                        
else 
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    streamWriter.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            s.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif    
//壓縮指定目錄的多個文件夾到一個壓縮文件
InBlock.gif
    public string  ZipMultiFile(string ZipFromFileDictory, string ZiptoFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string[] strFiles = Directory.GetFiles(ZipFromFileDictory);
InBlock.gif        
string strPhysicalPath = ZiptoFileName;
InBlock.gif        
string strZipFileName = strPhysicalPath + ".zip";
InBlock.gif
InBlock.gif        
if (File.Exists(strZipFileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            File.Delete(strZipFileName);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//需壓縮的文件個數
InBlock.gif
        string[] strFilePaths = new string[strFiles.Length];
InBlock.gif
InBlock.gif        MemoryStream oMemoryStream 
= new MemoryStream();
InBlock.gif
InBlock.gif        ZipOutputStream oZipStream 
= new ZipOutputStream(File.Create(strZipFileName));
InBlock.gif
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
for (int i = 0; i <= strFiles.Length - 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif                
Read File Data To Stream#region Read File Data To Stream
InBlock.gif
InBlock.gif                FileStream oReadFileStream 
= File.OpenRead(strFiles[i]);
InBlock.gif                
byte[] btFile = new byte[oReadFileStream.Length];
InBlock.gif                oReadFileStream.Read(btFile, 
0, btFile.Length);
InBlock.gif
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
InBlock.gif                
string strCurrentFileName = Path.GetFileName(strFiles[i]);
InBlock.gif                strFilePaths[i] 
= strPhysicalPath + "/" + strCurrentFileName;
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                ZipEntry oZipEntry 
= new ZipEntry(strCurrentFileName);
InBlock.gif
InBlock.gif                oZipEntry.DateTime 
= DateTime.Now;
InBlock.gif                oZipEntry.Size 
= oReadFileStream.Length;
InBlock.gif
InBlock.gif                Crc32 oCrc32 
= new Crc32();
InBlock.gif                oCrc32.Reset();
InBlock.gif                oCrc32.Update(btFile);
InBlock.gif
InBlock.gif
InBlock.gif                oZipEntry.Crc 
= oCrc32.Value;
InBlock.gif
InBlock.gif                oZipStream.PutNextEntry(oZipEntry);
InBlock.gif                oZipStream.Write(btFile, 
0, btFile.Length);
InBlock.gif
InBlock.gif                
//Stream Close
InBlock.gif
                oReadFileStream.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            oZipStream.Finish();
InBlock.gif            oZipStream.Close();
InBlock.gif
InBlock.gif            
return "A";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oZipStream.Finish();
InBlock.gif            oZipStream.Close();
InBlock.gif            
return "Error" + ex.Message;
ExpandedSubBlockEnd.gif        }

InBlock.gif       
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

壓縮控件:sharpziplib.zip

转载于:https://www.cnblogs.com/Nina-piaoye/archive/2008/06/25/1229582.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值