PC Desktop: Zip File Manager Part 2 Unzipping Files


Hello Everyone

At the end of our last post we stated we would look at the unzip features of our zip file manager and how they work, so lets begin.

Unzipping Files

We looked at zipping files, but truth being told the average user today does not need to zip files very often. I say this because in the past, transferring large files was difficult at best and most email programs had a five meg size limit. So in order to send photo’s or small video clips you had to zip the file. Today social media allows 20 meg files as the minimum and uploading the files is as simple as drag and drop and Grandma can watch the video on the media site. Unzipping files is however still needed. Many sites which have files for download zip the files to save on bandwidth so the user must be able to unzip those file easily.

Everyone knows there are several zip programs out there and they all have there good and bad points. It is not our intent to compete with these programs; our goal is only to provide a simple easy to use zip utility for the basic needs only. This next video shows how the unzip utility works. Watch it then come back to see the code.

As you can see from the video it is very simple to use and meets the basic needs of the user. Lets look at the code and contrast it with the several methods Alpha Software has for unzipping files. Each of the methods for unzipping files in Alpha Software work, but each has it’s own special considerations when using. and the help is not as clear as it could be. In our code I use all three examples. Look at the code then I will discuss each.

else if Var->mySelect = “UnZip-All” then
UpdateList = 4.1 / *count(listSource)
FileMgtSystemS:text1.text = “UnZipping All Files in List to:  “+Alltrim(file.filename_parse(trgfld,”NE”))
topparent:text2.Object.width = topparent:text2.Object.width + UpdateList
dim file_names2 as C
file_names2 = myFileS
unzip_filelist(file_names2,””,trgfld+chr(92) ,.f.)
listsource = “”
goto xdone
else if Var->mySelect = “UnZip-List” then
UpdateList = 4.1 / *count(listSource)
FileMgtSystemS:text1.text = “UnZipping All Files in List to:  “+Alltrim(file.filename_parse(trgfld,”NE”))
topparent:text2.Object.width = topparent:text2.Object.width + UpdateList
list2 = Listsource
topparent:text2.Object.width = .1
topparent:text2.show()
for each zfile in list2
UpdateList = 4.1 / *count(listSource)+1
FileMgtSystemS:text1.text = “Un-Zipping… “+zfile
topparent:text2.Object.width = topparent:text2.Object.width + UpdateList
list2 = Listsource
if right(Alltrim(zfile),1) = “.” then
goto continueuzs
end if
ui_yield()
FileMgtSystemS:text1.Refresh()
xbasic_wait_for_idle()
dim file_names2 as C
file_names2 = myFileS
dim unziptype as L
unziptype = *any(zfile, “/”)
dim sel_file as C
sel_file = STRTRAN(Alltrim(zfile),”/”,chr(92))
if unziptype = .t. then
file.unzip(file_names2, sel_file, trgfld)
else
unzip_files(file_names2, sel_file, trgfld)
end if
continueuzl:
next
listsource = “”
goto xdone
else if Var->mySelect = “UnZip-Selected” then
topparent:text2.Object.width = .1
topparent:text2.show()
UpdateList = 4.1 / *count(listSource)+1
FileMgtSystemS:text1.text = “Un-Zipping… “+sel_file
topparent:text2.Object.width = topparent:text2.Object.width + UpdateList
list2 = Listsource
if right(Alltrim(sel_file),1) = “.” then
goto continueuzs
end if
ui_yield()
FileMgtSystemS:text1.Refresh()
xbasic_wait_for_idle()
dim file_names2 as C
file_names2 = myFileS
dim unziptype as L
unziptype = *any(FileMgtSystemS:TagList.value, “/”)
dim sel_file as C
sel_file = STRTRAN(Alltrim(FileMgtSystemS:TagList.value),”/”,chr(92))
if unziptype = .t. then
file.unzip(file_names2, sel_file, trgfld)
else
unzip_files(file_names2, sel_file, trgfld)
end if
continueuzs:
goto xdone

File.unzip – Low level function which works fast and very well when working with files that do not have path information stored in the zip file. In the help file for Alpha Software it gives an example of how to unzip a file. In the zip file you see the following file

data/backup/Mesurements.Dbf

Next they show the sample code for extracting that file.

file.unzip(“c:\backup.zip”, “data\backup\measurements.dbf”, “c:\temp”)

In the zip file the folders in the path are seperated by a forward slash ‘/‘ and in the extract code they are seperated by a backward slash ‘\‘. If you are reading the line in the zip file and passing it to a variable then you must convert the forward slash to a backward slash. In our code we do this by using STRTRAN()

  dim unziptype as L
unziptype = *any(zfile, “/”)
dim sel_file as C
sel_file = STRTRAN(Alltrim(zfile),”/”,chr(92))
if unziptype = .t. then
file.unzip(file_names2, sel_file, trgfld)
else
unzip_files(file_names2, sel_file, trgfld)
end if

If you look at the code above you will see we first check to see if the forward slash is included in the line item and if so we use file.unzip. If not we use unzip_files. 

unzip_files – High level wrapper for file.unzip which is easier to use. Both of these functions unzip the files to the target folder and as written do not restore the original path. If you want to restore the original path of the zipped files you would use unzip_files and pass .t. to the Preserve_Paths part of the function. (see below)

   unzip_files(file_names2, sel_file, trgfld, .t.)

unzip_filelist – This function extracts all or specified files from a ZIP file to a specified directory. It automatically restores the original path and pops up an overwrite warning. Note( This function is only available from verson 6 and above.) You will notice in the unzip_filelist code we do not need to be concerned with the forward and backward slash. Alpha Software does this automatically.

 dim file_names2 as C
file_names2 = myFileS
unzip_filelist(file_names2,””,trgfld+chr(92) ,.t.,.t.)

You can also specify individual files to extract from the zip file by putting in the line value between the quotes (‘ “) in the function. Wildcards are accepted. Just remember this will unzip the selected files and restore the folder path for those files. (Special Note: in the video, I used unzip_filelist to extract selected files and list of files which restored the path for those files under the target directory. The way it is written above I believe is the better way to do it.)

There you have it. Our Zip Manager is finished and working well. Our next session will be on our EMail Manager so please stop by for that.

Well that’s it for today’s session. I hope you found it informative. Thanks again for stopping by and Remember, if you need help with an Alpha Software application or wish to inquire about a custom application for your business go to our website

www.cdc-takecharge.com

and inquire or contact

NLawson@cdc-TakeCharge.com

Have a great day.


Comments

Leave a comment