zoukankan      html  css  js  c++  java
  • 关于SharpZipLib压缩分散的文件及整理文件夹的方法

    今天为了解决压缩分散的文件时,发现想通过压缩对象直接进行文件夹整理很麻烦,因为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");
    }
    

      

      

  • 相关阅读:
    VS2005环境下的DLL应用
    Windows Xp下BMP位图转JPEG格式
    [转]C# 中使用委托对List<>进行排序和筛选
    [转]DRP系统知识点总结
    [转]计算机是如何启动的
    [转]JSP基础知识
    [转] 视觉直观感受若干常用排序算法
    [转]使用余弦定理计算两篇文章的相似性
    [转] 关于幂律分布的一个笔记
    [转]函数式编程(泛函编程模型)初探
  • 原文地址:https://www.cnblogs.com/foren/p/6244454.html
Copyright © 2011-2022 走看看