Examples of ZipFile


Examples of java.util.zip.ZipFile

     * responsability to call close() in it when it is no longer needed.
     * @return a ZipFile for reading
     * @throws IOException
     */
    protected ZipFile getZipFile() throws IOException {
        return new ZipFile(file);
    }
View Full Code Here

Examples of java.util.zip.ZipFile

    public synchronized void update() {
        if (file.lastModified() != lastModified ||
                repositories == null ||
                resources == null) {
            lastModified = file.lastModified();
            ZipFile zipfile = null;

            try {
                zipfile = getZipFile();
                Enumeration en = zipfile.entries();
                HashMap newRepositories = new HashMap();
                HashMap newResources = new HashMap();

                while (en.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry) en.nextElement();
                    String eName = entry.getName();

                    if (!eName.regionMatches(0, entryPath, 0, entryPath.length())) {
                        // names don't match - not a child of ours
                        continue;
                    }
                    String[] entrypath = StringUtils.split(eName, "/");
                    if (depth > 0 && !shortName.equals(entrypath[depth-1])) {
                        // catch case where our name is Foo and other's is FooBar
                        continue;
                    }

                    // create new repositories and resources for all entries with a
                    // path depth of this.depth + 1
                    if (entrypath.length == depth + 1 && !entry.isDirectory()) {
                        // create a new child resource
                        ZipResource resource = new ZipResource(entry.getName(), this);
                        newResources.put(resource.getShortName(), resource);
                    } else if (entrypath.length > depth) {
                        // create a new child repository
                        if (!newRepositories.containsKey(entrypath[depth])) {
                            ZipEntry child = composeChildEntry(entrypath[depth]);
                            ZipRepository rep = new ZipRepository(file, this, child);
                            newRepositories.put(entrypath[depth], rep);
                        }
                    }
                }

                repositories = (Repository[]) newRepositories.values()
                        .toArray(new Repository[newRepositories.size()]);
                resources = newResources;

            } catch (Exception ex) {
                ex.printStackTrace();
                repositories = emptyRepositories;
                if (resources == null) {
                    resources = new HashMap();
                } else {
                    resources.clear();
                }

            } finally {
                try {
                    // unlocks the zip file in the underlying filesystem
                    zipfile.close();
                } catch (Exception ex) {}
            }
        }
    }
View Full Code Here

Examples of java.util.zip.ZipFile

    public long getChecksum() {
        return file.lastModified();
    }

    public boolean exists() {
        ZipFile zipfile = null;
        try {
            /* a ZipFile needs to be created to see if the zip file actually
             exists; this is not cached to provide blocking the zip file in
             the underlying filesystem */
            zipfile = getZipFile();
            return true;
        } catch (IOException ex) {
            return false;
        }
        finally {
            try {
                // unlocks the zip file in the underlying filesystem
                zipfile.close();
            } catch (Exception ex) {
                return false;
            }
        }
    }
View Full Code Here

Examples of java.util.zip.ZipFile

    public long lastModified() {
        return repository.lastModified();
    }

    public InputStream getInputStream() throws IOException {
        ZipFile zipfile = null;
        try {
            zipfile = repository.getZipFile();
            ZipEntry entry = zipfile.getEntry(entryName);
            if (entry == null) {
                throw new IOException("Zip resource " + this + " does not exist");
            }
            int size = (int) entry.getSize();
            byte[] buf = new byte[size];
            InputStream in = zipfile.getInputStream(entry);
            int read = 0;
            while (read < size) {
                int r = in.read(buf, read, size-read);
                if (r == -1)
                    break;
                read += r;
            }
            in.close();
            return new ByteArrayInputStream(buf);
        } finally {
            zipfile.close();
        }
    }
View Full Code Here

Examples of java.util.zip.ZipFile

            zipfile.close();
        }
    }

    public boolean exists() {
        ZipFile zipfile = null;
        try {
            zipfile = repository.getZipFile();
            return (zipfile.getEntry(entryName) != null);
        } catch (Exception ex) {
            return false;
        } finally {
            try {
                zipfile.close();
            } catch (Exception ex) {}
        }
    }
View Full Code Here

Examples of java.util.zip.ZipFile

            } catch (Exception ex) {}
        }
    }

    public String getContent(String encoding) throws IOException {
        ZipFile zipfile = null;
        try {
            zipfile = repository.getZipFile();
            ZipEntry entry = zipfile.getEntry(entryName);
            if (entry == null) {
                throw new IOException("Zip resource " + this + " does not exist");
            }
            InputStream in = zipfile.getInputStream(entry);
            int size = (int) entry.getSize();
            byte[] buf = new byte[size];
            int read = 0;
            while (read < size) {
                int r = in.read(buf, read, size-read);
                if (r == -1)
                    break;
                read += r;
            }
            in.close();
            return encoding == null ?
                    new String(buf) :
                    new String(buf, encoding);
        } finally {
            if (zipfile != null) {
                zipfile.close();
            }
        }
    }
View Full Code Here

Examples of java.util.zip.ZipFile

        // http://java.sun.com/j2se/1.5.0/docs/api/java/net/JarURLConnection.html
        throw new UnsupportedOperationException("getUrl() not implemented for ZipResource");
    }

    public long getLength() {
        ZipFile zipfile = null;
        try {
            zipfile = repository.getZipFile();
            return zipfile.getEntry(entryName).getSize();           
        } catch (Exception ex) {
            return 0;
        } finally {
            try {
                zipfile.close();
            } catch (Exception ex) {}
        }
    }
View Full Code Here

Examples of java.util.zip.ZipFile

  protected ZipEntry entryFromArchive( String elementName )
    throws Exception
  {
    ZipEntry entry ;
    ZipFile archive ;
    String name ;
   
    name = str().replaceAll( elementName, "\\", "/" ) ;
    archive = this.container() ;
    entry = archive.getEntry( name ) ;

    if (DEBUG)
    {
      // if ( entry == null ) com.mdcs.joi.Inspector.inspect( name ) ;
      System.out.print( archive.getName() + "::" + name + " --- "
                + ( entry != null ) ) ;
      if ( entry == null )
      {
        System.out.println() ;       
      }
View Full Code Here

Examples of java.util.zip.ZipFile

  protected ZipFile archive()
    throws Exception
  {
    if ( this.getZipFile() == null )
    {
      this.setZipFile( new ZipFile( this.fileRef() ) ) ;
    }
    return this.getZipFile() ;
  } // archive()
View Full Code Here

Examples of java.util.zip.ZipFile

 
  @SuppressWarnings("unchecked")
  public static final void unzip(File file, File targetDir) throws ZipException, IOException {
      Enumeration entries = null;
      ZipFile zipFile = null;
      try {
        zipFile = new ZipFile(file);
       
        entries = zipFile.entries();

        while(entries.hasMoreElements()) {
          ZipEntry entry = (ZipEntry)entries.nextElement();
          if(entry.isDirectory()) {
            (new File(targetDir, entry.getName())).mkdir();
            continue;
          }
          InputStream in = zipFile.getInputStream(entry);
          OutputStream out = new FileOutputStream(new File(targetDir, entry.getName()));   
          try {
            WGUtils.inToOut(in, out, 2048);
          } finally {
            out.close();
            in.close();
          }
        }
        zipFile.close();
    } finally {
        if (zipFile != null) {
          try {
            zipFile.close();
          } catch (IOException e) {           
          }
        }
      }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.