Solved

Unrar Dll Converstion to Delphi.

Posted on 1998-10-13
17
1,152 Views
Last Modified: 2012-08-13
I have been banging my head against the wall with this one dll, I cannot get anything to work right. all i want to do is.

 #1. Extract rar archives.
 #2. When Extracing a rar archive or many rar archives to show Files being extracted and the name of the archive that it is  being extracted from.
 #3. Use Progress bar to show percent down on total extraction completed.

 I only want delphi native code with a example please, i don't want any componets like Rararc because they just don't work with long filenames over 6 spaces. unless there is a component on the web that is free with source.
i have included the Unrardll documentation below, I can also provide this documentaion by email if you wish.


    UNRAR.DLL is a 32-bit Windows dynamic-link library which provides file
 extraction from RAR archives. It handles archives created with RAR 2.0
 only and cannot extract files from archives of previous versions of RAR.


    Exported functions

====================================================================
HANDLE PASCAL RAROpenArchive(struct RAROpenArchiveData *ArchiveData)
====================================================================

Description
~~~~~~~~~~~
  Open RAR archive and allocate memory structures (about 1 Mb)

Parameters
~~~~~~~~~~
ArchiveData       Points to RAROpenArchiveData structure

struct RAROpenArchiveData
{
  char *ArcName;
  UINT OpenMode;
  UINT OpenResult;
  char *CmtBuf;
  UINT CmtBufSize;
  UINT CmtSize;
  UINT CmtState;
};

Structure fields:

ArcName
  Input parameter which should point to a, zero terminated string
  containing the archive name.

OpenMode
  Input parameter.

  Possible values

  RAR_OM_LIST           Open archive for reading file headers only
  RAR_OM_EXTRACT        Open archive for testing and extracting files

OpenResult
  Output parameter.

  Possible values

  0                     Success
  ERAR_NO_MEMORY        Not enough memory to initialize data structures
  ERAR_BAD_DATA         Archive header broken
  ERAR_BAD_ARCHIVE      File is not valid RAR archive
  ERAR_EOPEN            File open error

CmtBuf
  Input parameter which should point to the buffer for archive
  comments. Maximum comment size is limited to 64Kb. Comment text is
  zero terminated. If the comment text is larger than the buffer
  size, the comment text will be truncated. If CmtBuf is set to
  NULL, comments will not be read.

CmtBufSize
  Input parameter which should contain size of buffer for archive
  comments.

CmtSize
  Output parameter containing size of comments actually read into the
  buffer, cannot exceed CmtBufSize.

CmtState
  Output parameter.

  Possible values

  0                     comments not present
  1                     Comments read completely
  ERAR_NO_MEMORY        Not enough memory to extract comments
  ERAR_BAD_DATA         Broken comment
  ERAR_UNKNOWN_FORMAT   Unknown comment format
  ERAR_SMALL_BUF        Buffer too small, comments not completely read

Return values
~~~~~~~~~~~~~
  Archive handle or NULL in case of error


====================================================================
int PASCAL RARCloseArchive(HANDLE hArcData)
====================================================================

Description
~~~~~~~~~~~
  Close RAR archive and release allocated memory. It must be called when
  archive processing is finished, even if the archive processing was stopped
  due to an error.

Parameters
~~~~~~~~~~
hArcData
  This parameter should contain the archive handle obtained from the
  RAROpenArchive function call.

Return values
~~~~~~~~~~~~~
  0                     Success
  ERAR_ECLOSE           Archive close error


====================================================================
int PASCAL RARReadHeader(HANDLE hArcData,
                         struct RARHeaderData *HeaderData)
====================================================================

Description
~~~~~~~~~~~
  Read header of file in archive.

Parameters
~~~~~~~~~~
hArcData
  This parameter should contain the archive handle obtained from the
  RAROpenArchive function call.

HeaderData
  It should point to RARHeaderData structure:

struct RARHeaderData
{
  char ArcName[260];
  char FileName[260];
  UINT Flags;
  UINT PackSize;
  UINT UnpSize;
  UINT HostOS;
  UINT FileCRC;
  UINT FileTime;
  UINT UnpVer;
  UINT Method;
  UINT FileAttr;
  char *CmtBuf;
  UINT CmtBufSize;
  UINT CmtSize;
  UINT CmtState;
};

