C# 使用7z压缩

官网地址:https://archive.codeplex.com/?p=sevenzipsharp

项目中,直接获取7z的项目

Install-Package SevenZipSharp -Version 0.64.0

然后,下载7z.dll 

https://github.com/gdoujkzz/7zdll 

然后,项目会用SevenZipSharp的方法调用,7z.dll,实现对文件的7z算法压缩。

备用地址:https://download.csdn.net/download/kesshei/11755956

    /// 
    /// 7z压缩
    /// 
    public class R7z
    {
        public R7z()
        {
            var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "7z\\7z.dll");
            SevenZipBase.SetLibraryPath(path);
        }
        /// 
        /// 压缩文件
        /// 
        /// target.7z
        /// xxx.txt,xxx.txt
        /// 
        public bool CompressFiles(string targetName, params string[] files)
        {
            try
            {
                var tmp = new SevenZipCompressor();
                tmp.ScanOnlyWritable = true;
                tmp.CompressFiles(targetName, files);
                return true;
            }
            catch (Exception)
            { }
            return false;
        }
        /// 
        /// 加密压缩文件
        /// 
        /// target.7z
        /// xxx.txt,xxx.txt
        /// 
        public bool CompressFilesEncrypted(string targetName, string password, params string[] files)
        {
            try
            {
                var tmp = new SevenZipCompressor();
                tmp.ScanOnlyWritable = true;
                tmp.CompressFilesEncrypted(targetName, password, files);
                return true;
            }
            catch (Exception)
            { }
            return false;
        }
        /// 
        /// 解压文件
        /// 
        /// 压缩的文件
        /// 解压的目标地址下
        /// 解密密码
        /// 
        public bool Decompression(string CompressFile, string targetAddress, string password = "")
        {
            try
            {
                if (string.IsNullOrWhiteSpace(password))
                {
                    using (var tmp = new SevenZipExtractor(CompressFile))
                    {
                        for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
                        {
                            tmp.ExtractFiles(targetAddress, tmp.ArchiveFileData[i].Index);
                        }
                    }
                }
                else
                {
                    using (var tmp = new SevenZipExtractor(CompressFile, password))
                    {
                        for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
                        {
                            tmp.ExtractFiles(targetAddress, tmp.ArchiveFileData[i].Index);
                        }
                    }
                }
            }
            catch (Exception)
            { }
            return false;
        }
    }

使用方式

            new R7z().CompressFiles(@"C:\Users\Administrator\Desktop\a.7z", @"C:\Users\Administrator\Desktop\2a2baf80-8ded-4a09-80d4-880a740ebb49");
            new R7z().Decompression(@"C:\Users\Administrator\Desktop\a.7z", @"C:\Users\Administrator\Desktop\");
            new R7z().CompressFilesEncrypted(@"C:\Users\Administrator\Desktop\a.7z", "123456", @"C:\Users\Administrator\Desktop\2a2baf80-8ded-4a09-80d4-880a740ebb49");
            new R7z().Decompression(@"C:\Users\Administrator\Desktop\a.7z", @"C:\Users\Administrator\Desktop", "123456");

经测试正常,下面提供具体的demo

https://download.csdn.net/download/kesshei/11757430

链接:https://pan.baidu.com/s/1r1UzH7xtqWEI8ngtjXWxqg 
提取码:zz81 

 

你可能感兴趣的:(.Net)