Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

Rep: Reputation: 0
help with recursion function


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 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...
Old 05-12-2003, 01:20 AM   #2
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)


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.

Old 05-13-2003, 12:29 AM   #3
Senior Member
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,548

Rep: Reputation: 136Reputation: 136
which language?
Old 05-13-2003, 06:54 PM   #4
LQ 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:

while (zipFileExists)
  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
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

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

for i in $1
  if [ -d $i ] then 
    $0 $i
  elsif [ -f $i ] then
    unzip $i $1
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...


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 16 09-28-2016 11:15 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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:41 AM.

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