1 #exe = 1
2 #norun = 1
3 #gefile = 0
4 #libdir = %EXEPATH%\lib
5 #libdir1 = %EXEPATH%\..\lib\vis
6 #include = %EXEPATH%\lib\stdlib.ge
7 //#wait = 1
8 /******************************************************************************
9 *
10 * Copyright (C) 2009, The Gentee Group. All rights reserved.
11 * This file is part of the Gentee open source project - http://www.gentee.com.
12 *
13 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
14 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
15 * ACCEPTANCE OF THE AGREEMENT.
16 *
17 * Author: Alexander Antypenko ( santy )
18 *
19 ******************************************************************************/
20
21 include
22 {
23 $"unrar.g"
24 }
25
26 define
27 {
28 EXTRACT,
29 TEST,
30 PRINT
31 }
32
33 extern
34 {
35 func OutProcessFileError(int iError)
36 func ExtractArchive(str sArcName,int iMode, str strPsw)
37 }
38
39 func ShowComment(str cmtBuf)
40 {
41 print("\nComment:\n " +cmtBuf+ " \n")
42 }
43
44
45 func OutHelp()
46 {
47 print("\nUnrar_demo. This is a simple example of UNRAR.DLL usage\n")
48 print("\nSyntax:\n")
49 print("\nUnrar_demo X <Archive> <psw> extract archive contents")
50 print("\nUnrar_demo T <Archive> <psw> test archive contents")
51 print("\nUnrar_demo P <Archive> <psw> print archive contents to stdout \n\n")
52 }
53
54 func OutOpenArchiveError(int iError,str arcName)
55 {
56 switch (iError)
57 {
58 case $ERAR_NO_MEMORY: print("\nNot enough memory")
59 case $ERAR_EOPEN: print("\nCannot open " + arcName)
60 case $ERAR_BAD_ARCHIVE: print("\n "+arcName+" is not RAR archive")
61 case $ERAR_BAD_DATA: print("\n "+arcName+" : archive header broken")
62 case $ERAR_UNKNOWN: print("Unknown error")
63 }
64 }
65
66 // main programm
67
68 func unrar_demo <main>
69 {
70 str sOption = "" ,sParam2, sPassword
71 str dem1,dem2
72 //print(" \(argc()) \(argv(dem1.upper(),1)) \(argv(dem2.upper(),2))\n")
73 if (argc() != 2 )
74 {
75 OutHelp()
76 exit(0)
77 }
78 else {
79 argv(sOption,1)
80 argv(sPassword,3)
81 switch (sOption.upper())
82 {
83 case "X": ExtractArchive(argv(sParam2,2),$EXTRACT,sPassword)
84 case "T": ExtractArchive(argv(sParam2,2),$TEST,sPassword)
85 case "P": ExtractArchive(argv(sParam2,2),$PRINT,sPassword)
86 default
87 {
88 OutHelp()
89 exit(0)
90 }
91 }
92 }
93 }
94
95
96 func OutProcessFileError(int iError)
97 {
98 switch(iError)
99 {
100 case $ERAR_UNKNOWN_FORMAT: print("Unknown archive format")
101 case $ERAR_BAD_ARCHIVE: print("Bad volume")
102 case $ERAR_ECREATE: print("File create error")
103 case $ERAR_EOPEN: print("Volume open error")
104 case $ERAR_ECLOSE: print("File close error")
105 case $ERAR_EREAD: print("Read error")
106 case $ERAR_EWRITE: print("Write error")
107 case $ERAR_BAD_DATA: print("CRC error")
108 case $ERAR_UNKNOWN: print("Unknown error")
109 case $ERAR_MISSING_PASSWORD: print("Password for encrypted file is not specified")
110 }
111 }
112
113 func ExtractArchive(str sArcName,int iMode, str strPsw)
114 {
115 uint hArcData
116 int RHCode,PFCode
117 buf CmtBuf[16384]
118 str cmtBufHeader[255]
119 str sFileName,sTempStr
120 RARHeaderData HeaderData
121 RAROpenArchiveData OpenArchiveData
122
123 mzero( &OpenArchiveData, sizeof( RAROpenArchiveData ))
124 with OpenArchiveData
125 {
126 .ArcName=sArcName.ptr()
127 .CmtBuf=CmtBuf.ptr()
128 .CmtBufSize=16384
129 .OpenMode=$RAR_OM_EXTRACT
130 }
131
132 hArcData=RAROpenArchive(OpenArchiveData)
133 if (OpenArchiveData.OpenResult != 0)
134 {
135 OutOpenArchiveError(OpenArchiveData.OpenResult,sArcName)
136 return
137 }
138
139 if (OpenArchiveData.CmtState==1) : ShowComment(sTempStr.copy(CmtBuf.ptr()))
140
141 if strPsw != "" : RARSetPassword(hArcData,strPsw)
142
143 uint HeaderData_ptr = malloc(sizeof( RARHeaderData ))
144 while ((RHCode=RARReadHeader(hArcData,HeaderData_ptr)) == 0)
145 {
146 sFileName.load(HeaderData_ptr+260,260).setlenptr()
147 switch(iMode)
148 {
149 case $EXTRACT: print("\nExtracting "+sFileName)
150 case $TEST: print("\nTesting "+ sFileName )
151 case $PRINT: print("\nPrinting "+ sFileName+" \n")
152 }
153 PFCode = RARProcessFile(hArcData, ?(iMode==$EXTRACT,$RAR_EXTRACT,$RAR_TEST),"","")
154 if (PFCode == 0) : print(" Ok \n")
155 else
156 {
157 OutProcessFileError(PFCode)
158 break
159 }
160 }
161 if (RHCode == $ERAR_BAD_DATA) : print("\nFile header broken")
162 mfree(HeaderData_ptr)
163 RARCloseArchive(hArcData);
164 }
165
Редактировать