Ignore:
Timestamp:
2010-01-24T14:20:12+01:00 (14 years ago)
Author:
stoecker
Message:

close #4418 - support zip files for styles and presets

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r2853 r2889  
    1717import java.awt.image.BufferedImage;
    1818import java.io.File;
     19import java.io.InputStream;
    1920import java.io.IOException;
    2021import java.net.MalformedURLException;
     
    2627import java.util.List;
    2728import java.util.Map;
     29import java.util.zip.ZipEntry;
     30import java.util.zip.ZipFile;
    2831
    2932import javax.swing.Icon;
     
    8891     */
    8992    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) {
     93        return getIfAvailable(dirs, id, subdir, name, null);
     94    }
     95
     96    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) {
    9097        if (name == null)
    9198            return null;
     
    116123        if (dirs != null && dirs.size() > 0) {
    117124            cache_name = "id:" + id + ":" + full_name;
     125            if(archive != null)
     126                cache_name += ":" + archive.getName();
    118127        }
    119128
    120129        Image img = cache.get(cache_name);
    121130        if (img == null) {
     131            if(archive != null)
     132            {
     133                try
     134                {
     135                    ZipFile zipFile = new ZipFile(archive);
     136                    ZipEntry entry = zipFile.getEntry(full_name);
     137                    if(entry != null)
     138                    {
     139                        int size = (int)entry.getSize();
     140                        int offs = 0;
     141                        byte[] buf = new byte[size];
     142                        InputStream is = zipFile.getInputStream(entry);
     143                        while(size > 0)
     144                        {
     145                            int l = is.read(buf, offs, size);
     146                            offs += l;
     147                            size -= l;
     148                        }
     149                        img = Toolkit.getDefaultToolkit().createImage(buf);
     150                    }
     151                } catch (Exception e) {
     152                    System.err.println(tr("Warning: failed to handle zip file ''{0}''. Exception was: {1}", archive.getName(), e.toString()));
     153                }
     154            }
    122155            // getImageUrl() does a ton of "stat()" calls and gets expensive
    123156            // and redundant when you have a whole ton of objects. So,
     
    125158            // and don't bother to create a URL unless we're actually
    126159            // creating the image.
    127             URL path = getImageUrl(full_name, dirs);
    128             if (path == null)
    129                 return null;
    130             img = Toolkit.getDefaultToolkit().createImage(path);
     160            if(img == null)
     161            {
     162                URL path = getImageUrl(full_name, dirs);
     163                if (path == null)
     164                    return null;
     165                img = Toolkit.getDefaultToolkit().createImage(path);
     166            }
    131167            cache.put(cache_name, img);
    132168        }
Note: See TracChangeset for help on using the changeset viewer.