Loading...
Logo
Processing Forum
I am trying to use a java zip library http://www.javamex.com/arcmexer/ in my sketch.  I put the jar in the libraries folder and I am not getting an error on the import line so I think I have it correct.  My final goal is to extract files from a password'd zip file.

I think I need to pass it a java File object but I'm not sure I'm getting one.  If I try to use the File.exist() method I get a does not exist error, so I suspect that I'm not really getting a Java file.  

I get an  IOException at the ArchiveReader line when I try to compile the code:

Copy code

  1. import com.javamex.arcmexer.*;
  2. //import java.io.file;

  3. File f = new File(sketchPath("test.zip"));

  4. //InputStream f = createInput("test.zip");

  5. //if (f.exist()) {
  6. ArchiveReader r = ArchiveReader.getReader(f);
  7. //}

  8. void setup() {
  9.   try {
  10.     ArchiveEntry entry;
  11.     while ( (entry = r.nextEntry ()) != null) {
  12.       String filename = entry.getFilename();
  13.       println(filename);
  14.       //InputStream in = entry.getInputStream();
  15.       // ... read from in
  16.     }
  17.   } 
  18.   finally {
  19.     r.close();
  20.   }
  21. }

  22. void draw() {
  23.   
  24. }


Replies(7)

Are you sure that the file exists?
As far as I can tell, the only explanation is that the file does not exist.

Also, is the file located in the data folder?
If so, then instead of sketchPath(), you need to use dataPath().
1) That's f.exists(), not exist()
2) The zip file must be in the sketch folder, if you use sketchPath()
3) You can directly use: File f = sketchFile("test.zip");
4) sketchPath or sketchFile are valid only in a function, ie. after setup() has started and size() is called... If you use the in declarations, they are used before setup() is called and they return null.
5) Wouldn't it be simpler if you just removed the password on the zip file?
HTH.

OK got the f.exists() resolved (Thanks PhiLho) - to answer 5) It would be simpler but the files I'm getting have the password and I would like to avoid a manual step of unzipping them to use.

Now I'm having issues with the library itself I guess.  Anyway I'm trying to figure out who/how to ask about it.  Here is what I have and I'm getting "Unhandled exception type IOException" at line 11.  I expect I'll have to find out what's going on with the library and how I'm instantiating it.
Copy code
  1. import com.javamex.arcmexer.*;


  2. void setup() {
  3.   size(100,100);

  4.   File f = sketchFile("test.zip");  
  5.   
  6.   if (f.exists()) {  // this is returning false
  7.     ArchiveReader r = ArchiveReader.getReader(f);
  8.     //r.setDefaultPassword("ThePassword");
  9.     println("Found it");
  10.   }
  11.   
  12.   try {
  13.     ArchiveEntry entry;
  14.     while ( (entry = r.nextEntry ()) != null) {
  15.       String filename = entry.getFilename();
  16.       println(filename);
  17.       //InputStream in = entry.getInputStream();
  18.       // ... read from in
  19.     }
  20.   } 
  21.   finally {
  22.     r.close();
  23.   }
  24. }

  25. void draw() {
  26.   
  27. }
Just out of curiosity, what is the library that you are using?
(More specifically, where is the documentation for it?)
The link is in the first post. Javamex / Arcmexer
As the error message implies, you use a method that throws a (checked) exception, so you need to surround it with a try/catch statement.
Well after a lot of learning about (checked) exceptions I finally got my test program to work.  I left in all of my diagnostic println lines in case it helps others.  I guess the lesson learned is that if the library calls complain about  Unhandled exception type XXX, you need to handle that type XXX exception in your call.  The good news is that the compiler tells you what the name of the exception is that you need to handle.  Here is the code that works finally.  Thanks for the help PhiLho.

Copy code

  1. import com.javamex.arcmexer.*;
  2. import java.io.*;

  3. ArchiveReader r;

  4. void setup() {
  5.   size(100, 100);
  6.   String path = sketchPath;

  7.   File fv = sketchFile(path + "/test.zip");
  8.   try {
  9.     FileInputStream f = new FileInputStream(path+"/test.zip");
  10.   } 
  11.   catch(FileNotFoundException f1) {
  12.     println("FileNotFoundException");
  13.   }

  14.   if (fv.exists()) {  // this is returning false
  15.     try {
  16.       println("test1");
  17.       r = ArchiveReader.getReader(fv);
  18.       println(r);
  19.       println("test2");
  20.     }
  21.     catch(IOException i) {
  22.       println("problem");
  23.     }
  24.     catch(ArchiveFormatException i) {
  25.       println("problem2");
  26.     }
  27.     //r.setDefaultPassword("ThePassword");
  28.     println("Found it");
  29.   }

  30.   try {
  31.     ArchiveEntry entry;
  32.     println(r);

  33.     try
  34.     {
  35.       while ( (entry = r.nextEntry ()) != null) {
  36.         String filename = entry.getFilename();
  37.         println(filename);
  38.         //InputStream in = entry.getInputStream();
  39.         // ... read from in
  40.       }
  41.     } 
  42.     catch(IOException i) {
  43.       println("problem");
  44.     }
  45.     catch(ArchiveFormatException i) {
  46.       println("problem2");
  47.     }
  48.   } 
  49.   finally {
  50.     try
  51.     {
  52.       r.close();
  53.     } 
  54.     catch(IOException i) {
  55.       println("problem");
  56.     }
  57.   }
  58. }

  59. void draw() {
  60. }