积极答复者
请教关于用ICSharpCode.SharpZipLib做压缩的问题

问题
-
我写了代码,路径如下:
string[] array = new string[5]; array[0] = @"E:\实验\文件包\第6章 函数.wps"; array[1] = @"E:\实验\文件包\第8章 面向对象编程简介.wps"; array[2] = @"E:\实验\文件包\第9章 定义类.wps"; array[3] = @"E:\实验\文件包\第10章 定义类成员.wps";
array[4] = @"D:\新的"; //“新的”是一个文件夹 StartZip.Run(@"e:\", "压缩文件", array);
因为路径直接指向文件,所以我希望压缩的结果是,所有文件都在压缩包的根目录下。但是发现压缩之后,array[0]到array[3]路径下的文件并不在压缩包的根目录下,而是变成“实验\文件包\*.wps”,并不在根目录下,请问问题出在哪呢?
代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.IO; using ICSharpCode.SharpZipLib.Zip; using System.Text.RegularExpressions; namespace MyTest { public static class StartZip { public static void Run(string rootDirectory, string zipFileName, params string[] fileOrDirectory) { if (fileOrDirectory == null || fileOrDirectory.Length < 1) return; Regex reg1 = new Regex(@"[a-z]:$", RegexOptions.IgnoreCase); Match math1 = reg1.Match(rootDirectory); if (math1.Success) { rootDirectory =math1.Value+@"\"; } int zipLevel = 6; string fullZipFileName = rootDirectory + zipFileName + ".zip"; for (int count = 0; count < fileOrDirectory.Length; count++) { string str = fileOrDirectory[count]; if (str.EndsWith("\\")) { str = str.Substring(0, str.Length - 1); } str = str.Substring(0, str.LastIndexOf("\\")); Zip(fullZipFileName, str, zipLevel, fileOrDirectory); } } private static void Zip(string fullZipFileName, string currentDirectory, int zipLevel, params string[] fileOrDirectory) { ArrayList AllFiles = new ArrayList(); //AllFiles:存储具体文件路径的容器 if (fileOrDirectory != null) { for (int i = 0; i < fileOrDirectory.Length; i++) { if (File.Exists(fileOrDirectory[i])) { AllFiles.Add(fileOrDirectory[i]); } else if (Directory.Exists(fileOrDirectory[i])) { GetDirectoryFile(fileOrDirectory[i], AllFiles); } } } if (AllFiles.Count < 1) return; ZipOutputStream zipedStream = new ZipOutputStream(File.Create(fullZipFileName)); //ZipOutputStream的new初始化:创建对应zipFileName文件名的Zip文件 zipedStream.SetLevel(zipLevel); for (int i = 0; i < AllFiles.Count; i++) { string file = AllFiles[i].ToString(); FileStream fs; fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); //新建一个entry string fileName = file.Replace(currentDirectory, ""); if (fileName.StartsWith("\\")) { fileName = fileName.Substring(1); } ZipEntry entry = new ZipEntry(fileName); entry.DateTime = DateTime.Now; fs.Close(); zipedStream.PutNextEntry(entry); zipedStream.Write(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); } zipedStream.Finish(); zipedStream.Close(); } private static void GetDirectoryFile(string parentDirectory, ArrayList toStore) //递归获取一个目录下的所有文件 { string[] files = Directory.GetFiles(parentDirectory); //Directory.GetFiles 方法 (String):返回指定目录中“文件”的名称(包括其路径); for (int i = 0; i < files.Length; i++) { toStore.Add(files[i]); } string[] directorys = Directory.GetDirectories(parentDirectory); //Directory.GetDirectories 方法 (String):获取指定目录中的“子目录”的名称(包括其路径)。 //“子目录”也即“子文件夹”; for (int i = 0; i < directorys.Length; i++) { GetDirectoryFile(directorys[i], toStore); } } } }
- 已编辑 moon_prince 2012年7月19日 4:43
- 已移动 Min Zhu 2012年7月20日 9:04 (发件人:.NET Framework 一般性问题讨论区)
答案
-
你这个类不是.net framework 类吧,请去其官网看看:http://community.sharpdevelop.net/forums/12.aspx
谢谢
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 moon_prince 2012年7月27日 9:45
全部回复
-
你这个类不是.net framework 类吧,请去其官网看看:http://community.sharpdevelop.net/forums/12.aspx
谢谢
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 moon_prince 2012年7月27日 9:45
-
你这个类不是.net framework 类吧,请去其官网看看:http://community.sharpdevelop.net/forums/12.aspx
谢谢
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.