欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#调用动态unlha32.dll解压Lha后缀的打包文件分享

程序员文章站 2024-02-28 12:23:52
复制代码 代码如下:public class lhautity    {      ...

复制代码 代码如下:

public class lhautity
    {
        ///取得dll的版本
        [dllimport("unlha32")]
        private static extern uint16 unlhagetversion();

        /// <summary>
        /// '取得dll的执行情况
        /// </summary>
        /// <returns>是否成功</returns>
        [dllimport("unlha32")]
        private static extern  boolean unlhagetrunning();

        /// <summary>
        /// '文件检查
        /// </summary>
        /// <param name="szfilename"></param>
        /// <param name="imode"></param>
        /// <returns></returns>
        [dllimport("unlha32")]
        private static extern boolean unlhacheckarchive(string szfilename, int32 imode);

        /// <summary>
        /// 文件解压缩
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="szcmdline"></param>
        /// <param name="szoutput"></param>
        /// <param name="dwsize"></param>
        /// <returns></returns>
        [dllimport("unlha32")]
        private static extern int unlha(int hwnd, string szcmdline, string szoutput, int dwsize);

        /// <summary>
        /// 需要解压的文件
        /// </summary>
        /// <param name="archivefile">解压文件路径</param>
        /// <param name="extractdir">解压到路径</param>
        /// <param name="isdeletefile">是否删除</param>
        public static bool uncompress(string archivefile, string extractdir,bool  isdeletefile)
        {
            string extractfullpath = string.empty;
            string startpath = appdomain.currentdomain.basedirectory;

            if (!system.io.file.exists(archivefile))
            {
                //判断需要解压的文件存不存
                throw new exception(string.format("需要解压的{0}不存在", archivefile));
            }

            try
            {
                uint16 ver = lhautity.unlhagetversion();
            }
            catch (exception ex)
            {
                throw new exception("没找到unlha32.dll文件");
            }

            if (unlhagetrunning())
            {
                throw new exception("dll正在执行");
            }


            if (!unlhacheckarchive(archivefile, 0))
            {
                throw new exception("文件不能被解压缩");
            }

            //解压的路径
            if (string.isnullorempty(extractdir))
            {
                extractfullpath =string.format(@"{0}{1}\", startpath,archivefile.substring(archivefile.lastindexof("\\")+1,archivefile.indexof(".lha")-1-archivefile.lastindexof("\\")));
            }
            else
            {
                extractfullpath = extractdir;
            }

            if (!system.io.directory.exists(extractfullpath))
            {
                system.io.directory.createdirectory(extractfullpath);
            }
              


            int ret = unlha(0, string.format("x \"{0}\" \"{1}\"", archivefile, extractfullpath), null, 0);

            if (ret != 0)
            {


                if (ret == 32800)
                {
                    throw new exception("文件解压缩取消");
                }
                else
                {
                    throw new exception("文件解压缩异常结束");

                }

            }
            else
            {
                if (isdeletefile)
                {
                    system.io.file.delete(archivefile);
                }

                return true;
            }


        }

}


项目中需要到解压这类型的文件,无从下手,上网看资料发现是日本常用的压缩算法之一,

查了很多资料,都没有好的办法解包,

后来找到这个dll可以解包的

但是网上的代码都是vb或者c的

唯有自己写成c#版本的,其实即使c#调用动态链接库

先到网上下载这个dll,然后把这个dll放到c:\windows\system32目录下