LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-11-2003, 09:37 AM   #1
debdas
LQ Newbie
 
Registered: May 2003
Posts: 19

Rep: Reputation: 0
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
 
Old 05-12-2003, 01:20 AM   #2
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
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
 
Old 05-13-2003, 12:29 AM   #3
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,415

Rep: Reputation: 99
which language?
 
Old 05-13-2003, 06:54 PM   #4
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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
 
Old 05-14-2003, 03:03 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
chmod recursion -- files only Risc91 AIX 12 09-18-2012 08:58 AM
Recursion in C hubabuba Programming 12 10-03-2005 07:46 AM
Recursion using linked-list of structures SeniorSE Programming 6 09-21-2005 02:40 AM
Perl, recursion ivanatora Programming 11 03-14-2005 01:49 PM
tar: '--no-recursion' option doesn't prevent recursion Earl Parker II Slackware 12 08-17-2004 02:49 AM


All times are GMT -5. The time now is 11:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration