回 帖 发 新 帖 刷新版面

主题:C#调用SharpZipLib压缩文件夹

大家好,我想问个问题
我用ICSharpZipLib类进行文件解压缩时,只能压缩文件夹里面的子文件,如果有子文件夹却不能压缩.
能帮我解决一下吗,下面是我的程序,
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 
using System.IO.Compression; 
using System.ComponentModel; 
using ICSharpCode.SharpZipLib.Checksums; 
using ICSharpCode.SharpZipLib.Zip; 
using ICSharpCode.SharpZipLib.GZip; 
using System.Diagnostics; 

namespace SharpZipLib 


class Program 

public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize) 

//如果文件没有找到,则报错 
if (!System.IO.File.Exists(FileToZip)) 

throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd"); 


System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read); 
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile); 
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile); 
ZipEntry ZipEntry = new ZipEntry("ZippedFile"); 
ZipStream.PutNextEntry(ZipEntry); 
ZipStream.SetLevel(CompressionLevel); 
byte[] buffer = new byte[BlockSize]; 
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length); 
ZipStream.Write(buffer, 0, size); 
try 

while (size < StreamToZip.Length) 

int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); 
ZipStream.Write(buffer, 0, sizeRead); 
size += sizeRead; 


catch (System.Exception ex) 

throw ex; 

ZipStream.Finish(); 
ZipStream.Close(); 
StreamToZip.Close(); 


public void ZipFileMain(string[] args)//对文件进行压缩; 

string[] filenames = Directory.GetFiles(args[0]); 

Crc32 crc = new Crc32(); 
ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));//新建压缩文件流 “ZipOutputStream” 

s.SetLevel(6); // 0 - store only to 9 - means best compression 

foreach (string file in filenames) 

//打开要压缩的文件 
FileStream fs = File.OpenRead(file); 

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

entry.DateTime = DateTime.Now; 
entry.Size = fs.Length; 
fs.Close(); 

crc.Reset(); 
crc.Update(buffer); 

entry.Crc = crc.Value; 

s.PutNextEntry(entry); 

s.Write(buffer, 0, buffer.Length); 


s.Finish(); 
s.Close(); 

public static string ServerDir; 
static void Main(string[] args) 

int month = Convert.ToInt32(DateTime.Now.ToString("MM"));//只取当前月份; 
int iOldMonth = month - 1; 
string a = iOldMonth.ToString(); 
string year = DateTime.Now.ToString("yyyy");//只取当前年份; 
string time = Convert.ToString (year + "-" + a); 
System.Console.WriteLine(time); 
string abc = "c://unzipped/" + time; 
string[] FileProperties = new string[2]; 
FileProperties[0] = abc;//待压缩文件目录 
ServerDir = FileProperties[0]; 
//ServerDir = Path.GetDirectoryName("."); 
System.Console.WriteLine(ServerDir); 
FileProperties[1] = "C://zip/a.zip"; //压缩后的目标文件 
Program Zc = new Program(); 
Zc.AddZipEntry(FileProperties); 

这个只能压缩我指定的文件夹里的子文件,文件夹里的子文件夹就不能压缩
请大家帮忙

回复列表 (共23个回复)

21 楼

报错的句子就是
 ZipOutputStream u = new ZipOutputStream(File.Create(args[0], BUFFER));
报的错误是"对路径“C:\unzipped\2006-5”的访问被拒绝。"
请大哥帮我好好查查,
谢谢

22 楼

新建一个文件之前要保证文件所在的文件夹是已经存在的。
估计你这里没有先创建c:\unzipped文件夹吧。

23 楼

这个文件夹,还有这个文件夹的子文件夹都是我在写程序前都创建好的,而且你可以看看程序是已经找到的,所以请大哥帮帮忙,帮我在测试一下

我来回复

您尚未登录,请登录后再回复。点此登录或注册