using ICSharpCode.SharpZipLib.GZip;using ICSharpCode.SharpZipLib.Zip;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;namespace Share{ public class FileHelp { #region 删除指定路径的文件 ////// 删除指定路径的文件 /// /// public static void DeleteFile(string path) { if (File.Exists(path))//判断文件是不是存在 { File.Delete(path);//如果存在则删除 } } ////// 根据路径删除文件或者文件夹 /// /// public static void DeleteFile2(string path) { if (!Directory.Exists(path))//如果不存在,则返回 { return; } FileAttributes attr = File.GetAttributes(path); if (attr == FileAttributes.Directory) { Directory.Delete(path, true); } else { File.Delete(path); } } #endregion #region 压缩文件 ////// 压缩文件 /// /// 文件路径 /// 压缩路径 private static void CreateZipFile(string filesPath, string zipFilePath) { if (!Directory.Exists(filesPath)) { Console.WriteLine("Cannot find directory '{0}'", filesPath); return; } try { string[] filenames = Directory.GetFiles(filesPath); using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath))) { s.SetLevel(9); // 压缩级别 0-9 //s.Password = "123"; //Zip压缩文件密码 byte[] buffer = new byte[4096]; //缓冲区大小 foreach (string file in filenames) { ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } s.Finish(); s.Close(); } } catch (Exception ex) { Console.WriteLine("Exception during processing {0}", ex); } } #endregion #region 解压 ////// 解压,解压到exe目录下 /// /// 要解压的文件路径 public static bool UnZipFile(string zipFilePath, Action proValue,int pos, int posMax) { if (!File.Exists(zipFilePath)) { Console.WriteLine("Cannot find file '{0}'", zipFilePath); return false; } using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { proValue(pos + 1 >= posMax ? pos : pos++); Console.WriteLine(theEntry.Name); string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); // create directory if (directoryName.Length > 0) { Directory.CreateDirectory(directoryName); } if (fileName != String.Empty) { using (FileStream streamWriter = File.Create(theEntry.Name)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } } } } return true; } //使用GZIP解压文件的方法 ////// 使用GZIP解压文件的方法 /// /// /// ///static bool UnGzipFile(string zipfilename, string unzipfilename) { bool blResult;//表示解压是否成功的返回结果 //创建压缩文件的输入流实例 using (GZipInputStream zipFile = new GZipInputStream(File.OpenRead(zipfilename))) { //创建目标文件的流 FileStream destFile = File.Open(unzipfilename, FileMode.Create); try { int buffersize = 2048;//缓冲区的尺寸,一般是2048的倍数 byte[] FileData = new byte[buffersize];//创建缓冲数据 while (buffersize > 0)//一直读取到文件末尾 { buffersize = zipFile.Read(FileData, 0, buffersize);//读取压缩文件数据 destFile.Write(FileData, 0, buffersize);//写入目标文件 } blResult = true; } catch (Exception ee) { //Console.WriteLine(ee.Message); blResult = false; } destFile.Close();//关闭目标文件 zipFile.Close();//关闭压缩文件 } return blResult; } #endregion #region 根据文件路径获取string public static string GetFileJson(string filepath) { string json = string.Empty; using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite)) { using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312"))) { json = sr.ReadToEnd(); } } return json; } #endregion }}