The full code for the zip application is in the ActiveX Demo.zip file included with this newsletter and it's about 700 lines long so we'll just cover the major points here. For starters let's outline what the program needs to do, and decide what controls we will use.
At a minimum we need to:
Create new zip files.
Open a zip file.
Display the contents in detail.
Add one or more files to the archive at one time, with the option to preserve the relative path.
Delete one or more files from the archive at one time.
Extract the files from the archive to a folder of choice.
Items 1 and 2 can be handled with a standard filedialog. A new zip file is created by adding a file to a zip archive and specifying the path for the new zip file. If the zip file does not exist then XZip creates it. Items 3 and 5 suggest that the display be a multi-column and multi-row control with the ability to select more than one row at a time. The best choice for this would be a listview control. Item 4 can be accomplished with a multi-select file dialog, and a small dialog window can handle the 'preserve paths' option. Item 6 requires a folder to be chosen so we will use a Shell BrowseForFolder for that task. Listviews and multi-select file dialogs are not native to LB, so that means do it yourself. The code for these, as well as other items, is based entirely or in part on code from the Liberty BASIC 4 Companion and LB Workshop, reprinted with permission. Both are available at Alyces Restaurant.
More XZip Routines
Part 1 covered the fundamentals of initializing COM and supplied the first methods for this zip application, but a few more methods will be needed to accomplish our minimum application goals. To begin with, we will need a few more routines for XZip. Part 1 gave the routine for "Pack", and this next sub expands that functionality to include the path. If you studied part 1 you should have no trouble with these new methods.
Sub Zip.Pack.PreservePath XZipObject, src$, zip$
'Preserves the path of the file being zipped.Calldll#com,"dhCallMethod", XZipObject AsUlong,".Pack(%s, %s, %d)"AsPtr,_
src$ AsPtr, zip$ AsPtr,1AsLong, r AsLongEndSub
To delete a file from the archive...
Note: if all files are deleted from the archive, the archive itself is also deleted.
Sub Zip.Delete XZipObject, file$, zip$
'Deletes file$ from zip$.Calldll#com,"dhCallMethod", XZipObject AsUlong,".Delete(%s, %s)"AsPtr,_
file$ AsPtr, zip$ AsPtr, r AsLongEndSub
This next sub uses slightly modified code from the example in part 1 to display the contents of a zip file. The results are now output to our listview control.
Sub ShowZipFiles XZipObject, hList, zipFile$
'Display a list of the files contained in the zip file'along with their uncompressed size and any stored path.'Display the files in a listview control.
tFolder =1
tFile =2Call ListView.DeleteAllItems hList
Calldll#com,"dhGetValue","%o"AsPtr, comObj AsStruct, _
XZipObject AsUlong,".Contents(%s)"AsPtr, zipFile$ AsPtr, r AsLong
objItems = comObj.obj.struct: comObj.obj.struct=0
count = GetValueLong(objItems,".Count")For Idx =1To count
Calldll#com,"dhGetValue","%o"AsPtr, comObj AsStruct, _
objItems AsUlong,".Item(%d)"AsPtr, Idx AsLong, r AsLong
objItem = comObj.obj.struct: comObj.obj.struct=0If GetValueLong(objItem,".Type")= tFile Then
listIdx = listIdx +1
fileName$ = GetValueStr$(objItem,".Name")
fileTime$ = GetValueDateTime$(objItem,".Date")
fileSize = GetValueLong(objItem,".Size")
filePath$ = GetValueStr$(objItem,".Path")
r = ListView.InsertItem(hList, listIdx-1,0, fileName$)
r = ListView.SetItem(hList, listIdx-1,1, fileTime$)
r = ListView.SetItem(hList, listIdx-1,2,Str$(fileSize))
r = ListView.SetItem(hList, listIdx-1,3, filePath$)EndIfCall SetNothing objItem
Next Idx
Call SetNothing objItems
EndSub
Occasionally we will need to know how many files are in the archive.
This next part presents the routines for the listview control and the multi-select file dialog. For in-depth explanations of these items please consult the Liberty BASIC 4 Companion. The listview and file dialog code is portable except for the function ListView.GetSelectedFiles$ which is tailored for this zip application.
The last listview function ListView.GetSelectedFiles$ and the GetOpenFileName$ function return a chr$(13) delimited string of file paths. This next function parses these strings and puts the file paths into an array.
Function ParseOFNlist(strList$, maxElements)'Fills array OFNlist$() with paths from a chr$(13) delimited string'or a single non-delimited path.
maxElements =max(maxElements,1)Redim OFNlist$(maxElements)
idx =0IfInstr(strList$,Chr$(13))ThenWhile1
OFNlist$(idx)=Word$(strList$, idx+1,Chr$(13))If OFNlist$(idx)=""ThenExitWhile
idx = idx +1If idx > maxElements ThenRedim OFNlist$(1)'erase the listExitFunction'return 0, failedEndIfWendElse
OFNlist$(0)= strList$
EndIfIf OFNlist$(0)<>""Then ParseOFNlist =1'return successEndFunction
That covers the XZip methods and the custom widgets, excluding the BrowseForFolder. The rest of the code is simple enough. Now you have a free zip application that you can personalize to suit yourself, and the tools to use COM and ActiveX dlls in your Liberty BASIC programs. Don't forget that XZip.dll must be registered on your system for this to work.
Using ActiveX DLLs in Liberty BASIC
Part 2Originally published in NL 131, 2005
-
Using ActiveX DLLs in Liberty BASIC | More XZip Routines | Listview and File Dialog
You did read part 1, didn't you? In part 2 we'll add some more XZip methods and tie everything together in a simple zip archiver application.
Download files here.
The full code for the zip application is in the ActiveX Demo.zip file included with this newsletter and it's about 700 lines long so we'll just cover the major points here. For starters let's outline what the program needs to do, and decide what controls we will use.
At a minimum we need to:
Items 1 and 2 can be handled with a standard filedialog. A new zip file is created by adding a file to a zip archive and specifying the path for the new zip file. If the zip file does not exist then XZip creates it. Items 3 and 5 suggest that the display be a multi-column and multi-row control with the ability to select more than one row at a time. The best choice for this would be a listview control. Item 4 can be accomplished with a multi-select file dialog, and a small dialog window can handle the 'preserve paths' option. Item 6 requires a folder to be chosen so we will use a Shell BrowseForFolder for that task. Listviews and multi-select file dialogs are not native to LB, so that means do it yourself. The code for these, as well as other items, is based entirely or in part on code from the Liberty BASIC 4 Companion and LB Workshop, reprinted with permission. Both are available at Alyces Restaurant.
More XZip Routines
Part 1 covered the fundamentals of initializing COM and supplied the first methods for this zip application, but a few more methods will be needed to accomplish our minimum application goals. To begin with, we will need a few more routines for XZip. Part 1 gave the routine for "Pack", and this next sub expands that functionality to include the path. If you studied part 1 you should have no trouble with these new methods.To delete a file from the archive...
Note: if all files are deleted from the archive, the archive itself is also deleted.
This next sub uses slightly modified code from the example in part 1 to display the contents of a zip file. The results are now output to our listview control.
Occasionally we will need to know how many files are in the archive.
Listview and File Dialog
This next part presents the routines for the listview control and the multi-select file dialog. For in-depth explanations of these items please consult the Liberty BASIC 4 Companion. The listview and file dialog code is portable except for the function ListView.GetSelectedFiles$ which is tailored for this zip application.
The last listview function ListView.GetSelectedFiles$ and the GetOpenFileName$ function return a chr$(13) delimited string of file paths. This next function parses these strings and puts the file paths into an array.
That covers the XZip methods and the custom widgets, excluding the BrowseForFolder. The rest of the code is simple enough. Now you have a free zip application that you can personalize to suit yourself, and the tools to use COM and ActiveX dlls in your Liberty BASIC programs. Don't forget that XZip.dll must be registered on your system for this to work.
Enjoy.