LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help with recursion function (https://www.linuxquestions.org/questions/programming-9/help-with-recursion-function-59200/)

debdas 05-11-2003 09:37 AM

help with recursion function
 
Hi,

I am trying to write a recursive function that will do recursive unzipping....
I will explain...

Suppose I have two zipped files and a folder that contains other folders, zipped files and other files...

I need to unzip each file and then check the type of the file...my end condition or terminating condition will be the non existence of a zipped file i.e. i will keep on unzipping till i no longer get a zipped file..this is on the assumption that there will be zipped files inside the zipped files.

Once i finish with the first file i need to go on to the next zipped file and then work on the folder that may contain other zipped files and folders...

Maybe i can implement using a tree data structure... i'm not very sure...
I have been unable to devise a way in which i can do this...

Hope somebody can help or give me some kind of an idea...
thanks

yrraja 05-12-2003 01:20 AM

Following code gives an brief outline of how you can do this using recursion.

my_unzip(File, dest_path)
{
char zip_file_names[10][100];
int num_files=0;

unzipThisFile(File, dest_path);

status = check_path_for_zip_file(dest_path, zip_file_names, num_files);

if (status == ZIP_EXISTS)
{
for(i=0; i<num_files; i++)
{
my_unzip(File, dest_path)
}
}

reset_file_names(dest_path);
}


unzipThisFile() is the actual function that unzips the file to the dest_path.

check_path_for_zip_file() will read the names of all the zip files present in the folder and also change their extension to some other temp extension so that they are not checked by the next recursion.

reset_file_names() reset the extension of the files back to their original value.

Yaser

j-ray 05-13-2003 12:29 AM

which language?

wapcaplet 05-13-2003 06:54 PM

I am not clear on why it needs to be recursive... Most of the time, it's quite possible (and better programming) to write such functions using some kind of while loop. Something like:

Code:

while (zipFileExists)
{
  unzip(currentFile);
  currentFile = getNextFile();
  zipFileExists = currentFile.isZipFile();
}

And of course, your getNextFile() or equivalent function would scan the current directory in alphabetical order or some such, go into subfolders, re-check after zip files are unzipped, or whatever. Recursion adds nothing to that except some possible problems with stack overflow :)

acid_kewpie 05-14-2003 03:03 AM

i'd be thinking that bash would be a more suitable language than c for unzipping a bunch of stuff.

Code:

for i in $1
do
  if [ -d $i ] then
    $0 $i
  elsif [ -f $i ] then
    unzip $i $1
  fi
done

pretty crude and incorect version, you'd want to accurately check that it is a zip file, not just any file likethe -f flag does and such like...


All times are GMT -5. The time now is 01:10 PM.