10 Replies Latest reply: Dec 18, 2007 7:59 AM by 807603 RSS

    getting files from a zip file:

    807603
      Im trying to read a file from a zip file using the following method:
      public void readZipFiles(String filename)
          {
              try
              {
                  byte[] buf = new byte[1024];
                  ZipInputStream zipinputstream = null;
                  ZipEntry zipentry;
                  zipinputstream = new ZipInputStream(
                       new FileInputStream(filename));
      
                  zipentry = zipinputstream.getNextEntry();
                  while (zipentry != null) 
                  { 
                      //for each entry to be extracted
                      String entryName = zipentry.getName();
                      System.out.println("File ::"+entryName);
                      RandomAccessFile  rf;
                      File newFile = new File(entryName);
                      String directory = newFile.getParent();
                      if(directory == null)
                      {
                          if(newFile.isDirectory())
                              break;
                      }
                      System.out.println("d" );
                      rf = new RandomAccessFile(entryName,"r");               
                      System.out.println("e" );
                      String line;
      
                      if ((line =rf.readLine()) !=null)
                      {
                          System.out.println(line);
                      }
      
                      rf.close(); 
                      zipinputstream.closeEntry();
                      zipentry = zipinputstream.getNextEntry();
      
                  }//while
      
                  zipinputstream.close();
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
          }
      but just after System.out.println("d" ); I get an error?

      File:: jav_4250.pdf
      a
      b
      c
      d
      java.io.FileNotFoundException: jav_4250.pdf

      but why is it not found if it just got it from the zip?
        • 1. Re: getting files from a zip file:
          800322
          Eh. All you did so far was noticing that the ZIP contains such a file, and create a File instance (= filename) of the same name. It doesn't make that file magically appear in your file system; you'll have to read the entry's stream and write the content to the disk.

          And what are you doing with that RandomAccessFile?
          • 2. Re: getting files from a zip file:
            807603
            You are trying to open for read only a random access file using
                            rf = new RandomAccessFile(entryName,"r");               
            but the file does not exist since you have not created it yet! You have extracted the file name from the zip but you have not saved the content anywhere.
            • 3. Re: getting files from a zip file:
              807603
              ok, Ill try another method thanks.

              Edited by: Mr_Tuition on Dec 18, 2007 6:26 AM
              • 4. Re: getting files from a zip file:
                807603
                im now trying this method instead, it kind of works, but reads in all files (I just wanted XML files), and Im sure its not the best method for this:
                public void readZipXMLFiles(String inFilename)
                    {
                try {
                        // Open the ZIP file
                        
                        ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename));
                         
                        // Get the first entry
                        ZipEntry entry;
                        entry = in.getNextEntry();
                        while (entry != null){
                           
                        // Open the output file
                        String entryName = entry.getName();
                        String outFilename = entryName;
                        OutputStream out = new FileOutputStream(outFilename);
                    
                        CheckExtension("xml", entry.getName()); 
                        // this check the file extension, if correct FileType=true
                        System.out.println(entry.getName()+ ":" + FileType);
                        
                        if (FileType == true){
                        // Transfer bytes from the ZIP file to the output file
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                    }
                        // Close the streams
                        
                        entry = in.getNextEntry();
                        out.close(); 
                    }
                        
                        
                        in.close();
                        
                    } catch (IOException e) {
                    }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                Because I'm using a ZipInputStream, its grabbing all files even if the files are not .xml,

                Edited by: Mr_Tuition on Dec 18, 2007 7:05 AM

                Edited by: Mr_Tuition on Dec 18, 2007 7:08 AM
                • 5. Re: getting files from a zip file:
                  807603
                  So why are you openning (and hence creating) the output file before.the extension check?

                  And what's wrong with, simply,
                  if(entry.getName().toLowerCase().endsWith(".xml")) {
                  • 6. Re: getting files from a zip file:
                    807603
                    yup your right, it actually works (it was witting out 0k non xml files), thanks
                    • 7. Re: getting files from a zip file:
                      807603
                      phew im getting a head ache...

                      i need to put these into a string (for manipulation rather than writting out, not sure how to do this, I have tried swapping the above while loop with this:

                      while ((len = in.read(buf)) > 0) {
                                  // i dont need to write the xml to a file, just convert to a string 
                               // out.write(buf, 0, len);
                                sb.append((char)len);
                              }
                              String sb2sting = sb.toString();
                              out2.write(sb2sting);
                              out2.close(); 
                          }
                      but the output is crazy

                      Edited by: Mr_Tuition on Dec 18, 2007 7:55 AM

                      Edited by: Mr_Tuition on Dec 18, 2007 7:55 AM
                      • 8. Re: getting files from a zip file:
                        807603
                        If you want to read the zip entry as text then wrap the InputStream in an InputStreamReader (and probably that in a BuffererdReader).

                        Or maybe read the XML with a DOMFactory.
                        • 9. Re: getting files from a zip file:
                          807603
                          I wonder if there is a way of reading in the entire zip compressed file (rather than a line at a time)

                          I could use this:
                          BufferedReader allData = new BufferedReader(new InputStreamReader
                                  (new ZipInputStream(new FileInputStream(inFilename))));
                          but then I m not going to be able to .getNextEntry()

                          Edited by: Mr_Tuition on Dec 18, 2007 8:13 AM

                          Edited by: Mr_Tuition on Dec 18, 2007 8:15 AM
                          • 10. Re: getting files from a zip file:
                            DrClap
                            I think you should have a look at the Java I/O tutorial:

                            http://java.sun.com/docs/books/tutorial/essential/io/

                            And when you read from a zip entry, try to clear your mind of the fact that you are reading from a zip entry. It's just an InputStream. There's no Oh My God It's Reading From A Zip Entry InputStream, it's just an InputStream. That's all you need to know.