Structure fields:

ArcName
  Output parameter which contains a zero terminated string of the
  current archive name.  May be used to determine the current volume
  name.

FileName
  Output parameter which contains a zero terminated string of the
  file name.

Flags
  Output parameter which contains file flags:

  0x01 - file continued from previous volume
  0x02 - file continued on next volume
  0x04 - file encrypted with password
  0x08 - file comment present
  0x10 - compression of previous files is used (solid flag)

  bits 7 6 5

       0 0 0    - dictionary size   64 Kb
       0 0 1    - dictionary size  128 Kb
       0 1 0    - dictionary size  256 Kb
       0 1 1    - dictionary size  512 Kb
       1 0 0    - dictionary size 1024 Kb
       1 0 1    - reserved
       1 1 0    - reserved
       1 1 1    - file is directory

  Other bits are reserved.

PackSize
  Output parameter means packed file size or size of the
  file part if file was split between volumes.

UnpSize
  Output parameter - unpacked file size.

HostOS
  Output parameter - operating system used for archiving:

  0 - MS DOS;
  1 - OS/2.
  2 - Win32
  3 - Unix

FileCRC
  Output parameter which contains unpacked file CRC. It should not be
  used for file parts which were split between volumes.

FileTime
  Output parameter - contains date and time in standard MS DOS format.

UnpVer
  Output parameter - RAR version needed to extract file.
  It is encoded as 10 * Major version + minor version.

Method
  Output parameter - packing method.

FileAttr
  Output parameter - file attributes.

CmtBuf
  Input parameter which should point to the buffer for archive
  comments. Maximum comment size is limited to 64Kb. Comment text is
  a zero terminated string.  If the comment text is larger than the
  buffer size, the comment text will be truncated. If CmtBuf is set
  to NULL, comments will not be read.

CmtBufSize
  Input parameter which should contain size of buffer for archive
  comments.

CmtSize
  Output parameter containing size of comments actually read into the
  buffer, should not exceed CmtBufSize.

CmtState
  Output parameter.

  Possible values

  0                     Absent comments
  1                     Comments read completely
  ERAR_NO_MEMORY        Not enough memory to extract comments
  ERAR_BAD_DATA         Broken comment
  ERAR_UNKNOWN_FORMAT   Unknown comment format
  ERAR_SMALL_BUF        Buffer too small, comments not completely read

Return values
~~~~~~~~~~~~~

  0                     Success
  ERAR_END_ARCHIVE      End of archive
  ERAR_BAD_DATA         File header broken


====================================================================
int PASCAL RARProcessFile(HANDLE hArcData,
                          int Operation,
                          char *DestPath,
                          char *DestName)
====================================================================

Description
~~~~~~~~~~~
  Performs action and moves the current position in the archive to
  the next file. Extract or test the current file from the archive
  opened in RAR_OM_EXTRACT mode. If the mode RAR_OM_LIST is set,
  then a call to this function will simply skip the archive position
  to the next file.

Parameters
~~~~~~~~~~
hArcData
  This parameter should contain the archive handle obtained from the
  RAROpenArchive function call.

Operation
  File operation.

  Possible values

  RAR_SKIP              Move to the next file in the archive. If the
                        archive is solid and RAR_OM_EXTRACT mode was set
                        when the archive was opened, the current file will
                        be processed - the operation will be performed
                        slower than a simple seek.

  RAR_TEST              Test the current file and move to the next file in
                        the archive. If the archive was opened with
                        RAR_OM_LIST mode, the operation is equal to
                        RAR_SKIP.

  RAR_EXTRACT           Extract the current file and move to the next file.
                        If the archive was opened with RAR_OM_LIST mode,
                        the operation is equal to RAR_SKIP.


DestPath
  This parameter should point to a zero terminated string containing the
  destination directory to which to extract files to. If DestPath is equal
  to NULL it means extract to the current directory. This parameter has
  meaning only if DestName is NULL.

DestName
  This parameter should point to a string containing the full path and name
  of the file to be extracted or NULL as default. If DestName is defined
  (not NULL) it overrides the original file name saved in the archive and
  DestPath setting.

