主题:C#调用SharpZipLib压缩文件夹
			
 xtwvfp
				 [专家分:0]  发布于 2006-05-22 15:29:00							
			大家好,我想问个问题
我用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个回复)
		
								
				11 楼
				
					
jzyray [专家分:20610]  发布于 2006-05-23 18:28:00				
				你想变成什么样………………
							 
						
				12 楼
				
					
jzyray [专家分:20610]  发布于 2006-05-23 18:30:00				
				你想写成什么样………………
							 
						
				13 楼
				
					
xtwvfp [专家分:0]  发布于 2006-05-30 15:43:00				
				我就想写成用用ICSharpZipLib类进行文件解压缩时,指定一个文件夹路径,然后对这个文件夹里的内容进行压缩,在这个指定的压缩文件夹里有子文件也有文件夹,也就是将指定压缩的文件夹里所有内容都压缩了
请jzyray老大帮帮我
也请个位帮帮我想想办法
							 
						
				14 楼
				
					
jzyray [专家分:20610]  发布于 2006-05-30 20:25:00				
				
 static void Main(string[] args)
        {
            ipOutputStream u = new ZipOutputStream(File.Create(args[1]));
            string[] FileProperties = new string[2];
            FileProperties[0] = args[0];//待压缩文件目录
            ServerDir = FileProperties[0];
            ServerDir = Path.GetDirectoryName(".");
            FileProperties[1] = args[1];  //压缩后的目标文件
            Program Zc = new Program();
            Zc.AddZipEntry(ServerDir , u);
    }
  public static string ServerDir;
  public string ShortDir(string s)
        {
           //将文件的绝对路径转为相对路径
           string d = s.Replace(ServerDir, "");
            return d;
        }
public void AddZipEntry(string p, ZipOutputStream u)
        {
            
            string[] filenames = Directory.GetDirectories(ServerDir);
            string s = ServerDir + p;//设定一个目录,s是目录存放的路径
            Crc32 crc = new Crc32();
            if (Directory.Exists(s))  //文件夹的处理
            {
                DirectoryInfo di = new DirectoryInfo(s);
                if (di.GetDirectories().Length <= 0)   //没有子目录
                {
                    ZipEntry z = new ZipEntry(p + "//"); //末尾“\\”用于文件夹的标记
                    u.PutNextEntry(z);
                }
                foreach (DirectoryInfo tem in di.GetDirectories())  //获取子目录
                {
                    ZipEntry z = new ZipEntry(this.ShortDir(tem.FullName) + "//"); //末尾“\\”用于文件夹的标记
                    u.PutNextEntry(z);    //此句不可少,否则空目录不会被添加
                    s = this.ShortDir(tem.FullName);
                    this.AddZipEntry(s, u);       //递归
                }
                foreach (FileInfo temp in di.GetFiles())  //获取此目录的文件
                {
                    s = this.ShortDir(temp.FullName);
                    this.AddZipEntry(s, u);      //递归
                }
            }
            else if (File.Exists(s))  //文件的处理
            {
                u.SetLevel(9);      //压缩等级
                FileStream f = File.OpenRead(s);
                byte[] b = new byte[f.Length];
                f.Read(b, 0, b.Length);          //将文件流加入缓冲字节中
                ZipEntry z = new ZipEntry(this.ShortDir(s));
                u.PutNextEntry(z);             //为压缩文件流提供一个容器
                u.Write(b, 0, b.Length);  //写入字节
                f.Close();
            }
            
        }
//我没测试过,试下吧
							 
						
				15 楼
				
					
xtwvfp [专家分:0]  发布于 2006-06-08 12:00:00				
				你写的和我以前报错都是一样的,谢谢你的帮助,
我又写了一个,但这次是报我没有权限访问我所要压缩文件夹,我的系统是2003我都已经将权限加上了Everyone,就还是报没有权限,请大哥在帮我好好看看,
谢谢:   下面是我的新写的程序
							 
						
				16 楼
				
					
xtwvfp [专家分:0]  发布于 2006-06-08 12:01:00				
				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 ICSharpCode.SharpZipLib.BZip2;
