美文网首页工具癖
文件或者文件夹压缩辅助类ZipUtility

文件或者文件夹压缩辅助类ZipUtility

作者: 05b0963bb480 | 来源:发表于2018-12-12 12:48 被阅读0次

实现效果

  • 本辅助类主要是用来方便实现文件(包括文件夹)压缩的辅助操作。
  • 本类库使用了第三方开源控件ICSharpCode.SharpZipLib。

实现步骤

  • 在代码引用相关的代码实现动态调用。
  • 引用ICSharpCode.SharpZipLib.dll类库。

实现代码

  • 辅助类提供的方法代码如下所示:
/// <summary> 
/// 压缩文件中的文件,可设置密码
/// </summary>
public static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)
/// <summary>
/// 解压文件到指定的目录,可设置密码、删除原文件等
/// </summary>
public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
/// <summary>
/// 压缩文件
/// </summary>
public static bool ZipFile(string fileToZip, string zipedFile)
/// <summary>
/// 解压缩以后的文件名和路径,压缩前的路径
/// </summary>
public static Boolean UnZipFile(string zipFile, string targetDirectory)
  • 辅助类的使用例子代码如下所示
if (manifest != null)
{
string path = Path.GetFullPath(manifest.MyApplication.Location);
foreach (ManifestFile file in manifest.ManifestFiles.Files)
{
bool unzip = false;
bool.TryParse(file.Unzip, out unzip);
if (file.Source.EndsWith(".zip", StringComparison.OrdinalIgnoreCase) && unzip)
{
string zipFile = Path.Combine(path, file.Source);
try
{
ZipUtility.UnZipFiles(zipFile, path, null, true);
}
catch (Exception ex)
{
WriteLine(ex.ToString());
}
}
}
}
/// <summary>
/// 编辑状态下的数据保存
/// </summary>
/// <returns></returns>
public override bool SaveUpdated()
{
bool exist = false;
new ItemDetailServiceClient().Using(client =>
{
exist = client.CheckExist(this.txtItemNo.Text, ID);
});
if (exist)
{
MessageUtil.ShowTips("指定的编号已经存在,不能重复添加,请修改");
return false;
}
ItemDetailInfo info = null;
new ItemDetailServiceClient().Using(client =>
{
info = client.FindByID(ID);
});

附上源码

     /// <summary>
    /// 使用ICSharpCode.SharpZipLib进行文件(包括文件夹)压缩的辅助类
    /// </summary>
    public static class ZipUtility
    {
        #region 文件夹压缩、解压缩

        /// <summary>
        /// 压缩指定文件列表到一个指定的内存流里面,可设置密码
        /// </summary>
        /// <param name="fileList">文件路径列表</param>
        /// <param name="stream">指定的内存流</param>
        /// <param name="password">压缩密码</param>
        public static void ZipFiles(List<string> fileList, Stream stream, string password)
        {
            FileStream ostream;
            byte[] obuffer;
            ZipOutputStream oZipStream = new ZipOutputStream(stream);
            if (!string.IsNullOrEmpty(password))
            {
                oZipStream.Password = password;
            }
            oZipStream.SetLevel(9); // 设置最大压缩率

            ZipEntry oZipEntry;
            foreach (string file in fileList)
            {
                string fileName = Path.GetFileName(file);
                oZipEntry = new ZipEntry(fileName);
                oZipStream.PutNextEntry(oZipEntry);

                if (!file.EndsWith(@"/")) // 如果文件以 '/' 结束,则是目录
                {
                    ostream = File.OpenRead(file);
                    obuffer = new byte[ostream.Length];
                    ostream.Read(obuffer, 0, obuffer.Length);
                    oZipStream.Write(obuffer, 0, obuffer.Length);
                }
            }
            oZipStream.Finish();
            oZipStream.Close();
        }

        /// <summary>
        /// 压缩指定文件列表到一个指定的压缩文件里面,可设置密码
        /// </summary>
        /// <param name="fileList">文件路径列表</param>
        /// <param name="outputPathAndFile">输出的压缩文件全名</param>
        /// <param name="password">压缩密码</param>
        public static void ZipFiles(List<string> fileList, string outputPathAndFile, string password)
        {
            FileStream ostream;
            byte[] obuffer;
            string outPath = outputPathAndFile;//inputFolderPath + @"\" + outputPathAndFile;
            ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath));
            if (!string.IsNullOrEmpty(password))
            {
                oZipStream.Password = password;
            }
            oZipStream.SetLevel(9); // 设置最大压缩率

            ZipEntry oZipEntry;
            foreach (string file in fileList)
            {
                string fileName = Path.GetFileName(file);
                oZipEntry = new ZipEntry(fileName);
                oZipStream.PutNextEntry(oZipEntry);

                if (!file.EndsWith(@"/")) // 如果文件以 '/' 结束,则是目录
                {
                    ostream = File.OpenRead(file);
                    obuffer = new byte[ostream.Length];
                    ostream.Read(obuffer, 0, obuffer.Length);
                    oZipStream.Write(obuffer, 0, obuffer.Length);
                }
            }
            oZipStream.Finish();
            oZipStream.Close();
        }

        /// <summary>
        /// 压缩文件中的文件,可设置密码
        /// </summary>
        /// <param name="inputFolderPath">输入的文件夹</param>
        /// <param name="outputPathAndFile">输出的压缩文件全名</param>
        /// <param name="password">压缩密码</param>
        public static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)
        {
            List<string> fileList = GenerateFileList(inputFolderPath);
            int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;
            // find number of chars to remove   // from orginal file path
            TrimLength += 1; //remove '\'

            FileStream ostream;
            byte[] obuffer;
            string outPath = outputPathAndFile;//inputFolderPath + @"\" + outputPathAndFile;
            ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath));
            if (!string.IsNullOrEmpty(password))
            {
                oZipStream.Password = password;
            }
            oZipStream.SetLevel(9); // 设置最大压缩率

            ZipEntry oZipEntry;
            foreach (string file in fileList)
            {
                oZipEntry = new ZipEntry(file.Remove(0, TrimLength));
                oZipStream.PutNextEntry(oZipEntry);

                if (!file.EndsWith(@"/")) // 如果文件以 '/' 结束,则是目录
                {
                    ostream = File.OpenRead(file);
                    obuffer = new byte[ostream.Length];
                    ostream.Read(obuffer, 0, obuffer.Length);
                    oZipStream.Write(obuffer, 0, obuffer.Length);
                }
            }
            oZipStream.Finish();
            oZipStream.Close();
        }

        /// <summary>
        /// 根据文件夹生成文件列表
        /// </summary>
        /// <param name="Dir">指定文件夹</param>
        /// <returns></returns>
        private static List<string> GenerateFileList(string Dir)
        {
            List<string> fileList = new List<string>();
            bool Empty = true;
            foreach (string file in Directory.GetFiles(Dir))
            {
                fileList.Add(file);
                Empty = false;
            }

            if (Empty)
            {
                //加入完全为空的目录
                if (Directory.GetDirectories(Dir).Length == 0)
                {
                    fileList.Add(Dir + @"/");
                }
            }

            foreach (string dirs in Directory.GetDirectories(Dir)) // 递归目录
            {
                foreach (string file in GenerateFileList(dirs))
                {
                    fileList.Add(file);
                }
            }
            return fileList;
        }

        /// <summary>
        /// 解压文件到指定的目录,可设置密码、删除原文件等
        /// </summary>
        /// <param name="zipPathAndFile">压缩文件全名</param>
        /// <param name="outputFolder">解压输出文件目录</param>
        /// <param name="password">解压密码</param>
        /// <param name="deleteZipFile">是否删除原文件(压缩文件)</param>
        public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
        {
            using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile)))
            {
                if (password != null && password != String.Empty)
                {
                    s.Password = password;
                }

                ZipEntry theEntry;
                string tmpEntry = String.Empty;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    #region 遍历每个Entry对象进行解压处理
                    string directoryName = outputFolder;
                    string fileName = Path.GetFileName(theEntry.Name);
                    if (directoryName != "")
                    {
                        Directory.CreateDirectory(directoryName);
                    }

                    if (fileName != String.Empty)
                    {
                        if (theEntry.Name.IndexOf(".ini") < 0)
                        {
                            string fullPath = directoryName + "\\" + theEntry.Name;
                            fullPath = fullPath.Replace("\\ ", "\\");
                            string fullDirPath = Path.GetDirectoryName(fullPath);
                            if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
                            using (FileStream streamWriter = File.Create(fullPath))
                            {
                                #region 写入文件流
                                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;
                                    }
                                }
                                #endregion
                            }
                        }
                    }
                    #endregion
                }
            }

            if (deleteZipFile)
            {
                File.Delete(zipPathAndFile);
            }
        }

        #endregion

        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="fileToZip">待压缩文件路径</param>
        /// <param name="zipedFile">压缩后文件路径</param>
        /// <returns></returns>
        public static bool ZipFile(string fileToZip, string zipedFile)
        {
            try
            {
                FastZip fastZip = new FastZip();
                bool recurse = true;//压缩后的文件名,压缩目录 ,是否递归 
                                
                fastZip.CreateZip(fileToZip, zipedFile, recurse, "");
                return true;
            }
            catch { return false; }
        }

        /// <summary>
        /// 解压缩以后的文件名和路径,压缩前的路径
        /// </summary>
        /// <param name="zipFile">压缩的文件路径</param>
        /// <param name="targetDirectory">解压后的文件路径</param>
        /// <returns></returns>
        public static Boolean UnZipFile(string zipFile, string targetDirectory)
        {
            try
            {
                FastZip fastZip = new FastZip();
                fastZip.ExtractZip(zipFile, targetDirectory, "");
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

相关文章

网友评论

    本文标题:文件或者文件夹压缩辅助类ZipUtility

    本文链接:https://www.haomeiwen.com/subject/zkzlcqtx.html