C # Call dynamic unlha32.dll to decompress the package file with the Lha suffix

Source: Internet
Author: User

Copy codeThe Code is as follows:
Public class LhaUtity
{
/// Obtain the DLL version
[DllImport ("unlha32")]
Private static extern UInt16 UnlhaGetVersion ();

/// <Summary>
/// 'Obtain the DLL execution status
/// </Summary>
/// <Returns> 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>
/// Decompress the file
/// </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 sz1_line, string szOutput, int dwSize );

/// <Summary>
/// The file to be decompressed
/// </Summary>
/// <Param name = "archiveFile"> decompress the file path </param>
/// <Param name = "extractDir"> extract the package to the path </param>
/// <Param name = "isDeleteFile"> whether to 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 ))
{
// Determine whether the files to be decompressed are saved
Throw new Exception (string. Format ("the {0} 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 is being executed ");
}


If (! UnlhaCheckArchive (archiveFile, 0 ))
{
Throw new Exception ("files cannot be decompressed ");
}

// Decompressed 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 ("unzipping an object ");
}
Else
{
Throw new Exception ("file decompression ends abnormally ");

}

}
Else
{
If (isDeleteFile)
{
System. IO. File. Delete (archiveFile );
}

Return true;
}


}

}

Files of this type need to be decompressed in the project. It is one of the common compression algorithms in Japan to view data online,

I checked a lot of information and couldn't unpack it,

Later, I found this dll to unpackage.

However, the Code on the internet is VB or C.

Only the C # version can be written by yourself. In fact, even if C # Calls the dynamic link library

First download the dll from the Internet and put it in the C: \ Windows \ System32 directory.

Related Article

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.