今天为了解决压缩分散的文件时,发现想通过压缩对象直接进行文件夹整理很麻烦,因为SharpZipLib没有提供压缩进某个指定文件夹的功能,在反复分析了SharpZipLib提供的各个接口方法后,终于找到了解决方法,现在贴出来,给需要的同学参考参考。
下面是封装的压缩类:
using ICSharpCode.SharpZipLib.Zip; using System; using System.IO; namespace CompressTools { /// <summary> /// SharpZipLib压缩 /// </summary> public class Zip { /// <summary> /// 创建压缩对象 /// </summary> /// <param name="targeFile">目标文件</param> /// <returns></returns> public static ZipOutputStream CreateZip(string targeFile) { Directory.CreateDirectory(Path.GetDirectoryName(targeFile)); var s = new ZipOutputStream(File.Create(targeFile)); s.SetLevel(6); return s; } /// <summary> /// 关闭压缩对象 /// </summary> /// <param name="zip"></param> public static void CloseZip(ZipOutputStream zip) { zip.Finish(); zip.Close(); } /// <summary> /// 压缩文件 /// </summary> /// <param name="s">压缩文件流</param> /// <param name="source">不可空,待压缩的文件</param> /// <param name="folder">可空,目标文件夹(不指定则默认取待压缩文件目录的计算值)</param> public static bool Compress(ZipOutputStream s, string source, string folder) { if (s == null) { throw new FileNotFoundException("压缩文件流不可为空"); } if (!File.Exists(source)) { throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", source)); } using (FileStream fs = File.OpenRead(source)) { ZipEntry entry = null; if (string.IsNullOrWhiteSpace(folder)) { entry = new ZipEntry(source.Replace(Path.GetPathRoot(source), "")); } else { var path = folder.Contains(":\") ? folder.Replace(Path.GetPathRoot(folder), "") : folder; entry = new ZipEntry(path + "\" + Path.GetFileName(source)); } var buffer = File.ReadAllBytes(source); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } return true; } /// <summary> /// 解压缩 /// </summary> /// <param name="sourceFile">源文件</param> /// <param name="targetPath">目标路经</param> public static bool Decompress(string sourceFile, string targetPath) { if (!File.Exists(sourceFile)) { throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", sourceFile)); } if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } using (ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFile))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directorName = Path.Combine(targetPath, Path.GetDirectoryName(theEntry.Name)); string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name)); if (directorName.Length > 0) { Directory.CreateDirectory(directorName); } if (!string.IsNullOrWhiteSpace(fileName)) { using (FileStream streamWriter = File.Create(fileName)) { int size = (int)theEntry.Size; byte[] data = new byte[size]; size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } } } } } return true; } } }
测试方法:
public ActionResult Index() { //创建压缩对象 var zip = Zip.CreateZip(@"D:\testZip\test.zip"); //根据需要添加压缩对象 Zip.Compress(zip, "E:\Document\down.png", ""); Zip.Compress(zip, "E:\Document\ending.mp4", ""); Zip.Compress(zip, "E:\Document\ending.mp4", "D:\testChildFolder"); Zip.Compress(zip, "E:\WorkSpace\test.jpg", "testChildFolder"); Zip.Compress(zip, "E:\WorkSpace\test.jpg", "testFolder\testChildFolder"); //关闭压缩对象 zip.Close(); // 解压缩 Zip.Decompress(@"D:\testZip\test.zip", @"D:\Zip"); }