We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProcessing DevelopmentLibraries,  Tool Development › Add zip reading support: code included
Page Index Toggle Pages: 1
Add zip reading support: code included (Read 1753 times)
Add zip reading support: code included
Mar 28th, 2006, 11:25pm
 
Zipfile has to be situated in the data-folder, and unpackFileFromZip will write to the data-folder.

usage:
PImage image = loadImageFromZip("zipname.zip","image.gif");
String text = loadStringFromZip("zipname.zip","helloworld.txt");
byte[] bytes = loadBytesFromZip("zipname.zip","whatever.duh");

Quote:


String unpackFileFromZip(String zipname, String fname) // indepentent  (meaning everything else works without this)
{
 if(online) return "only offline, or with a signed applet";
 
 byte[] bytes = loadBytesFromZip(zipname,fname);
 File output = new File(sketchPath("data\\"+fname));
 try {
   OutputStream out = new FileOutputStream(output.getAbsolutePath());
   out.write(bytes,0,bytes.length);
   out.close();
 }catch(Exception e){
   return e.toString();
 }
 return output.getAbsolutePath();
}

String loadStringFromZip(String zipname, String fname) // indepentent
{
 byte[] ret = loadBytesFromZip(zipname,fname);
 if(ret == null) return null;
 return new String(ret);
}

PImage loadImageFromZip(String zipname, String fname) // indepentent
{
 if(!(fname.toLowerCase().endsWith(".gif") || fname.toLowerCase().endsWith(".jpg"))){
     println("Format not accepted: "+fname);
     return null;
 }
 byte[] ret = loadBytesFromZip(zipname,fname);
 if(ret == null) return null;
 return loadImage(ret);
}

PImage loadImage(byte[] bytes) // used by loadImageFromZip()
{
 Image img = Toolkit.getDefaultToolkit().createImage(bytes);
 MediaTracker t = new MediaTracker(this);
 t.addImage(img, 0);
 try {
   t.waitForAll();
 } catch (Exception e) {
   println(e);
 }
 return new PImage(img);
}

byte[] loadBytesFromZip(String zipname, String fname) // used by loadStringFromZip, loadImageFromZip and unpackFileFromZip
{
 try {
   // Open the ZIP file
   File inFilename = new File(sketchPath("data\\"+zipname));
   ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename.getAbsolutePath()));
   
   // Get the entry that matches
   ZipEntry entry = in.getNextEntry();
   while(!entry.getName().toLowerCase().equals(fname.toLowerCase())){
     entry = in.getNextEntry();
   }
   
   if(!entry.getName().toLowerCase().equals(fname.toLowerCase())){
     println("No such file: "+fname);
     return null;
   }

   // Transfer bytes from the ZIP file to the bytearrayoutputstream
   ByteArrayOutputStream bout = new ByteArrayOutputStream();
   byte[] buf = new byte[1024];
   int len;
   while ((len = in.read(buf)) > 0) {
     bout.write(buf,0,len);
   }
   
   // Close the stream
   byte[] ret = bout.toByteArray();
   in.close();
   bout.close();
   return ret;
 } catch (IOException e) {
   println(e);
 }
 return null;
}




-seltar
Re: Add zip reading support: code included
Reply #1 - Mar 29th, 2006, 4:48pm
 
this sounds like a useful library, or even just a .jar that could be dropped into a sketch (via the code folder) easily.

i'd say package it up as a single class that works something like:

PileOfZip poz = new PileOfZip("blah.zip");

PImage image = poz.loadImage("something.png");
byte bytes[] = poz.loadBytes("thebytes.dat");

etc..

having a single zip instance loaded can make things a little more efficient, especially if you're trying to pull a *lot* of files.

you'd need a better name than PileOfZip, but i've found zip file access useful in other projects, it's just something that i see as being outside the core api.
Re: Add zip reading support: code included
Reply #2 - Mar 29th, 2006, 5:34pm
 
understandable, and i did actually start it yesterday, but i dismissed it.. don't remember why..

i'll get right to it! and thanks for the encouragement Smiley

-seltar
Re: Add zip reading support: code included
Reply #3 - Mar 29th, 2006, 7:00pm
 
Thanks seltar!!!!

I would be really happy with that kind of library also.  Some of the font files I use get compressed to half their size!!!  That would help a lot.

And Ben's idea of the API looks really good.
Re: Add zip reading support: code included
Reply #4 - Mar 29th, 2006, 8:09pm
 
http://seltar.wliia.org/libraries/unzipit/

voila! Smiley

enjoy, and comments.. pleeease!

should probably make a version for PFont as well..

i'll look in to it now.. Smiley should i start a new post for this... nah.. i'll just leave it Smiley

-seltar
Re: Add zip reading support: code included
Reply #5 - Mar 29th, 2006, 8:54pm
 
Fonts are working as well.. i only figured out .vlw support though.. but that should do you well.. i compressed ArialItalicMT-48.vlw from 241kb to 48kb, and loaded it back in.. so it works.. hurray!

Available from version 1.1

-seltar
Re: Add zip reading support: code included
Reply #6 - Mar 29th, 2006, 9:18pm
 
I feel I should point out that .jar files are actually just .zip files with certain required files.

So if you're putting stuff into your data folder, it's going to be zipped when you export, so you don't need to use this on /data/ files, only loading external files, to keep the size down.

That being said, this looks very useful for a couple of things I'm developing, where it'll be useful to be able to only load the elements required for a certain part of the sketch at any one time.
Page Index Toggle Pages: 1