• .net关于解压


    1.可以调用电脑上的winrar程序进行解压, 缺点是电脑上必须装有winrar程序

    #region WinRAR解压
    /// <summary> 
    /// 利用 WinRAR 进行解压缩 (前提是电脑上面安装了WinRAR解压软件)
    /// </summary> 
    /// <param name="path">文件解压路径(绝对)</param> 
    /// <param name="rarPath">将要解压缩的 .rar 文件的存放目录(绝对路径)</param> 
    /// <param name="rarName">将要解压缩的 .rar 文件名(包括后缀)</param> 
    /// <returns>true 或 false。解压缩成功返回 true,反之,false。</returns> 
    public static bool UnRAR(string path, string rarPath, string rarName)
    {
    bool flag = false;
    string rarexe;
    RegistryKey regkey;
    Object regvalue;
    string cmd;
    ProcessStartInfo startinfo;
    Process process;
    try
    {
    //不同机器的winrar的注册码不同,根据情况而定路径 ApplicationsWinRAR.exeshellopencommand
    regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWAREClassesWinRARshellopencommand"); //目的是为了找到winrar.exe的路径,然后启动WinRAR
    regvalue = regkey.GetValue("");
    rarexe = regvalue.ToString();
    regkey.Close();
    rarexe = rarexe.Substring(1, rarexe.Length - 7);
    Directory.CreateDirectory(path);
    //解压缩命令,相当于在要压缩文件(rarName)上点右键 ->WinRAR->解压到当前文件夹 
    cmd = string.Format("x {0} {1} -y", rarName, path);
    startinfo = new ProcessStartInfo();
    startinfo.FileName = rarexe;
    startinfo.Arguments = cmd;
    startinfo.WindowStyle = ProcessWindowStyle.Hidden;
    startinfo.WorkingDirectory = rarPath;
    process = new Process();
    process.StartInfo = startinfo;
    process.Start();
    process.WaitForExit();
    if (process.HasExited)
    {
    flag = true;
    }
    process.Close();
    }
    catch (Exception e)
    {
    throw e;
    }
    return flag;
    }
    
    #endregion

    2.解压zip格式的文件,可以完全有C#代码进行实现

    #region 解压zip文件
    
    //zipFilePath:解压文件的绝对路径,ZipedFolder要解压到的文件夹的路径,Password解压文件的密码,可为null
    public static bool ZipToFile(string zipFilePath, string ZipedFolder, string Password)
    {
    if (!File.Exists(zipFilePath))
    {
    return false;
    }
    
    if (!Directory.Exists(ZipedFolder))
    {
    Directory.CreateDirectory(ZipedFolder);
    }
    
    ZipInputStream s = null;
    ZipEntry theEntry = null;
    
    string fileName;
    FileStream streamWriter = null;
    try
    {
    s = new ZipInputStream(File.OpenRead(zipFilePath));
    s.Password = Password;
    while ((theEntry = s.GetNextEntry()) != null)
    {
    if (theEntry.Name != String.Empty)
    {
    fileName = Path.Combine(ZipedFolder, theEntry.Name);
    ///判断文件路径是否是文件夹
    if (fileName.EndsWith("/") || fileName.EndsWith("\"))
    {
    Directory.CreateDirectory(fileName);
    continue;
    }
    try
    {
    streamWriter = File.Create(fileName);
    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;
    }
    }
    }
    catch { }
    }
    }
    return true;
    }
    catch { return false; }
    finally
    {
    if (streamWriter != null)
    {
    streamWriter.Close();
    streamWriter = null;
    }
    if (theEntry != null)
    {
    theEntry = null;
    }
    if (s != null)
    {
    s.Close();
    s = null;
    }
    GC.Collect();
    GC.Collect(1);
    }
    
    }
    #endregion
  • 相关阅读:
    Css进阶
    Css布局
    遇到的小问题
    MySQL 8.017连接Navicat中出现的问题
    ConcurrentHashMap图文源码解析
    HashMap图文源码解析
    接口和抽象类
    dependencies 和 devDependencies
    2020.7.7第二天
    2020.7.6第一天
  • 原文地址:https://www.cnblogs.com/wangchengshen/p/3595931.html
Copyright © 2020-2023  润新知