SoFunction
Updated on 2025-03-07

C# calls dynamically decompressing the packaged file sharing of Lha suffix


public class LhaUtity
    {
///Get the version of the DLL
        [DllImport("unlha32")]
        private static extern UInt16 UnlhaGetVersion();

        /// <summary>
/// 'Get the execution status of the DLL
        /// </summary>
/// <returns>Is it successful</returns>
        [DllImport("unlha32")]
        private static extern  Boolean UnlhaGetRunning();

        /// <summary>
/// 'File check
        /// </summary>
        /// <param name="szFileName"></param>
        /// <param name="iMode"></param>
        /// <returns></returns>
        [DllImport("unlha32")]
        private static extern Boolean UnlhaCheckArchive(String szFileName, Int32 iMode);

        /// <summary>
/// File decompression
        /// </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>
/// Files that need to be decompressed
        /// </summary>
/// <param name="archiveFile">Decompress file path</param>
/// <param name="extractDir">Decompress to path</param>
/// <param name="isDeleteFile">Does it be deleted</param>
        public static bool UnCompress(string archiveFile, string extractDir,bool  isDeleteFile)
        {
            string extractFullPath = ;
            string startPath = ;

            if (!(archiveFile))
            {
//Judge whether the file that needs to be decompressed is stored
throw new Exception(("The {0} that needs to be decompressed does not exist", archiveFile));
            }

            try
            {
                UInt16 ver = ();
            }
            catch (Exception ex)
            {
throw new Exception("File not found");
            }

            if (UnlhaGetRunning())
            {
throw new Exception("DLL is executing");
            }


            if (!UnlhaCheckArchive(archiveFile, 0))
            {
throw new Exception("File cannot be decompressed");
            }

//The path to decompression
            if ((extractDir))
            {
                extractFullPath =(@"{0}{1}\", startPath,(("\\")+1,(".lha")-("\\")));
            }
            else
            {
                extractFullPath = extractDir;
            }

            if (!(extractFullPath))
            {
                (extractFullPath);
            }
              


            int ret = Unlha(0, ("x \"{0}\" \"{1}\"", archiveFile, extractFullPath), null, 0);

            if (ret != 0)
            {


                if (ret == 32800)
                {
throw new Exception("File decompression cancel");
                }
                else
                {
throw new Exception("File decompression exception ends");

                }

            }
            else
            {
                if (isDeleteFile)
                {
                    (archiveFile);
                }

                return true;
            }


        }

}