zoukankan      html  css  js  c++  java
  • C# 7-zip 压缩和解压缩

    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;
                }
            }
        }
    }
  • 相关阅读:
    Hibernatede 一对多映射配置
    Hibrenate之事务的理解以及代码编写
    The servlet name already exists.解决方法
    hibernate入门程序
    什么是orm思想?
    Java的MVC模式简介
    JAVA框架之Hibernate框架的学习步骤
    java常见命名规则
    解决get方法提交参数中文乱码问题:
    谈谈对Spring IOC(控制反转)的理解--转
  • 原文地址:https://www.cnblogs.com/netlock/p/13194507.html
Copyright © 2011-2022 走看看