zoukankan      html  css  js  c++  java
  • 实现在线压缩文件的实现程序代码

    [引入using System.Diagnostics;using Microsoft.Win32;]
    #region 使用GZip压缩文件,返回bool值
    /// <param name="FileSource">要压缩的源文件名</param>
    /// <param name="FileTarget">压缩后的文件名</param>
    /// <returns>压缩后的成功类型</returns>
    public static bool GZipFile(string FileSource, string FileTarget)
    {
    byte[] myByte = null;
    FileStream myStream = null;
    FileStream myDesStream = null;
    GZipStream myComStream = null;
    try
    {
    myStream = new FileStream(FileSource, FileMode.Open, FileAccess.Read, FileShare.Read);
    myByte = new byte[myStream.Length];
    myStream.Read(myByte, 0, myByte.Length);
    myDesStream = new FileStream(FileTarget, FileMode.OpenOrCreate, FileAccess.Write);
    myComStream = new GZipStream(myDesStream, CompressionMode.Compress, true);
    myComStream.Write(myByte, 0, myByte.Length);
    return true;
    }
    catch(Exception ex)
    {
    exceptionMessage = ex.Message;
    return false;
    }
    finally
    {
    myStream.Close();
    myComStream.Close();
    myDesStream.Close();
    }

    }
    #endregion

    #region 使用GZip解压文件,返回bool值
    /// <summary>
    /// 使用GZip解压文件,返回bool值
    /// </summary>
    /// <param name="FileSource">要解压的源文件名</param>
    /// <param name="FileTarget">要解压的文件名</param>
    /// <returns>解压的成功类型</returns>

    public static bool GZipSFile(string FileSource, string FileTarget)
    {
    byte[] myByte = null;
    FileStream myStream = null;
    FileStream myDesStream = null;
    GZipStream myDeComStream = null;
    try
    {
    myStream = new FileStream(FileSource, FileMode.Open);
    myDeComStream = new GZipStream(myStream, CompressionMode.Decompress, true);
    myByte = new byte[4];
    int myPosition = (int)myStream.Length - 4;
    myStream.Position = myPosition;
    myStream.Read(myByte, 0, 4);
    myStream.Position = 0;
    int myLength = BitConverter.ToInt32(myByte, 0);
    byte[] myData = new byte[myLength + 100];
    int myOffset = 0;
    int myTotal = 0;
    while (true)
    {
    int myBytesRead = myDeComStream.Read(myData, myOffset, 100);
    if (myBytesRead == 0)
    break;
    myOffset += myBytesRead;
    myTotal += myBytesRead;
    }
    myDesStream = new FileStream(FileTarget, FileMode.Create);
    myDesStream.Write(myData, 0, myTotal);
    myDesStream.Flush();
    return true;
    }
    catch(Exception ex)
    {
    exceptionMessage = ex.Message;
    return false;
    }
    finally
    {
    myStream.Close();
    myDeComStream.Close();
    myDesStream.Close();
    }
    }
    #endregion

    [目标机器上必须装有winrar压缩软件]
    #region 使用WinRAR压缩文件
    /// <summary>
    /// 使用WinRAR压缩文件
    /// </summary>
    /// <param name="FileSource">要压缩的源文件名</param>
    /// <param name="FileTarget">压缩后的文件名</param>
    /// <returns>压缩的成功类型</returns>
    public static bool RarFile(string FileSource, string FileTarget)
    {
    String myRar;
    RegistryKey myReg;
    Object myObj;
    String myInfo;
    ProcessStartInfo myStartInfo;
    Process myProcess;
    try
    {
    myReg = Registry.ClassesRoot.OpenSubKey("Applications\WinRAR.exe\Shell\Open\Command");
    myObj = myReg.GetValue("");
    myRar = myObj.ToString();
    myReg.Close();
    myRar = myRar.Substring(1, myRar.Length - 7);
    myInfo = " a " + FileTarget + " " + FileSource;
    myStartInfo = new ProcessStartInfo();
    myStartInfo.FileName = myRar;
    myStartInfo.Arguments = myInfo;
    myStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    myProcess = new Process();
    myProcess.StartInfo = myStartInfo;
    myProcess.Start();
    return true;
    }
    catch(Exception ex)
    {
    exceptionMessage = ex.Message;
    return false;
    }
    }
    #endregion

    #region 使用WinRAR解压文件
    /// <summary>
    /// 使用WinRAR解压文件
    /// </summary>
    /// <param name="FileSource">要解压的源文件名</param>
    /// <param name="FileTarget">解压后的文件名</param>
    /// <returns>解压的成功类型</returns>

    public static bool RarSFile(string FileSource, string FileTarget)
    {
    String myRar;
    RegistryKey myReg;
    Object myObj;
    String myInfo;
    ProcessStartInfo myStartInfo;
    Process myProcess;
    try
    {
    myReg = Registry.ClassesRoot.OpenSubKey("Applications\WinRar.exe\Shell\Open\Command");
    myObj = myReg.GetValue("");
    myRar = myObj.ToString();
    myReg.Close();
    myRar = myRar.Substring(1, myRar.Length - 7);
    myInfo = " X " + FileSource+ " " + FileTarget + "\";
    myStartInfo = new ProcessStartInfo();
    myStartInfo.FileName = myRar;
    myStartInfo.Arguments = myInfo;
    myStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    myProcess = new Process();
    myProcess.StartInfo = myStartInfo;
    myProcess.Start();
    return true;
    }
    catch(Exception ex)
    {
    exceptionMessage = ex.Message;
    return false;
    }
    }
    #endregion

    转载原文:http://www.zhixing123.cn/net/16278.html

  • 相关阅读:
    统计八连块
    linux-shell编程-添加用户并设置权限
    chrome 的网站测试工具
    windows10安装自带的ubuntu子系统
    开源项目阅读笔记--appium+adb
    TODO 软件质量模型--理论
    java -static的特性和使用,静态类/方法/块/内部类/回收机制
    TODO 竞品分析方法——关于导航评测的一些笔记
    mock工具:mock.js 和vscode faker,moco
    移动App性能评测与优化-Android内存测试 ,DVM原理
  • 原文地址:https://www.cnblogs.com/lizihong/p/4322812.html
Copyright © 2011-2022 走看看