Return values
~~~~~~~~~~~~~
  0                     Success
  ERAR_BAD_DATA         File CRC error
  ERAR_BAD_ARCHIVE      Volume is not valid RAR archive
  ERAR_UNKNOWN_FORMAT   Unknown archive format
  ERAR_EOPEN            Volume open error
  ERAR_ECREATE          File create error
  ERAR_ECLOSE           File close error
  ERAR_EREAD            Read error
  ERAR_EWRITE           Write error


====================================================================
void PASCAL RARSetChangeVolProc(HANDLE hArcData,
            int (*ChangeVolProc)(char *ArcName,int Mode));
====================================================================

Description
~~~~~~~~~~~
  Set a user-defined function to process volume changing.

Parameters
~~~~~~~~~~
hArcData
  This parameter should contain the archive handle obtained from the
  RAROpenArchive function call.

ChangeVolProc
  It should point to a user-defined "volume change processing" function.

  The function will be passed two parameters:

  ArcName                Points to the zero terminated name
                         of the next volume.

  Mode                   The function call mode.

    Possible values

    RAR_VOL_ASK          Required volume is absent. The function should
                         prompt user and return a non-zero value
                         to retry or return a zero value to terminate
                         operation. The function may also specify a new
                         volume name, placing it to the ArcName parameter.

    RAR_VOL_NOTIFY       Required volume is successfully opened.
                         This is a notification call and ArcName
                         modification is not allowed. The function should
                         return a non-zero value to continue or a zero
                         value to terminate operation.

  Other functions of UNRAR.DLL should not be called from the
  ChangeVolProc function.

Return values
~~~~~~~~~~~~~
  None


====================================================================
void PASCAL RARSetProcessDataProc(HANDLE hArcData,
            int (*ProcessDataProc)(unsigned char *Addr,int Size))
====================================================================

Description
~~~~~~~~~~~
  Set a user-defined function to process unpacked data. It may be used
  to read a file while it is being extracted or tested without actual
  extracting file to disk.

Parameters
~~~~~~~~~~
hArcData
  This parameter should contain the archive handle obtained from the
  RAROpenArchive function call.

ProcessDataProc
  It should point to user-defined "data processing" function.

  The function is called each time when the next data portion is unpacked.
  It will be passed two parameters.

  Addr                  The address pointing to the unpacked data. The
                        function may refer to the data but must not change
                        it.

  Size                  The size of the unpacked data. It is guaranteed only
                        that the size will not exceed 1 Mb (1048576 bytes).
                        Any other presumptions may not be correct for
                        future implementations of UNRAR.DLL.

  The ProcessDataProc function should return a non-zero value to continue
  process or a zero value to cancel the archive operation.

  Other functions of UNRAR.DLL should not be called from the
  ProcessDataProc function.

Return values
~~~~~~~~~~~~~
  None


====================================================================
void PASCAL RARSetPassword(HANDLE hArcData,
                           char *Password);
====================================================================

Description
~~~~~~~~~~~
  Set a password to decrypt files.

Parameters
~~~~~~~~~~
hArcData
  This parameter should contain the archive handle obtained from the
  RAROpenArchive function call.

Password
  It should point to a string containing a zero terminated password.

Return values
~~~~~~~~~~~~~
  None


 


         
0
Comment
Question by:hyperion66
  • 11
  • 4
  • 2
17 Comments
 

Author Comment

by:hyperion66
ID: 1342715
Sorry for my spelling and bad english.  hyperion66.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1342716
hi, hyper!

would you mind to send me the dll & the docu so that i can test this?
andreas.naguschewski@vt.siemens.de
let's see if i can figure the one or other point out.

cheerio,

Black Death.
0
 

Author Comment

by:hyperion66
ID: 1342717
Hi BlackDeath.
 mail has been sent and you should get it soon. Hyperion66.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1342718
i c what i can do. so long,

Black Death.
0
 

Author Comment

by:hyperion66
ID: 1342719
Hi BlackDeath.
 mail has been sent and you should get it soon. Hyperion66.
0
 

Author Comment

by:hyperion66
ID: 1342720
hmm sorry for the double post don't know how i managed that.
0
 
LVL 7

Accepted Solution

by:
BlackMan earned 500 total points
ID: 1342721
It is allready done! There is a free library, including full Delphi source, that encapsulates the unrar.dll-file.
Check http://www.monisys.ca/~bclarke/Files/rar32.zip
0
 

Author Comment

