C # call Dynamic Unlha32.dll decompression lha suffix Packaging file sharing _ Practical Tips

Source: Internet
Author: User

Copy Code code as follows:

public class Lhautity
{
Get version of DLL
[DllImport ("Unlha32")]
private static extern UInt16 unlhagetversion ();

<summary>
' Get the implementation of the DLL
</summary>
<returns> Success </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 unzipped
       ///</summary>
        ///<param name= "Archivefile" > Extract file path </param>
        ///<param name= "Extractdir" > Extract to Path </param>
       /// <param name= "Isdeletefile" > Delete </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))
            {
               //Judge the files that need to be extracted cannot be saved
                throw New Exception ( String. Format ("{0} that needs to be decompressed does not exist", archivefile));
           }

            Try
             {
                 UInt16 ver = lhautity.unlhagetversion ();
           }
            catch (Exception ex)
             {
                 throw new Exception ("Unlha32.dll File not Found");
           }

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


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

           //unzipped path
             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 ("File decompression cancellation");
}
Else
{
throw new Exception ("File decompression abnormal end");

}

}
Else
{
if (isdeletefile)
{
System.IO.File.Delete (Archivefile);
}

return true;
}


}

}


The project needs to extract this type of file, do not know, the Internet to see the data found is a common Japanese compression algorithm,

Look up a lot of information, there is no good solution to the package,

Later found this DLL can unpack the

But the code on the Web is VB or C.

Only a C # version of itself, in fact, even if C # calls the dynamic link library

Download the DLL first, and then put the DLL in the C:\Windows\System32 directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.