/** * Output the entries to the specified output folder on the file system. * * @param outputFolder The target output folder. * @throws java.io.IOException Write failure. */ public void toFileSystem(File outputFolder) throws IOException { AssertArgument.isNotNull(outputFolder, "outputFolder"); if (outputFolder.isFile()) { throw new IOException( "Cannot write Archive entries to '" + outputFolder.getAbsolutePath() + "'. This is a normal file i.e. not a directory."); } if (!outputFolder.exists()) { outputFolder.mkdirs(); } Set<Map.Entry<String, File>> entrySet = entries.entrySet(); for (Map.Entry<String, File> entry : entrySet) { File archEntryFile = entry.getValue(); byte[] fileBytes; File entryFile = new File(outputFolder, entry.getKey()); if (archEntryFile != null) { fileBytes = FileUtils.readFile(archEntryFile); entryFile.getParentFile().mkdirs(); FileUtils.writeFile(fileBytes, entryFile); } else { entryFile.mkdirs(); } } }
/** * Get the value of the entry at the specified index in the archive. * * @param index The index. * @return The entry value at that index. */ public byte[] getEntryValue(int index) { Set<Map.Entry<String, File>> entrySet = entries.entrySet(); int i = 0; for (Map.Entry<String, File> entry : entrySet) { if (i == index) { File entryFile = entry.getValue(); if (entryFile != null) { try { return FileUtils.readFile(entryFile); } catch (IOException e) { throw new IllegalStateException( "Unexpected error reading Archive file '" + entryFile.getAbsolutePath() + "'.", e); } } else { return null; } } i++; } throw new ArrayIndexOutOfBoundsException(index); }
/** * Add the supplied data as an entry in the deployment. * * @param path The target path of the entry when added to the archive. * @param data The data. Pass null to create a directory. * @return This archive instance. */ public Archive addEntry(String path, byte[] data) { AssertArgument.isNotNullAndNotEmpty(path, "path"); File entryFile = new File(tmpDir, path); if (entryFile.exists()) { entryFile.delete(); } entryFile.getParentFile().mkdirs(); if (data == null) { entryFile.mkdir(); } else { try { FileUtils.writeFile(data, entryFile); } catch (IOException e) { throw new IllegalStateException( "Unexpected error writing Archive file '" + entryFile.getAbsolutePath() + "'.", e); } } entries.put(trimLeadingSlash(path.trim()), entryFile); return this; }
private void writeEntriesToArchive(ZipOutputStream archiveStream) throws IOException { File manifestFile = entries.get(JarFile.MANIFEST_NAME); // Always write the jar manifest as the first entry, if it exists... if (manifestFile != null) { byte[] manifest = FileUtils.readFile(manifestFile); writeEntry(JarFile.MANIFEST_NAME, manifest, archiveStream); } Set<Map.Entry<String, File>> entrySet = entries.entrySet(); for (Map.Entry<String, File> entry : entrySet) { if (!entry.getKey().equals(JarFile.MANIFEST_NAME)) { File file = entry.getValue(); if (file != null && !file.isDirectory()) { writeEntry(entry.getKey(), FileUtils.readFile(file), archiveStream); } else { writeEntry(entry.getKey(), null, archiveStream); } } } }
/** * Get an Archive entries bytes. * * @param resName Entry resource name. * @return The bytes, or null if the entry is not in the Archive. */ public byte[] getEntryBytes(String resName) { File entryFile = getEntry(resName); if (entryFile != null) { try { return FileUtils.readFile(entryFile); } catch (IOException e) { throw new IllegalStateException( "Unexpected error reading Archive file '" + entryFile.getAbsolutePath() + "'.", e); } } else { return null; } }
private static synchronized void deleteDirs() { if (dirsToDelete == null) { throw new IllegalStateException("Shutdown in progress"); } List<String> dirs = dirsToDelete; dirsToDelete = null; for (String dir : dirs) { try { FileUtils.deleteDir(new File(dir)); } catch (Exception e) { // Ignore... keep deleting dirs... } } }
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; }