using System; using System.Collections.Generic; using System.IO; using System.Text; using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; namespace Tools { public class SharpZipHelper { /// <summary> /// 存放待压缩的文件的绝对路径 /// </summary> public List<string> AbsolutePaths { set; get; } public string ErrorMsg { set; get; } public SharpZipHelper() { ErrorMsg = ""; AbsolutePaths = new List<string>(); } /// <summary> /// 添加压缩文件 /// </summary> /// <param name="fileAbsolutePath">文件的绝对路径</param> public void AddFile(string fileAbsolutePath) { AbsolutePaths.Add(fileAbsolutePath); } /// <summary> /// 压缩文件 /// </summary> /// <param name="zipedFilePath">压缩后的文件(初始时不存在)</param> /// <param name="compressionLevel">压缩级别</param> /// <param name="blockSize">每次写入的内存大小</param> /// <returns></returns> public bool CompressionZip(string zipedFilePath, int compressionLevel, int blockSize) { bool result = true; FileStream fs = null; try { Encoding gbk = Encoding.GetEncoding("gbk"); // 防止中文名乱码 ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gbk.CodePage; FileStream ZipFile = File.Create(zipedFilePath); // 创建压缩文件 ZipOutputStream ZipStream = new ZipOutputStream(ZipFile); ZipStream.SetLevel(compressionLevel); // 设置压缩级别 foreach (string path in AbsolutePaths) { //如果是目录 if (Directory.Exists(path)) { ZipFloder(path, ZipStream, path); } else if (File.Exists(path))//如果是文件 { fs = File.OpenRead(path); //fs = new FileStream(path, FileMode.Open, FileAccess.Read); // 读取需压缩的文件 //byte[] bts = new byte[fs.Length]; //fs.Read(bts, 0, bts.Length); ZipEntry ze = new ZipEntry(new FileInfo(path).Name); ZipStream.PutNextEntry(ze); //为压缩文件流提供一个容器 byte[] bts = new byte[blockSize]; int size = fs.Read(bts, 0, bts.Length); // 每次读入指定大小 ZipStream.Write(bts, 0, size); while (size < fs.Length) // 保证文件被全部写入 { int sizeRead = fs.Read(bts, 0, bts.Length); ZipStream.Write(bts, 0, sizeRead); size += sizeRead; } //ZipStream.Write(bts, 0, bts.Length); //写入字节 } } ZipStream.Finish(); // 结束压缩 ZipStream.Close(); fs.Close(); } catch (Exception ex) { if (fs != null) { fs.Close(); } ErrorMsg = ex.Message; result = false; } return result; } /// <summary> /// 压缩文件夹 /// </summary> /// <param name="ofloderPath">要压缩的文件目录</param> /// <param name="zos">压缩后的文件</param> /// <param name="floderPath">压缩后存放的文件目录</param> private void ZipFloder(string ofloderPath, ZipOutputStream zos, string floderPath) { Crc32 crc = new Crc32(); Encoding gbk = Encoding.GetEncoding("gbk"); // 防止中文名乱码 ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gbk.CodePage; foreach (FileSystemInfo item in new DirectoryInfo(floderPath).GetFileSystemInfos()) { if (Directory.Exists(item.FullName)) { ZipFloder(ofloderPath, zos, item.FullName); } else if (File.Exists(item.FullName))//如果是文件 { DirectoryInfo oDir = new DirectoryInfo(ofloderPath); string fullName2 = new FileInfo(item.FullName).FullName; string path = oDir.Name + fullName2.Substring(oDir.FullName.Length, fullName2.Length - oDir.FullName.Length);//获取相对目录 FileStream fs = File.OpenRead(fullName2); byte[] bts = new byte[fs.Length]; fs.Read(bts, 0, bts.Length); ZipEntry ze = new ZipEntry(path); ze.DateTime = DateTime.Now; ze.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(bts); ze.Crc = crc.Value; zos.PutNextEntry(ze); //为压缩文件流提供一个容器 zos.Write(bts, 0, bts.Length); //写入字节 } } } /// <summary> /// 解压 /// </summary> /// <param name="depositPath">压缩文件路径</param> /// <param name="fileDir">解压的路径</param> /// <returns></returns> public bool DeCompressionZip(string depositPath, string fileDir) { bool result = true; FileStream fs = null; try { Encoding gbk = Encoding.GetEncoding("gbk"); // 防止中文名乱码 ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gbk.CodePage; ZipInputStream inpStream = new ZipInputStream(File.OpenRead(depositPath.Trim())); ZipEntry ze;//获取压缩文件中的每一个文件 Directory.CreateDirectory(fileDir);//创建解压文件夹 while ((ze = inpStream.GetNextEntry()) != null)//如果解压完ze则是null { if (ze.IsFile)//压缩zipINputStream里面存的都是文件。带文件夹的文件名字是文件夹\文件名 { string[] strs = ze.Name.Split('\');//如果文件名中包含’\‘则表明有文件夹 if (strs.Length > 1) { //两层循环用于一层一层创建文件夹 for (int i = 0; i < strs.Length - 1; i++) { string floderPath = fileDir; for (int j = 0; j < i; j++) { floderPath = floderPath + "\" + strs[j]; } floderPath = floderPath + "\" + strs[i]; Directory.CreateDirectory(floderPath); } } fs = new FileStream(fileDir + "\" + ze.Name, FileMode.OpenOrCreate, FileAccess.Write);//创建文件 //循环读取文件到文件流中 while (true) { byte[] bts = new byte[2048]; int i = inpStream.Read(bts, 0, bts.Length); if (i > 0) { fs.Write(bts, 0, i); } else { break; } } fs.Flush(); fs.Close(); } } inpStream.Close(); } catch (Exception ex) { if (fs != null) { fs.Close(); } ErrorMsg = ex.Message; result = false; } return result; } } }
参考:
http://blog.csdn.net/e295166319/article/details/52824235 使用C#压缩解压zip格式文件
http://www.open-open.com/lib/view/open1389773696961.html C#解压或压缩文件夹
http://www.cnblogs.com/Bonker/archive/2012/12/25/2831970.html C#中关于zip压缩解压帮助类的封装
http://blog.sina.com.cn/s/blog_45eaa01a0102x7if.html C#获取文件夹下的所有文件的文件名
String path = @"X:xxxxxx"; //第一种方法 var files = Directory.GetFiles(path, "*.txt"); foreach (var file in files) Console.WriteLine(file); //第二种方法 DirectoryInfo folder = new DirectoryInfo(path); foreach (FileInfo file in folder.GetFiles("*.txt")) { Console.WriteLine(file.FullName); }
C#获取文件夹下的所有文件的文件名
//C#遍历指定文件夹中的所有文件 DirectoryInfo TheFolder=new DirectoryInfo(folderFullName); //遍历文件夹 foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories()) this.listBox1.Items.Add(NextFolder.Name); //遍历文件 foreach(FileInfo NextFile in TheFolder.GetFiles()) this.listBox2.Items.Add(NextFile.Name);
1. DirectoryInfo.GetFiles():获取目录中(不包含子目录)的文件,返回类型为FileInfo[],支持通配符查找;
2. DirectoryInfo.GetDirectories():获取目录(不包含子目录)的子目录,返回类型为DirectoryInfo[],支持通配符查找;
3. DirectoryInfo. GetFileSystemInfos():获取指定目录下(不包含子目录)的文件和子目录,返回类型为FileSystemInfo[],支持通配符查找;
如何获取指定文件的基本信息;
FileInfo.Exists:获取指定文件是否存在;
FileInfo.Name,FileInfo.Extensioin:获取文件的名称和扩展名;
FileInfo.FullName:获取文件的全限定名称(完整路径);
FileInfo.Directory:获取文件所在目录,返回类型为DirectoryInfo;
FileInfo.DirectoryName:获取文件所在目录的路径(完整路径);
FileInfo.Length:获取文件的大小(字节数);
FileInfo.IsReadOnly:获取文件是否只读;
FileInfo.Attributes:获取或设置指定文件的属性,返回类型为FileAttributes枚举,可以是多个值的组合
FileInfo.CreationTime、FileInfo.LastAccessTime、FileInfo.LastWriteTime:分别用于获取文件的创建时间、访问时间、修改时间;
http://developer.51cto.com/art/200908/144236.htm C#删除指定文件两种方法
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Collections; /// 〈 summary〉 /// 删除目录再创建相同目录 /// 〈 /summary〉 /// 〈 param name="strDir"〉目录地址 〈 /param〉 public static void deleteFiles( string strDir) { if (Directory.Exists(strDir)) { Directory.Delete(strDir, true); Directory.CreateDirectory(strDir); Console.WriteLine("文件删除成功!"); } else { Console.WriteLine("此目录不存在!"); } }
/// 〈 summary〉 /// 直接删除目录下的所有文件及文件夹(保留目录) /// 〈 /summary〉 /// 〈 param name="strDir"〉目录地址 〈 /param〉 public static void deleteFiles3( string strDir) { if (Directory.Exists(strDir)) { string[] strDirs = Directory.GetDirectories(strDir); string[] strFiles = Directory.GetFiles(strDir); foreach (string strFile in strFiles) { File.Delete(strFile); } foreach (string strdir in strDirs) { Directory.Delete(strdir, true); } Console.WriteLine("删除成功!"); } else { Console.WriteLine("此目录不存在!"); } }
//C# 查找指定文件夹下指定后缀名的所有文件 public void GetFiles(string dir) { try { string[] files = Directory.GetFiles(dir);//得到文件 foreach (string file in files)//循环文件 { string exname = file.Substring(file.LastIndexOf(".") + 1);//得到后缀名 // if (".txt|.aspx".IndexOf(file.Substring(file.LastIndexOf(".") + 1)) > -1)//查找.txt .aspx结尾的文件 if (".txt".IndexOf(file.Substring(file.LastIndexOf(".") + 1)) > -1)//如果后缀名为.txt文件 { FileInfo fi = new FileInfo(file);//建立FileInfo对象 alst.Add(fi.FullName);//把.txt文件全名加人到FileInfo对象 } } } catch { } } private static string result = ""; /// <summary> /// 得某文件夹下所有的文件 /// </summary> /// <param name="directory">文件夹名称</param> /// <param name="pattern">搜寻指类型</param> /// <returns></returns> public static string GetFiles(DirectoryInfo directory, string pattern) { if(directory.Exists || pattern.Trim()!=string.Empty) { foreach(FileInfo info in directory.GetFiles(pattern)) { result = result + info.FullName.ToString() + ";"; //result = result + info.Name.ToString() + ";"; } foreach(DirectoryInfo info in directory.GetDirectories()) { GetFiles(info,pattern); } } string returnString = result; return returnString; } //GetFiles(new DirectoryInfo(@"D:TestGetFilesTestGetFilesxiaobaigang"),"*.txt");