1 2 Previous Next 24 Replies Latest reply: Oct 19, 2011 9:19 PM by EJP RSS

    Question about traversal of directories, how could it be in my code?

    817788
      I want to add several files and directories to zip archive. But the question about traversal of directories.
      Have the next file structure:
               directory1:
                      directory3:
                              11.txt
                              22.txt
                              33.txt
                      directory4:
                              111.txt
                              222.txt
                      directory6:
                              555.txt
                              666.txt
                      1.txt
                      2.txt
                      5.txt
      Class with main method:
      package main;
      
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import main.zip.ZipArchiver;
      
      public class ZipTest{
          public ZipTest(){
              try{
                  new ZipArchiver("directory1.zip").zip("directory1");
              }catch(FileNotFoundException ex){
                  ex.printStackTrace();
              }catch(IOException ex){
                  ex.printStackTrace();
              }
          }
          public static void main(String[] args){
              new ZipTest();
          }
      }
      ZipArchiver class:
      package main.zip;
      
      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.util.zip.ZipEntry;
      import java.util.zip.ZipOutputStream;
      
      public class ZipArchiver{
      
          public ZipArchiver(String zfn) throws FileNotFoundException{
              zipFileName=zfn;
              fos0=new FileOutputStream(zipFileName);
              zos0=new ZipOutputStream(fos0);
      
          }
      
          public void zip(String directory) throws FileNotFoundException,IOException{
              System.err.println("Starting zip("+directory+"):");
              System.out.println("directory="+directory);
              File f0=new File(directory);
              String[] files=f0.list();
              System.out.println("files.length 1="+files.length);
              for(int j=0;j<files.length;j++){
                  System.out.println("files["+j+"]="+files[j]);
              }
              for(int i=0;i<files.length;i++){
                  System.out.println("files.length 2="+files.length);
                  for(int j=0;j<files.length;j++){
                      System.out.println("files["+j+"]="+files[j]);
                  }
                  System.out.println("files["+i+"]="+files);
      ZipEntry zipEntry=null;
      if(new File(directory+fileSeparator+files[i]).isDirectory()){
      System.out.println("Adding directory: "+files[i]);
      zipEntry=new ZipEntry(files[i]+fileSeparator);
      zos0.putNextEntry(zipEntry);
      zos0.closeEntry();
      System.out.println("zip(files["+i+"].getPath())="+files[i]);
      zip(directory+fileSeparator+files[i]+fileSeparator);
      }else{
      System.out.println("Adding file: "+files[i]);
      zipEntry=new ZipEntry(files[i]);
      zos0.putNextEntry(zipEntry);
      zos0.closeEntry();
      }
      }
      }
      public final static String fileSeparator=System.getProperty("file.separator");
      private FileOutputStream fos0=null;
      private ZipOutputStream zos0=null;
      private String zipFileName=null;
      }

      To tell the truth I can’t figure out how the recursion of directories could be here?
      Here is an out:
      Starting zip(directory1):
      directory=directory1
      files.length 1=6
      files[0]=1.txt
      files[1]=2.txt
      files[2]=5.txt
      files[3]=directory3
      files[4]=directory4
      files[5]=directory6
      files.length 2=6
      files[0]=1.txt
      files[1]=2.txt
      files[2]=5.txt
      files[3]=directory3
      files[4]=directory4
      files[5]=directory6
      files[0]=1.txt
      Adding file: 1.txt
      files.length 2=6
      files[0]=1.txt
      files[1]=2.txt
      files[2]=5.txt
      files[3]=directory3
      files[4]=directory4
      files[5]=directory6
      files[1]=2.txt
      Adding file: 2.txt
      files.length 2=6
      files[0]=1.txt
      files[1]=2.txt
      files[2]=5.txt
      files[3]=directory3
      files[4]=directory4
      files[5]=directory6
      files[2]=5.txt
      Adding file: 5.txt
      files.length 2=6
      files[0]=1.txt
      files[1]=2.txt
      files[2]=5.txt
      files[3]=directory3
      files[4]=directory4
      files[5]=directory6
      files[3]=directory3
      Adding directory: directory3
      zip(files[3].getPath())=directory3
      directory=directory1\directory3\
      Starting zip(directory1\directory3\):
      files.length 1=3
      files[0]=11.txt
      files[1]=22.txt
      files[2]=33.txt
      files.length 2=3
      files[0]=11.txt
      files[1]=22.txt
      files[2]=33.txt
      files[0]=11.txt
      Adding file: 11.txt
      files.length 2=3
      files[0]=11.txt
      files[1]=22.txt
      files[2]=33.txt
      files[1]=22.txt
      Adding file: 22.txt
      files.length 2=3
      files[0]=11.txt
      files[1]=22.txt
      files[2]=33.txt
      files[2]=33.txt
      Adding file: 33.txt
      files.length 2=6 *<------WHY THE LENGTH OF ARRAY files =6 AGAIN?*
      files[0]=1.txt
      files[1]=2.txt
      files[2]=5.txt
      files[3]=directory3
      files[4]=directory4
      files[5]=directory6
      files[4]=directory4
      Adding directory: directory4
      zip(files[4].getPath())=directory4
      Starting zip(directory1\directory4\):
      directory=directory1\directory4\
      files.length 1=2
      files[0]=111.txt
      files[1]=222.txt
      files.length 2=2
      files[0]=111.txt
      files[1]=222.txt
      files[0]=111.txt
      Adding file: 111.txt
      files.length 2=2
      files[0]=111.txt
      files[1]=222.txt
      files[1]=222.txt
      Adding file: 222.txt
      files.length 2=6
      files[0]=1.txt
      files[1]=2.txt
      files[2]=5.txt
      files[3]=directory3
      files[4]=directory4
      files[5]=directory6
      files[5]=directory6
      Adding directory: directory6
      zip(files[5].getPath())=directory6
      Starting zip(directory1\directory6\):
      directory=directory1\directory6\
      files.length 1=2
      files[0]=555.txt
      files[1]=666.txt
      files.length 2=2
      files[0]=555.txt
      files[1]=666.txt
      files[0]=555.txt
      Adding file: 555.txt
      files.length 2=2
      files[0]=555.txt
      files[1]=666.txt
      files[1]=666.txt
      Adding file: 666.txt

      Edited by: user124 on Oct 19, 2011 6:13 PM

      deleted
              if(directory.endsWith("directory1\\tmp")){
                  zos0.close();
                  fos0.close();
              }
      it was from previous version.
        • 1. Re: Question about making recursion of directories, how could it be in my code?
          baftos
          Google for "recursive directory traversal java" and you get code that works.
          • 2. Re: Question about making recursion of directories, how could it be in my code?
            817788
            My code works, but i can't figure out what made a recursion?
            • 3. Re: Question about making recursion of directories, how could it be in my code?
              gimbal2
              user124 wrote:
              files.length 2=6 *<------WHY THE LENGTH OF ARRAY files =6 AGAIN?*
              Well checking the code, this piece to be exact:
              System.out.println("files.length 1="+files.length); // printing out files.length
                      for(int j=0;j<files.length;j++){
                          System.out.println("files["+j+"]="+files[j]);
                      }
                      for(int i=0;i<files.length;i++){
                          System.out.println("files.length 2="+files.length); // printing out files.length again
              <snip>
              I really don't understand this code.
              • 4. Re: Question about making recursion of directories, how could it be in my code?
                817788
                files.lengh 1 <- before cycle for(int i=0;i<files.length;i++) , files.lengh 2 <- inside cycle.

                The question is: if i have only one array files and if it evry time contains the content of a directory which is process at current , how could it contain the content of directory1 after finishing process directory3 ?
                • 5. Re: Question about making recursion of directories, how could it be in my code?
                  796283
                  user124 wrote:
                  My code works, but i can't figure out what made a recursion?
                  Ignoring your code for a minute, what is the most basic requirement for recursion?

                  ...

                  Once you've answered that you should be able to quickly point that out that feature your code.
                  • 6. Re: Question about making recursion of directories, how could it be in my code?
                    817788
                    If you know - please tell, because i cant' understand how array files after assigning new values could revert to previous?
                    • 7. Re: Question about making recursion of directories, how could it be in my code?
                      796440
                      user124 wrote:
                      If you know - please tell, because i cant' understand how array files after assigning new values could revert to previous?
                      Maybe you could try answering his question, noting, of course, that his question has nothing at all to do with your code or with files.
                      • 8. Re: Question about making recursion of directories, how could it be in my code?
                        817788
                        jverd wrote:
                        user124 wrote:
                        If you know - please tell, because i cant' understand how array files after assigning new values could revert to previous?
                        Maybe you could try answering his question, noting, of course, that his question has nothing at all to do with your code or with files.
                        Ok. Method zip call itself when a new directory found and send it to itself as a parameter.
                        Then array files got all files and directories from new directory by code:
                        File f0=new File(directory);
                        String[] files=f0.list();
                        After scanning all content of nested directory, what make array files (from my code) revert to root directory?

                        I saw several examples for scanning directories in filesystem, but they all have at least several calls for method list() .
                        • 9. Re: Question about making recursion of directories, how could it be in my code?
                          796283
                          user124 wrote:
                          what make array files (from my code) revert to root directory?
                          Again, ignoring your code for a second...now that you know what the basic premise for recursion is, next question is: how does it work (specifically execution flow of recursive method calls)?
                          user124 wrote:
                          I saw several examples for scanning directories in filesystem, but they all have at least several calls for method list() .
                          It's possible those were not recursive directory traversals. If it was written recursively, I see no need to have a call to list() more than twice in the code (once outside recursion, once inside). But since your question is dealing with why (is array etc...), I wouldn't worry about this yet.
                          • 10. Re: Question about making recursion of directories, how could it be in my code?
                            817788
                            bogdana wrote:
                            user124 wrote:
                            what make array files (from my code) revert to root directory?
                            Again, ignoring your code for a second...now that you know what the basic premise for recursion is, next question is: how does it work (specifically execution flow of recursive method calls)?
                            user124 wrote:
                            I saw several examples for scanning directories in filesystem, but they all have at least several calls for method list() .
                            It's possible those were not recursive directory traversals. If it was written recursively, I see no need to have a call to list() more than twice in the code (once outside recursion, once inside). But since your question is dealing with why (is array etc...), I wouldn't worry about this yet.
                            I speak in Russian, maybe we misunderstood each other a little bit.
                            Yes, you right, no need to have a call to list() more than twice. But i have only one call of list() and the values assigning for the same array. If i rewrite array could it remember previous values? I don't send root directory as a parameter after nested directory scanning finished.
                            • 11. Re: Question about making recursion of directories, how could it be in my code?
                              817788
                              Corrected my question. Sorry, i mean not to how make a recursion, i want to find out about traversal of directories, especially how could array files revert its values inside cycle for?
                              • 12. Re: Question about making recursion of directories, how could it be in my code?
                                817788
                                Simplifying example.
                                 
                                package main; 
                                
                                import java.io.File; 
                                
                                public class FilesTraversal{ 
                                    public FilesTraversal(String directory){ 
                                        scan(directory); 
                                    } 
                                
                                    public void scan(String directory){ 
                                        File f0=new File(directory); 
                                        File[] files=f0.listFiles(); 
                                        for(int i=0;i<files.length;i++){ 
                                            if(files.isDirectory()){ 
                                System.out.println("Directory: "+files[i].getName());
                                scan(files[i].getPath());
                                }else{
                                System.out.println("File: "+files[i].getName());
                                }
                                }
                                }

                                public static void main(String[] args){
                                new FilesTraversal("directory1");
                                }
                                }
                                • 13. Re: Question about making recursion of directories, how could it be in my code?
                                  DrClap
                                  I lost track of your question. Was it incredulity at how the local variable "files" could work?

                                  There isn't any "reverting" going on. Each instantiation of the method has its own instance of that local variable. You'd only need "reverting" if there were only one instance of the variable shared by all instantiations of the method, which isn't the case.
                                  • 14. Re: Question about making recursion of directories, how could it be in my code?
                                    817788
                                    DrClap wrote:
                                    I lost track of your question. Was it incredulity at how the local variable "files" could work?

                                    There isn't any "reverting" going on. Each instantiation of the method has its own instance of that local variable. You'd only need "reverting" if there were only one instance of the variable shared by all instantiations of the method, which isn't the case.
                                    Please, look at the last example. After scanning all content of nested directory, what make array files revert its values to upper level directory? Which methods (except of scan ) has its own, local, instance of array files ?

                                    Each time when i call *scan(files.getPath())* , array files rewrites its values, am i right?
                                    1 2 Previous Next