by:hyperion66
ID: 1342722
Hi Blackdeath

Ok i'll check it out and talk to you later! thank's.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:hyperion66
ID: 1342723
Sorry I ment BlackMan i keep typing blackDeath for some stupid reason. Hyperion66 =)
0
 

Author Comment

by:hyperion66
ID: 1342724
Hello blackMan.

 i can extract Archives but i cannot get unrar to show percent done by progress bar while extracting files from archives like winrar and i know it can be done but how its done that another question. i'll email you a unrar component i was up allnight fixing bugs and errors, I have it extracting archives better then the components I have found.. if you can figure out how to show rar file total percent donw with the use of a progressbar  and with the archive name its on thats all i want everything else works great.! i
0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1342725
Hi hyperion66,

Are you confusing me with BlackDeath :-?
Anyway, you can email it to me at black@mail1.stofanet.dk
0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1342726
Just looked at it again.. You'll have to make your own progress bar and the way you do it, is but taking the unpacked filesize from the headerdata and then increasing the progressbar for every block you read from the packed file. The ProcessDataProc is called for every block, so it is here, you'll modify the progressbar.
I've made the modifications to the code from the included example..

function ProcessDataProc(Addr:PChar; Size:integer):integer;
<snip>
->  FileCurrentSize := FileCurrentSize + Size;
// The percentage is calculated as FileCurrentSize / FileActualSize


procedure TForm1.ExtractArchive(ArcName:pchar; Mode:integer);
<snip>
  RHCode:=RARReadHeader(hArcData,HeaderData);
  while RHCode=0 do
  begin
    s:=HeaderData.FileName;
    s:=trim(s);
    case Mode of
       CEXTRACT: maslin('Extracting '+s);
       CTEST:    maslin('Testing    '+s);
       CPRINT:   maslin('Printing   '+s);
    end;

->    FileActualSize:=HeaderData.UnpSize;
->    FileCurrentSize:=0;
    PFCode:=RARProcessFile(hArcData,ModoOP,NiL,NiL);

    if PFCode<>0 then
       begin
        OutProcessFileError(PFCode);
        exit;
       end;

    maslin(' Ok');
    RHCode:=RARReadHeader(hArcData,HeaderData);
  end;


0
 

Author Comment

by:hyperion66
ID: 1342727
Hi BlackMan and yes i got confused due to the fact i had no sleep in two days and was'nt thinking clearly.. sorry guy's =) hyperion66 and i'll send you a copy of the component.
0
 

Author Comment

by:hyperion66
ID: 1342728
Hello BlackDeath & BlackMan.

  There is just one problem left to fix everything else works.
when putting to labels on a form to show files and file its extracting from in the code below " Label2.Caption:=EFile.ArcName;" when useing extracting from directories using more then 4 spaces i see carp like this "~#@"
i don't know why is does this any ideas? on a fix for this?

hyperion66
       
procedure TForm1.RARExtracting(Sender: TObject; eFile: RARHeaderData);
begin
ProgressBar.MinValue:=0;
ProgressBar.Progress:=0;
ProgressBar.MaxValue:=eFile.UnpSize;
Label1.Caption:=eFile.FileName;
Label2.Caption:=eFile.ArcName;
Application.ProcessMessages;
end;  
0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1342729
I'm not quite sure I understand what you mean.. Is it when the directoryname stored with the file contains more than 4 spaces you get garbage?
Can you give an example of what you are expecting and what you are getting...
0
 

Author Comment

by:hyperion66
ID: 1342730
I get garbage when the ExeFile(Unrar progarm) is in a directory that has longfilenames, even when i try to take LongFileNames out, it still happens i get " *&%* " when
Label1.caption := EFile.ArcName; and the ExeFile(Unrar progarm) is in a directory that has LongFileNames its weird because it happens once and awhile, I'm going to try some other things and i'll let you know what i find. hyperion66.
0
 

Author Comment

by:hyperion66
ID: 1342731
What i want to do is simple this.

 #1 have a label on a form when extracting rar archives the caption show's

Label.Caption := ExtractFileName("Blahfilename: rar filename here")); but i get garbage sometimes like explain on the above comment.  Hyperion66.

 

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 117
HTML text in the body of an email (delphi code) 12 74
Magic Software info 18 105
How to debug For loops? 3 34
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now