From f5306591821b5f6d4e381ff30b647bd149d51f43 Mon Sep 17 00:00:00 2001 From: jprocter Date: Thu, 27 Sep 2007 13:03:38 +0000 Subject: [PATCH] Switched to using pure java Apache Zip implementation git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@479 be28352e-c001-0410-b1a7-c7978e42abec --- .../vamsas/client/simpleclient/VamsasArchive.java | 11 ++- .../client/simpleclient/VamsasArchiveReader.java | 100 +++++++++----------- .../ac/vamsas/test/simpleclient/VamsasArchive.java | 16 +++- src/uk/ac/vamsas/test/simpleclient/ZipTest.java | 15 +++ 4 files changed, 79 insertions(+), 63 deletions(-) diff --git a/src/uk/ac/vamsas/client/simpleclient/VamsasArchive.java b/src/uk/ac/vamsas/client/simpleclient/VamsasArchive.java index 0cb8909..c01ac26 100644 --- a/src/uk/ac/vamsas/client/simpleclient/VamsasArchive.java +++ b/src/uk/ac/vamsas/client/simpleclient/VamsasArchive.java @@ -139,7 +139,7 @@ public class VamsasArchive { /** * Output stream for archived data */ - JarOutputStream newarchive=null; + org.apache.tools.zip.ZipOutputStream newarchive=null; /** * JarEntries written to archive */ @@ -296,7 +296,8 @@ public class VamsasArchive { * @throws IOException if entry name was invalid or a new entry could not be made on newarchive */ private boolean addValidEntry(String entry) throws IOException { - JarEntry je = new JarEntry(entry); + org.apache.tools.zip.ZipEntry je = new org.apache.tools.zip.ZipEntry(entry); + // je.setExsetExtra(null); if (!addEntry(entry)) return false; newarchive.flush(); @@ -326,7 +327,7 @@ public class VamsasArchive { if (newarchive!=null) { try { newarchive.closeEntry(); - newarchive.putNextEntry(new JarEntry("deleted")); + newarchive.putNextEntry(new org.apache.tools.zip.ZipEntry("deleted")); newarchive.closeEntry(); newarchive.close(); @@ -571,8 +572,8 @@ public class VamsasArchive { if (!rchive.lockFile()) throw new IOException("Failed to get lock on file "+archive); // LATER: locked IO stream based access. - Manifest newmanifest = new Manifest(); - newarchive = new JarOutputStream(rchive.fileLock.getBufferedOutputStream(true), newmanifest); + // Manifest newmanifest = new Manifest(); + newarchive = new org.apache.tools.zip.ZipOutputStream(rchive.fileLock.getBufferedOutputStream(true));// , newmanifest); //newarchive = new JarOutputStream(new BufferedOutputStream(new java.io.FileOutputStream(archive))); entries = new Hashtable(); } diff --git a/src/uk/ac/vamsas/client/simpleclient/VamsasArchiveReader.java b/src/uk/ac/vamsas/client/simpleclient/VamsasArchiveReader.java index 7f9a008..97efc73 100644 --- a/src/uk/ac/vamsas/client/simpleclient/VamsasArchiveReader.java +++ b/src/uk/ac/vamsas/client/simpleclient/VamsasArchiveReader.java @@ -10,13 +10,7 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; -import java.util.jar.JarInputStream; -import java.util.jar.JarOutputStream; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - +import org.apache.tools.zip.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -33,18 +27,21 @@ public class VamsasArchiveReader { private static final int JARFILE_OPEN_RETRIES = 50; private static final int JARFILE_OPEN_RETRYWAIT = 1; private static Log log = LogFactory.getLog(VamsasArchiveReader.class); - JarFile jfile=null; + ZipFile jfile=null; + String jfileName="randomAccessFile"; boolean stream=false; // true if we are seeking on the stream. RandomAccessFile rfile; - ZipInputStream jstream=null; + // ZipInputStream jstream=null; Hashtable strmentries = null; private void streamInit() { //throw new Error("VamsasArchiveReader(Stream) Not implemented!"); if (!stream) { log.debug("Skipping init for Jar Stream input."); return; + } else { + throw new Error("Implementation error - we don't do streams - only files or RA files"); } - strmentries = new Hashtable(); + /*strmentries = new Hashtable(); log.debug("Jar Stream input Initialisation"); try { rfile.seek(0); @@ -67,7 +64,7 @@ public class VamsasArchiveReader { catch (Exception e) { log.warn("Exceptions during init!",e); jstream=null; - } + } */ } public VamsasArchiveReader(File vamsasfile) { @@ -78,7 +75,8 @@ public class VamsasArchiveReader { while (jfile==null && --retries>0) { try { - jfile=new JarFile(vamsasfile); + jfile=new ZipFile(vamsasfile); + jfileName = vamsasfile.toString(); } catch (Exception e) { ex = e; @@ -101,19 +99,25 @@ public class VamsasArchiveReader { * @param vamsaslock */ public VamsasArchiveReader(Lock vamsaslock) { - // LATER: implement or remove + jfile = null; if (vamsaslock==null || !vamsaslock.isLocked()) throw new Error("IMPLEMENTATION ERROR: Cannot create a VamsasArchiveReader without a valid lock."); // throw new Error("VamsasArchiveReading from locked IO stream not yet implemented."); try { rfile = vamsaslock.getRaFile(); + jfile = new ZipFile(rfile); + if (vamsaslock.target!=null) + jfileName = vamsaslock.target.toString(); } catch (Exception e) { + rfile = null; + jfile = null; log.warn("Unexpected IO Exception when accessing locked vamsas archive stream "+vamsaslock.target,e); } - stream = true; + /*stream = true; streamInit(); if (jstream==null) throw new Error("Failed to open archive from Locked random access stream."); + */ } /** @@ -136,7 +140,6 @@ public class VamsasArchiveReader { * seeks jstream to the given entry name and reads it. * @param entryname * @return - */ private JarEntry seekEntry(String entryname) { if (jstream==null) return null; @@ -165,18 +168,19 @@ public class VamsasArchiveReader { } return null; } + */ /** * * @return JarEntry for VamsasArchiveReader.VAMSASDOC */ - protected JarEntry getVamsasDocumentEntry() { + protected ZipEntry getVamsasDocumentEntry() { return getJarEntry(VAMSASDOC); } /** * * @return JarEntry for VamsasArchiveReader.VAMSASXML */ - protected JarEntry getVamsasXmlEntry() { + protected ZipEntry getVamsasXmlEntry() { return getJarEntry(VAMSASXML); } /** @@ -185,22 +189,22 @@ public class VamsasArchiveReader { */ public boolean isValid() { // TODO: check if VAMSASDOC is well formed (follows www.vamsas.ac.uk/schemas/vamsasDocument.xsd) and all appData references are resolvable - preferably as jar entries - if (jfile!=null || jstream!=null) + if (jfile!=null) // || jstream!=null) return (getVamsasDocumentEntry()!=null); return false; } - protected JarEntry getAppdataEntry(String AppdataRef) { - JarEntry entry; - if ((jfile==null && jstream==null) || !isValid() || (entry=getJarEntry(AppdataRef))==null) + protected ZipEntry getAppdataEntry(String AppdataRef) { + ZipEntry entry; + if (jfile==null || !isValid() || (entry=getJarEntry(AppdataRef))==null) return null; return entry; } public InputStream getAppdataStream(String AppdataRef) { - JarEntry entry=getAppdataEntry(AppdataRef); + ZipEntry entry=getAppdataEntry(AppdataRef); try { if (entry!=null) return getInputStream(entry); @@ -215,7 +219,7 @@ public class VamsasArchiveReader { */ public InputStream getVamsasDocumentStream() { InputStream vdoc; - if ((jfile==null && jstream==null) || !isValid()) + if (jfile==null || !isValid()) return null; try { vdoc = getInputStream(getVamsasDocumentEntry()); @@ -234,7 +238,7 @@ public class VamsasArchiveReader { public InputStream getVamsasXmlStream() { // log.warn("Deprecated call"); - JarEntry xmle=getVamsasXmlEntry(); + ZipEntry xmle=getVamsasXmlEntry(); InputStream vdoc; if (xmle==null) return null; @@ -255,18 +259,10 @@ public class VamsasArchiveReader { if (jfile!=null) { try { jfile.close(); - } catch (IOException e) { - log.error("Whilst closing JarFile "+jfile.getName(), e); - } - } - if (jstream!=null) { - try { - jstream.closeEntry(); - jstream=null; - // LATER: reference counting for random access file instances is necessary. - } + rfile=null; + } catch (Exception e) { - log.error("Whilst finishing reading from jar input stream",e); + log.error("Whilst closing JarFile "+jfileName, e); } } } @@ -276,10 +272,10 @@ public class VamsasArchiveReader { * @return array of entries. */ public Vector getExtraEntries() { - if ((jfile==null && jstream==null)|| !isValid()) + if (jfile==null || !isValid()) return null; Vector e = new Vector(); - if (jstream!=null) { + /*if (jstream!=null) { Enumeration entries = strmentries.keys(); if (entries!=null && entries.hasMoreElements()) { do { @@ -288,18 +284,16 @@ public class VamsasArchiveReader { e.add(new String(el.getName())); // avoid references } while (entries.hasMoreElements()); } - } else { - Enumeration entries = jfile.entries(); - if (entries!=null && entries.hasMoreElements()) { - do { - JarEntry el = (JarEntry) entries.nextElement(); - if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML)) - e.add(new String(el.getName())); // avoid references - } while (entries.hasMoreElements()); - } - return e; + } else */ + Enumeration entries = jfile.getEntries(); + if (entries!=null && entries.hasMoreElements()) { + do { + ZipEntry el = (ZipEntry) entries.nextElement(); + if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML)) + e.add(new String(el.getName())); // avoid references + } while (entries.hasMoreElements()); } - return null; + return e; } /* (non-Javadoc) @@ -308,21 +302,15 @@ public class VamsasArchiveReader { private InputStream getInputStream(ZipEntry ze) throws IOException { if (jfile!=null) return jfile.getInputStream(ze); - if (jstream!=null) { - seekEntry(ze.getName()); - return new AppDataInputStream(jstream); - } return null; } /* (non-Javadoc) * @see java.util.jar.JarFile#getJarEntry(java.lang.String) */ - private JarEntry getJarEntry(String name) { + private ZipEntry getJarEntry(String name) { if (jfile!=null) - return jfile.getJarEntry(name); - if (jstream!=null) - return seekEntry(name); + return jfile.getEntry(name); return null; } } diff --git a/src/uk/ac/vamsas/test/simpleclient/VamsasArchive.java b/src/uk/ac/vamsas/test/simpleclient/VamsasArchive.java index 52dec7e..175c2bc 100644 --- a/src/uk/ac/vamsas/test/simpleclient/VamsasArchive.java +++ b/src/uk/ac/vamsas/test/simpleclient/VamsasArchive.java @@ -112,13 +112,25 @@ public class VamsasArchive { JarFile jf = new JarFile(av, true, JarFile.OPEN_READ); if (jf.getEntry("vamsasDocument.xml")!=null) { - log.info("Valid archive "+av); + log.info("Valid archive (sun) "+av); } jf.close(); } catch (Exception f) { - log.info("Couldn't access jar archive "+av, f); + log.warn("Couldn't access jar archive with sun jartools: "+av, f); } + try { + org.apache.tools.zip.ZipFile jf = new org.apache.tools.zip.ZipFile(av); + if (jf.getEntry("vamsasDocument.xml")!=null) + { + log.info("Valid archive (apache) "+av); + } + jf.close(); + } catch (Exception f) + { + log.warn("Couldn't access jar archive with apache ziptool: "+av,f); + } + log.info("Opening archive "+av); uk.ac.vamsas.client.simpleclient.VamsasArchive varchive = new uk.ac.vamsas.client.simpleclient.VamsasArchive(av, true); diff --git a/src/uk/ac/vamsas/test/simpleclient/ZipTest.java b/src/uk/ac/vamsas/test/simpleclient/ZipTest.java index 0c6fa11..a860e0e 100644 --- a/src/uk/ac/vamsas/test/simpleclient/ZipTest.java +++ b/src/uk/ac/vamsas/test/simpleclient/ZipTest.java @@ -18,6 +18,7 @@ public class ZipTest { */ public static void main(String[] args) { File av = new File(args[0]); + boolean jfailed=false; try { JarFile jf = new JarFile(av, false, JarFile.OPEN_READ); if (jf.getEntry("vamsasDocument.xml")!=null) @@ -25,6 +26,20 @@ public class ZipTest { System.out.println("Valid archive "+av); } jf.close(); + return; + } catch (Exception f) + { + System.out.println("Couldn't access jar archive "+av); + f.printStackTrace(System.out); + } + try { + System.out.println("Trying the Apache Zip Package:"); + org.apache.tools.zip.ZipFile jf = new org.apache.tools.zip.ZipFile(av); + if (jf.getEntry("vamsasDocument.xml")!=null) + { + System.out.println("Valid archive "+av); + } + jf.close(); } catch (Exception f) { System.out.println("Couldn't access jar archive "+av); -- 1.7.10.2