码迷,mamicode.com
首页 > Windows程序 > 详细

C# 7-zip 压缩和解压缩

时间:2020-06-26 14:24:05      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:多个   color   win   nbsp   var   text   new   create   unzip   

using System;
using System.Collections.Generic;
using System.Text;
using SevenZip;
using System.IO;
using System.Windows.Forms;

namespace TestZip
{
    internal static class Zip
    {
        public static bool Init()
        {
            string _7z32 =  Application.StartupPath+ "\\7z32.dll";
            string _7z64 = Application.StartupPath + "\\7z64.dll";
            string _7z = _7z32;
            try
            {
                if (IntPtr.Size == 8)
                    _7z = _7z64;
                SevenZipExtractor.SetLibraryPath(_7z);
                SevenZipCompressor.SetLibraryPath(_7z);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="zip">解压的zip文件</param>
        /// <param name="folder">解压到某个文件夹</param>
        /// <returns></returns>
        public static bool UnZip(string zip, string folder)
        {
            try
            {
                var tmp = new SevenZipExtractor(zip);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                tmp.ExtractArchive(folder);
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }


        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="zip">压缩文件新的保存路径</param>
        /// <param name="fileFullNames">需要压缩文件所在路径(多个需要压缩的文件)</param>
        /// <returns></returns>
        public static bool ToZip(string zip, params string[] fileFullNames)
        {
            try
            {
                var tmp = new SevenZipCompressor();
                tmp.CompressFiles(zip, fileFullNames);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="folder">需要压缩文件所在路径</param>
        /// <param name="zip">压缩文件新的保存路径</param>
        /// <returns></returns>
        public static bool ToZip(string folder, string zip)
        {
            try
            {
                var tmp = new SevenZipCompressor(folder);
                
                tmp.CompressDirectory(folder, zip);
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
    }
}

 

C# 7-zip 压缩和解压缩

标签:多个   color   win   nbsp   var   text   new   create   unzip   

原文地址:https://www.cnblogs.com/netlock/p/13194507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!