using System.Diagnostics;
using ICSharpCode.SharpZipLib.Core;
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)
{
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);
            ServerDir = "C:";
            string p = "unzipped";
            string abc = ServerDir + "//" + p + "/" + time;           // "c://unzipped/" + time;
            System.Console.WriteLine(abc);
            string[] FileProperties = new string[2];
            FileProperties[0] = abc;
            FileProperties[1] = "C://zip/a.zip";
            Program Zc = new Program();
            int BUFFER = 2048;
            ZipOutputStream u = new ZipOutputStream(File.Create(args[0], BUFFER));
            
            ZipOutputStream u_3 = new ZipOutputStream(File.Create(args[1], BUFFER));
            Zc.AddZipEntry(p, u, out u_3);
}
public void CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
        {
            recurse = true;
        }
public static string ServerDir;
public string ShortDir(string s)
        {
            //将文件的绝对路径转为相对路径
            string d = s.Replace(ServerDir, "");
            return d;
        }
下面还有,一次发不完只能分两次发(见下:)
							 
						
				17 楼
				
					
xtwvfp [专家分:0]  发布于 2006-06-08 12:02:00				
				public void AddZipEntry(string p, ZipOutputStream u, out ZipOutputStream u_3)
        {
            //添加压缩项目:p 为需压缩的文件或文件夹; u 为现有的源ZipOutputStream;out j为已添加“ZipEntry”的“ZipOutputStream”
            string ServerDir = "C:";
            string s = ServerDir+"//" + p;//设定一个目录,s是目录存放的路径
            Crc32 crc = new Crc32();
            if (Directory.Exists(s))  //文件夹的处理
            {
                DirectoryInfo di = new DirectoryInfo(s);
                if (di.GetDirectories().Length <= 0)   //没有子目录
                {
                    ZipEntry z = new ZipEntry(p + "//"); //末尾“\\”用于文件夹的标记
                    u.PutNextEntry(z);
                }
                foreach (DirectoryInfo tem in di.GetDirectories())  //获取子目录
                {
                    ZipEntry z = new ZipEntry(this.ShortDir(tem.FullName) + "//"); //末尾“\\”用于文件夹的标记
                    u.PutNextEntry(z);    //此句不可少,否则空目录不会被添加
                    s = this.ShortDir(tem.FullName);
                    this.AddZipEntry(s, u, out u);       //递归
                }
                foreach (FileInfo temp in di.GetFiles())  //获取此目录的文件
                {
                    s = this.ShortDir(temp.FullName);
                    this.AddZipEntry(s, u, out u);      //递归
                }
            }
            else if (File.Exists(s))  //文件的处理
            {
                u.SetLevel(9);      //压缩等级
                FileStream f = File.OpenRead(s);
                byte[] b = new byte[f.Length];
                f.Read(b, 0, b.Length);          //将文件流加入缓冲字节中
                ZipEntry z = new ZipEntry(this.ShortDir(s));
                u.PutNextEntry(z);             //为压缩文件流提供一个容器
                u.Write(b, 0, b.Length);  //写入字节
                f.Close();
            }
            u_3 = u;    //返回已添加数据的“ZipOutputStream”
            //throw new Exception("The method or operation is not implemented.");
        }
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);
            ServerDir = "C:";
            string p = "unzipped";
            string abc = ServerDir + "//"+p+"/" + time;// "c://unzipped/" + time;
            System.Console.WriteLine(abc);
            string[] FileProperties = new string[2];
            FileProperties[0] = abc ;//待压缩文件目录
            FileProperties[1] = "C://zip/a.zip";  //压缩后的目标文件
            Program Zc = new Program();
            Zc.ZipFileMain(FileProperties);
}
							 
						
				18 楼
				
					
jzyray [专家分:20610]  发布于 2006-06-08 12:41:00				
				先检查一下错误堆栈
							 
						
				19 楼
				
					
xtwvfp [专家分:0]  发布于 2006-06-08 14:21:00				
				老大我真不知怎么改了,能否帮帮忙改改
谢谢
							 
						
				20 楼
				
					
jzyray [专家分:20610]  发布于 2006-06-08 19:31:00				
				至少得给我错误信息吧……哪一条语句的错误?
							 
									
			
我来回复