C# 7-zip 压缩和解压缩

2021-01-04 10:29

阅读:589

标签:多个   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;
            }
        }

        /// 
        /// 解压
        /// 
        /// 解压的zip文件
        /// 解压到某个文件夹
        /// 
        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;
            }
        }


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

        /// 
        /// 压缩
        /// 
        /// 需要压缩文件所在路径
        /// 压缩文件新的保存路径
        /// 
        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


评论


亲,登录后才可以留言!