private static Archive loadArchive(InputStream rawStream) { try { return new Archive(new ZipInputStream(rawStream)); } catch(Exception e) { // Assume it's not a Zip file. Just return null... return null; } }
@Override public InputStream getResourceAsStream(String name) { byte[] bytes = archive.getEntryBytes(name); if (bytes != null) { return new ByteArrayInputStream(bytes); } else { return super.getResourceAsStream(name); } }
public Archive merge(Archive archive) { Map<String, File> entriesToMerge = archive.getEntries(); for (Entry<String, File> entry : entriesToMerge.entrySet()) { File file = entry.getValue(); if (file != null && !file.isDirectory()) { try { addEntry(entry.getKey(), FileUtils.readFile(file)); } catch (IOException e) { throw new IllegalStateException("Unexpected error reading Archive file '" + file.getAbsolutePath() + "'.", e); } } else { addEntry(entry.getKey(), (byte[]) null); } } return this; }
/** * Public constructor. * @param archiveStream Archive stream containing initial archive entries. * @throws IOException Error reading from zip stream. */ public Archive(ZipInputStream archiveStream) throws IOException { this.archiveName = "Unknown"; createTempDir(); addEntries(archiveStream); }
/** * Add the supplied character data as an entry in the deployment. * * @param path The target path of the entry when added to the archive. * @param data The data. * @return This archive instance. */ public Archive addEntry(String path, String data) { AssertArgument.isNotNullAndNotEmpty(path, "path"); AssertArgument.isNotNull(data, "data"); try { addEntry(trimLeadingSlash(path.trim()), data.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unexpected UnsupportedEncodingException exception for encoding 'UTF-8' when writing Archive entry from a StringBuilder instance.", e); } return this; }
private static Archive createArchive(EdiSpecificationReader ediSpecificationReader, String urn, String... messages) throws IOException { Archive archive = new Archive(); StringBuilder modelListBuilder = new StringBuilder(); StringWriter messageEntryWriter = new StringWriter(); archive.merge(schemas); archive.addEntry(EDIUtils.EDI_MAPPING_MODEL_ZIP_LIST_FILE, modelListBuilder.toString()); archive.addEntry(EDIUtils.EDI_MAPPING_MODEL_URN, urn); interchangeProperties.store(propertiesOutStream, "UN/EDIFACT Interchange Properties"); propertiesOutStream.flush(); archive.addEntry(EDIUtils.EDI_MAPPING_MODEL_INTERCHANGE_PROPERTIES_FILE, propertiesOutStream.toByteArray()); } finally { propertiesOutStream.close();
public Archive createArchive(Set<EPackage> packages, String pluginID, String pathPrefix) throws IOException { String qualifier = qualifierFormat.format(Calendar.getInstance() .getTime()); Archive archive = new Archive(pluginID + "_1.0.0.v" + qualifier + ".jar"); StringBuilder pluginBuilder = new StringBuilder( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<?eclipse version=\"3.0\"?>\n" + "<plugin>\n"); StringBuilder xmlExtension = new StringBuilder( "\t<extension point=\"org.eclipse.wst.xml.core.catalogContributions\"><catalogContribution>\n"); for (EPackage pkg : packages) { ResourceSet rs = createResourceSet(); Resource resource = addSchemaResource(rs, pkg); EObject obj = resource.getContents().get(0); String fileName = resource.getURI().lastSegment(); String ecoreEntryPath = pathPrefix + "/" + fileName; xmlExtension.append(saveSchema(archive, ecoreEntryPath, resource, ((XSDSchema) obj).getTargetNamespace(), pluginID)); // Save memory System.gc(); } xmlExtension.append("\t</catalogContribution></extension>\n"); pluginBuilder.append(xmlExtension); pluginBuilder.append("</plugin>"); archive.addEntry(FRAGMENT_XML_ENTRY, pluginBuilder.toString()); return archive; }
/** * Create a {@link ZipInputStream} for the entries defined in this * archive. * @return The {@link ZipInputStream} for the entries in this archive. * @throws java.io.IOException Failed to create stream. */ public ZipInputStream toInputStream() throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); toOutputStream(new ZipOutputStream(outputStream)); return new ZipInputStream(new ByteArrayInputStream(outputStream.toByteArray())); }
@Override protected URL findResource(String resName) { URL resource = archive.getEntryURL(resName); if (resource != null) { return resource; } return getParent().getResource(resName); }
/** * Public constructor. */ public Archive() { this.archiveName = "Unknown"; createTempDir(); }
/** * Get an Archive entry resource URL. * @param resName Entry resource name. * @return The entry resource URL, or null if the entry is not in the Archive. */ public URL getEntryURL(String resName) { File entry = getEntry(resName); if (entry != null) { try { return entry.toURI().toURL(); } catch (MalformedURLException e) { throw new IllegalStateException("Unexpected error getting URL for Archive file '" + entry.getAbsolutePath() + "'.", e); } } else { return null; } }
/** * Add the supplied class as an entry in the deployment. * @param clazz The class to be added. * @return This archive instance. * @throws java.io.IOException Failed to read class from classpath. */ public Archive addEntry(Class<?> clazz) throws IOException { AssertArgument.isNotNull(clazz, "clazz"); String className = clazz.getName(); className = className.replace('.', '/') + ".class"; addClasspathResourceEntry(className, "/" + className); return this; }
/** * Add the supplied character data as an entry in the deployment. * * @param path The target path of the entry when added to the archive. * @param data The data. * @return This archive instance. */ public Archive addEntry(String path, String data) { AssertArgument.isNotNullAndNotEmpty(path, "path"); AssertArgument.isNotNull(data, "data"); try { addEntry(trimLeadingSlash(path.trim()), data.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unexpected UnsupportedEncodingException exception for encoding 'UTF-8' when writing Archive entry from a StringBuilder instance.", e); } return this; }
/** * Public constructor. * @param archiveStream Archive stream containing initial archive entries. * @throws IOException Error reading from zip stream. */ public Archive(ZipInputStream archiveStream) throws IOException { this.archiveName = "Unknown"; createTempDir(); addEntries(archiveStream); }
/** * Create a {@link ZipInputStream} for the entries defined in this * archive. * @return The {@link ZipInputStream} for the entries in this archive. * @throws java.io.IOException Failed to create stream. */ public ZipInputStream toInputStream() throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); toOutputStream(new ZipOutputStream(outputStream)); return new ZipInputStream(new ByteArrayInputStream(outputStream.toByteArray())); }
@Override protected URL findResource(String resName) { URL resource = archive.getEntryURL(resName); if (resource != null) { return resource; } return getParent().getResource(resName); }
/** * Public constructor. */ public Archive() { this.archiveName = "Unknown"; createTempDir(); }
/** * Get an Archive entry resource URL. * @param resName Entry resource name. * @return The entry resource URL, or null if the entry is not in the Archive. */ public URL getEntryURL(String resName) { File entry = getEntry(resName); if (entry != null) { try { return entry.toURI().toURL(); } catch (MalformedURLException e) { throw new IllegalStateException("Unexpected error getting URL for Archive file '" + entry.getAbsolutePath() + "'.", e); } } else { return null; } }