List of usage examples for java.util.zip ZipOutputStream closeEntry
public void closeEntry() throws IOException
From source file:org.sakaiproject.metaobj.shared.mgt.impl.StructuredArtifactDefinitionManagerImpl.java
protected void storeFileInZip(ZipOutputStream zos, InputStream in, String entryName) throws IOException { byte data[] = new byte[1024 * 10]; if (File.separatorChar == '\\') { entryName = entryName.replace('\\', '/'); }/*from w w w . j a v a2s . com*/ ZipEntry newfileEntry = new ZipEntry(entryName); zos.putNextEntry(newfileEntry); BufferedInputStream origin = new BufferedInputStream(in, data.length); int count; while ((count = origin.read(data, 0, data.length)) != -1) { zos.write(data, 0, count); } zos.closeEntry(); in.close(); }
From source file:com.hichinaschool.flashcards.async.Connection.java
private Payload doInBackgroundUpgradeDecks(Payload data) { // Enable http request canceller mCancelCallback = new CancelCallback(); String path = (String) data.data[0]; File ankiDir = new File(path); if (!ankiDir.isDirectory()) { data.success = false;//from w ww . j a va 2 s . c o m data.data = new Object[] { "wrong anki directory" }; return data; } // step 1: gather all .anki files into a zip, without media. // we must store them as 1.anki, 2.anki and provide a map so we don't run into // encoding issues with the zip file. File[] fileList = ankiDir.listFiles(new OldAnkiDeckFilter()); List<String> corruptFiles = new ArrayList<String>(); JSONObject map = new JSONObject(); byte[] buf = new byte[1024]; String zipFilename = path + "/upload.zip"; String colFilename = path + AnkiDroidApp.COLLECTION_PATH; try { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilename)); int n = 1; for (File f : fileList) { String deckPath = f.getAbsolutePath(); // set journal mode to delete try { AnkiDb d = AnkiDatabaseManager.getDatabase(deckPath); } catch (SQLiteDatabaseCorruptException e) { // ignore invalid .anki files corruptFiles.add(f.getName()); continue; } finally { AnkiDatabaseManager.closeDatabase(deckPath); } // zip file String tmpName = n + ".anki"; FileInputStream in = new FileInputStream(deckPath); ZipEntry ze = new ZipEntry(tmpName); zos.putNextEntry(ze); int len; while ((len = in.read(buf)) >= 0) { zos.write(buf, 0, len); } zos.closeEntry(); map.put(tmpName, f.getName()); n++; } // if all .anki files were found corrupted, abort if (fileList.length == corruptFiles.size()) { data.success = false; data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) }; return data; } ZipEntry ze = new ZipEntry("map.json"); zos.putNextEntry(ze); InputStream in = new ByteArrayInputStream(Utils.jsonToString(map).getBytes("UTF-8")); int len; while ((len = in.read(buf)) >= 0) { zos.write(buf, 0, len); } zos.closeEntry(); zos.close(); } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } catch (JSONException e) { throw new RuntimeException(e); } File zipFile = new File(zipFilename); // step 1.1: if it's over 50MB compressed, it must be upgraded by the user if (zipFile.length() > 50 * 1024 * 1024) { data.success = false; data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_exceeds) }; return data; } // step 2: upload zip file to upgrade service and get token BasicHttpSyncer h = new BasicHttpSyncer(null, null); // note: server doesn't expect it to be gzip compressed, because the zip file is compressed // enable cancelling publishProgress(R.string.upgrade_decks_upload, null, true); try { HttpResponse resp = h.req("upgrade/upload", new FileInputStream(zipFile), 0, false, null, mCancelCallback); if (resp == null && !isCancelled()) { data.success = false; data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) }; return data; } String result; String key = null; if (!isCancelled()) { result = h.stream2String(resp.getEntity().getContent()); if (result != null && result.startsWith("ok:")) { key = result.split(":")[1]; } else { data.success = false; data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) }; return data; } } while (!isCancelled()) { result = h.stream2String(h.req("upgrade/status?key=" + key).getEntity().getContent()); if (result.equals("error")) { data.success = false; data.data = new Object[] { "error" }; return data; } else if (result.startsWith("waiting:")) { publishProgress(R.string.upgrade_decks_upload, result.split(":")[1]); } else if (result.equals("upgrading")) { publishProgress(new Object[] { R.string.upgrade_decks_upgrade_started }); } else if (result.equals("ready")) { break; } else { data.success = false; data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) }; return data; } Thread.sleep(1000); } // step 4: fetch upgraded file. this will return the .anki2 file directly, with // gzip compression if the client says it can handle it if (!isCancelled()) { publishProgress(new Object[] { R.string.upgrade_decks_downloading }); resp = h.req("upgrade/download?key=" + key, null, 6, true, null, mCancelCallback); // uploads/downloads have finished so disable cancelling } publishProgress(R.string.upgrade_decks_downloading, null, false); if (isCancelled()) { return null; } if (resp == null) { data.success = false; data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) }; return data; } // step 5: check the received file is valid InputStream cont = resp.getEntity().getContent(); if (!h.writeToFile(cont, colFilename)) { data.success = false; data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_sdcard, new File(colFilename).length() / 1048576 + 1) }; (new File(colFilename)).delete(); return data; } // check the received file is ok publishProgress(new Object[] { R.string.sync_check_download_file }); publishProgress(R.string.sync_check_download_file); try { AnkiDb d = AnkiDatabaseManager.getDatabase(colFilename); if (!d.queryString("PRAGMA integrity_check").equalsIgnoreCase("ok")) { data.success = false; data.data = new Object[] { sContext.getResources() }; return data; } } finally { AnkiDatabaseManager.closeDatabase(colFilename); } Collection col = AnkiDroidApp.openCollection(colFilename); ArrayList<String> decks = col.getDecks().allNames(false); ArrayList<String> failed = new ArrayList<String>(); ArrayList<File> mediaDirs = new ArrayList<File>(); for (File f : fileList) { String name = f.getName().replaceFirst("\\.anki$", ""); if (!decks.contains(name)) { failed.add(name); } else { mediaDirs.add(new File(f.getAbsolutePath().replaceFirst("\\.anki$", ".media"))); } } File newMediaDir = new File(col.getMedia().getDir()); // step 6. move media files to new media directory publishProgress(new Object[] { R.string.upgrade_decks_media }); ArrayList<String> failedMedia = new ArrayList<String>(); File curMediaDir = null; for (File mediaDir : mediaDirs) { curMediaDir = mediaDir; // Check if media directory exists and is local if (!curMediaDir.exists() || !curMediaDir.isDirectory()) { // If not try finding it in dropbox 1.2.x curMediaDir = new File(AnkiDroidApp.getDropboxDir(), mediaDir.getName()); if (!curMediaDir.exists() || !curMediaDir.isDirectory()) { // No media for this deck continue; } } // Found media dir, copy files for (File m : curMediaDir.listFiles()) { try { Utils.copyFile(m, new File(newMediaDir, m.getName())); } catch (IOException e) { failedMedia.add(curMediaDir.getName().replaceFirst("\\.media$", ".anki")); break; } } } data.data = new Object[] { failed, failedMedia, newMediaDir.getAbsolutePath() }; data.success = true; return data; } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (IllegalStateException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { (new File(zipFilename)).delete(); } }
From source file:org.openmeetings.servlet.outputhandler.BackupExport.java
public void addToZip(File directoryToZip, File file, ZipOutputStream zos) throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream(file); // we want the zipEntry's path to be a relative path that is relative // to the directory being zipped, so chop off the rest of the path String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1, file.getCanonicalPath().length()); log.debug("Writing '" + zipFilePath + "' to zip file"); ZipEntry zipEntry = new ZipEntry(zipFilePath); zos.putNextEntry(zipEntry);/*from w w w . j a v a2 s . com*/ byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } zos.closeEntry(); fis.close(); }
From source file:edu.umd.cs.eclipse.courseProjectManager.TurninProjectAction.java
public void run(IAction action) { // TODO Refactor: Should the places where we raise a dialog and return // could throw an exception instead? String timeOfSubmission = "t" + System.currentTimeMillis(); // Make sure we can get the workbench... IWorkbench workbench = PlatformUI.getWorkbench(); if (workbench == null) { Dialogs.errorDialog(null, "Warning: project submission failed", "Could not submit project", "Internal error: Can't get workbench", IStatus.ERROR); return;// w ww. ja v a2 s .co m } // ...and the workbenchWindow IWorkbenchWindow wwin = workbench.getActiveWorkbenchWindow(); if (wwin == null) { Dialogs.errorDialog(null, "Error submitting project", "Could not submit project", "Internal error: Can't get workbench window", IStatus.ERROR); return; } // Shell to use as parent of dialogs. Shell parent = wwin.getShell(); // Sanity check. if (!(selection instanceof IStructuredSelection)) { Dialogs.errorDialog(parent, "Warning: Selection is Invalid", "Invalid turnin action: You have selected an object that is not a Project. Please select a Project and try again.", "Object selected is not a Project", IStatus.WARNING); return; } IStructuredSelection structured = (IStructuredSelection) selection; Object obj = structured.getFirstElement(); Debug.print("Selection object is a " + obj.getClass().getName() + " @" + System.identityHashCode(obj)); IProject project; if (obj instanceof IProject) { project = (IProject) obj; } else if (obj instanceof IProjectNature) { project = ((IProjectNature) obj).getProject(); } else { Dialogs.errorDialog(null, "Warning: Selection is Invalid", "Invalid turnin action: You have selected an object that is not a Project. Please select a Project and try again.", "Object selected is not a Project", IStatus.WARNING); return; } Debug.print("Got the IProject for the turnin action @" + System.identityHashCode(project)); // ================================= save dirty editors // ======================================== // save dirty editors try { if (!saveDirtyEditors(project, workbench)) { Dialogs.errorDialog(parent, "Submit not performed", "Projects cannot be submitted unless all open files are saved", "Unsaved files prevent submission", IStatus.WARNING); return; } } catch (CoreException e) { Dialogs.errorDialog(parent, "Submit not performed", "Could not turn on cvs management for all project files", e); return; } // ========================= Add all non-ignored files in the project // ========================= IResource[] files; try { Set<IResource> resourceSet = getProjectResources(project); ArrayList<IFile> addedFiles = new ArrayList<IFile>(); for (Iterator<IResource> iter = resourceSet.iterator(); iter.hasNext();) { IResource resource = iter.next(); if (resource instanceof IFile) { IFile file = (IFile) resource; if (!AutoCVSPlugin.isCVSIgnored(file) && !AutoCVSPlugin.isCVSManaged(file)) { addedFiles.add(file); } } } files = (IResource[]) addedFiles.toArray(new IResource[addedFiles.size()]); } catch (CoreException e) { Dialogs.errorDialog(parent, "Submit not performed", "Could not perform submit; unable to find non-ignored resources", e); return; // TODO what to do here? } // ================================= perform CVS commit // ======================================== // TODO Somehow move this into the previous try block // This forces add/commit operations when AutoSync was shut off // Would it just be easier to enable autoSync and then trigger // a resource changed delta, since this method appears to enable // autoSync anyway? // String cvsStatus = "Not performed"; try { cvsStatus = forceCommit(project, files); } catch (Exception e) { Dialogs.errorDialog(parent, "CVS commit not performed as part of submission due to unexpected exception", e.getClass().getName() + " " + e.getMessage(), e); } // ================================= perform CVS tag // ======================================== try { CVSOperations.tagProject(project, timeOfSubmission, CVSOperations.SYNC); } catch (Exception e) { AutoCVSPlugin.getPlugin().getEventLog() .logError("Error tagging submission; submission via the web unlikely to work", e); } // ================================= find properties // ======================================== // find the .submitProject file IResource submitProjectFile = project.findMember(AutoCVSPlugin.SUBMITPROJECT); if (submitProjectFile == null) { Dialogs.errorDialog(parent, "Warning: Project submission not enabled", "Submission is not enabled", "There is no " + AutoCVSPlugin.SUBMITPROJECT + " file for the project", IStatus.ERROR); return; } // Get the properties from the .submit file, and the .submitUser file, // if it exists // or can be fetched from the server Properties allSubmissionProps = null; try { allSubmissionProps = getAllProperties(timeOfSubmission, parent, project, submitProjectFile); } catch (IOException e) { String message = "IOException finding " + AutoCVSPlugin.SUBMITPROJECT + " and " + AutoCVSPlugin.SUBMITUSER + " files; " + cvsStatus; AutoCVSPlugin.getPlugin().getEventLog().logError(message, e); Dialogs.errorDialog(parent, "Submission failed", message, e.getMessage(), IStatus.ERROR); Debug.print("IOException: " + e); return; } catch (CoreException e) { String message = "IOException finding " + AutoCVSPlugin.SUBMITPROJECT + " and " + AutoCVSPlugin.SUBMITUSER + " files; " + cvsStatus; AutoCVSPlugin.getPlugin().getEventLog().logError(message, e); Dialogs.errorDialog(parent, "Submission failed", message, e.getMessage(), IStatus.ERROR); Debug.print("CoreException: " + e); return; } // // THE ACTUAL SUBMIT HAPPENS HERE // try { // ============================== find files to submit // ==================================== Collection<IFile> cvsFiles = findFilesForSubmission(project); // ========================== assemble zip file in byte array // ============================== ByteArrayOutputStream bytes = new ByteArrayOutputStream(4096); ZipOutputStream zipfile = new ZipOutputStream(bytes); zipfile.setComment("zipfile for submission created by CourseProjectManager version " + AutoCVSPlugin.getPlugin().getVersion()); try { byte[] buf = new byte[4096]; for (IFile file : cvsFiles) { if (!file.exists()) { Debug.print("Resource " + file.getName() + " being ignored because it doesn't exist"); continue; } ZipEntry entry = new ZipEntry(file.getProjectRelativePath().toString()); entry.setTime(file.getModificationStamp()); zipfile.putNextEntry(entry); // Copy file data to zip file InputStream in = file.getContents(); try { while (true) { int n = in.read(buf); if (n < 0) break; zipfile.write(buf, 0, n); } } finally { in.close(); } zipfile.closeEntry(); } } catch (IOException e1) { Dialogs.errorDialog(parent, "Warning: Project submission failed", "Unable to zip files for submission\n" + cvsStatus, e1); return; } finally { if (zipfile != null) zipfile.close(); } // ============================== Post to submit server // ==================================== String version = System.getProperties().getProperty("java.runtime.version"); boolean useEasyHttps = version.startsWith("1.3") || version.startsWith("1.2") || version.startsWith("1.4.0") || version.startsWith("1.4.1") || version.startsWith("1.4.2_0") && version.charAt(7) < '5'; if (useEasyHttps) { String submitURL = allSubmissionProps.getProperty("submitURL"); if (submitURL.startsWith("https")) submitURL = "easy" + submitURL; allSubmissionProps.setProperty("submitURL", submitURL); } // prepare multipart post method MultipartPostMethod filePost = new MultipartPostMethod(allSubmissionProps.getProperty("submitURL")); // add properties addAllPropertiesButSubmitURL(allSubmissionProps, filePost); // add filepart byte[] allInput = bytes.toByteArray(); filePost.addPart(new FilePart("submittedFiles", new ByteArrayPartSource("submit.zip", allInput))); // prepare httpclient HttpClient client = new HttpClient(); client.setConnectionTimeout(5000); int status = client.executeMethod(filePost); // Piggy-back uploading the launch events onto submitting. EclipseLaunchEventLog.postEventLogToServer(project); if (status == HttpStatus.SC_OK) { Dialogs.okDialog(parent, "Project submission successful", "Project " + allSubmissionProps.getProperty("projectNumber") + " was submitted successfully\n" + filePost.getResponseBodyAsString()); } else { Dialogs.errorDialog(parent, "Warning: Project submission failed", "Project submission failed", filePost.getStatusText() + "\n " + cvsStatus, IStatus.CANCEL); AutoCVSPlugin.getPlugin().getEventLog().logMessage(filePost.getResponseBodyAsString()); } } catch (CoreException e) { Dialogs.errorDialog(parent, "Warning: Project submission failed", "Project submissions via https failed\n" + cvsStatus, e); } catch (HttpConnection.ConnectionTimeoutException e) { Dialogs.errorDialog(parent, "Warning: Project submission failed", "Project submissions failed", "Connection timeout while trying to connect to submit server\n " + cvsStatus, IStatus.ERROR); } catch (IOException e) { Dialogs.errorDialog(parent, "Warning: Project submission failed", "Project submissions failed\n " + cvsStatus, e); } }
From source file:marytts.tools.voiceimport.VoicePackager.java
/** * Create zip file containing all of the voice files (including the config file, which should be in <b>files</b>). * //from w ww .ja v a 2 s . c o m * @param files * <property, File> Map, e.g. "WaveTimelineMaker.waveTimeline" → * File("VOICE_DIR/mary/timeline_waves.mry") * @return the zip File object * @throws Exception */ protected File createZipFile(HashMap<String, File> files) throws Exception { // TODO this should probably be optimized by using buffered Readers and Writer: byte[] buffer = new byte[4096]; // initialize zip file: String zipFileName = String.format("mary-%s-%s.zip", getVoiceName(), getMaryVersion()); logger.info("Creating voice package " + zipFileName); File zipFile = new File(getMaryBase() + "download" + File.separator + zipFileName); FileOutputStream outputStream = new FileOutputStream(zipFile); ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(zipFile)); // TODO this doesn't explicitly create each ancestor of the voicePath as a separate directory entry in the zip file, but // that doesn't seem necessary: String voicePath = "lib" + File.separator + "voices" + File.separator + getVoiceName() + File.separator; // iterate over files: for (String key : files.keySet()) { File file = files.get(key); // open data file for reading: FileInputStream inputStream = null; try { inputStream = new FileInputStream(file); } catch (FileNotFoundException e) { if (key.equals(EXAMPLETEXTFILE) && getProp(EXAMPLETEXTFILE).length() == 0 && !getVoiceDomain().equalsIgnoreCase("limited")) { logger.debug("Example text file " + getProp(EXAMPLETEXTFILE) + " not found, ignoring."); continue; } else { logger.error("File " + file + " not found!"); throw e; } } // make new entry in zip file, with the appropriate target path: logger.debug("Deflating file " + file); if (key.equals("CONFIG")) { zipStream.putNextEntry(new ZipEntry("conf" + File.separator + file.getName())); } else { zipStream.putNextEntry(new ZipEntry(voicePath + file.getName())); } int len; // stream file contents into zip file: while ((len = inputStream.read(buffer)) > 0) { zipStream.write(buffer, 0, len); } // complete entry and close data file: zipStream.closeEntry(); inputStream.close(); } // close zip file: zipStream.close(); return zipFile; }
From source file:com.pari.pcb.zip.ZIPProcessor.java
private static void writeToZip(File f, String fileName, ZipOutputStream zout) throws IOException { FileInputStream fis = null;//from ww w. j a va 2 s. c o m try { if (f.isDirectory()) { File files[] = f.listFiles(); for (File f1 : files) { writeToZip(f1, fileName + "/" + f1.getName(), zout); } return; } ZipEntry ze = new ZipEntry(fileName); zout.putNextEntry(ze); byte b[] = new byte[1024]; fis = new FileInputStream(f); int n = 0; while ((n = fis.read(b)) >= 0) { zout.write(b, 0, n); } fis.close(); zout.closeEntry(); } finally { try { if (fis != null) { fis.close(); } } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:cz.zcu.kiv.eegdatabase.wui.core.experiments.ExperimentDownloadProvider.java
@Transactional public FileDTO generatePackageFile(ExperimentPackage pckg, MetadataCommand mc, License license, List<Experiment> selectList, Person loggedUser, DownloadPackageManager manager) { ZipOutputStream zipOutputStream = null; FileOutputStream fileOutputStream = null; File tempZipFile = null;//from w w w. j a v a 2s . co m ZipInputStream in = null; File file = null; try { FileDTO dto = new FileDTO(); dto.setFileName(pckg.getName().replaceAll("\\s", "_") + ".zip"); // create temp zip file tempZipFile = File.createTempFile("experimentDownload_", ".zip"); // open stream to temp zip file fileOutputStream = new FileOutputStream(tempZipFile); // prepare zip stream zipOutputStream = new ZipOutputStream(fileOutputStream); for (Experiment tmp : selectList) { Experiment exp = service.getExperimentForDetail(tmp.getExperimentId()); String experimentDirPrefix = ""; // create directory for each experiment. String scenarioName = exp.getScenario().getTitle(); if (scenarioName != null) { experimentDirPrefix = "Experiment_" + exp.getExperimentId() + "_" + scenarioName.replaceAll("\\s", "_") + "/"; } else experimentDirPrefix = "Experiment_data_" + exp.getExperimentId() + "/"; // generate temp zip file with experiment byte[] licenseFile = licenseService.getLicenseAttachmentContent(license.getLicenseId()); file = zipGenerator.generate(exp, mc, exp.getDataFiles(), licenseFile, license.getAttachmentFileName()); in = new ZipInputStream(new FileInputStream(file)); ZipEntry entryIn = null; // copy unziped experiment in package zip file. // NOTE: its easier solution copy content of one zip in anoter instead create directory structure via java.io.File. while ((entryIn = in.getNextEntry()) != null) { zipOutputStream.putNextEntry(new ZipEntry(experimentDirPrefix + entryIn.getName())); IOUtils.copyLarge(in, zipOutputStream); zipOutputStream.closeEntry(); } // mark all temp files for package for delete on exit FileUtils.deleteOnExitQuietly(file); IOUtils.closeQuietly(in); FileUtils.deleteQuietly(file); createHistoryRecordAboutDownload(exp, loggedUser); synchronized (this) { manager.setNumberOfDownloadedExperiments(manager.getNumberOfDownloadedExperiments() + 1); } } dto.setFile(tempZipFile); // no problem detected - close all streams and mark file for delete on exit. // file is deleted after download action FileUtils.deleteOnExitQuietly(tempZipFile); IOUtils.closeQuietly(zipOutputStream); IOUtils.closeQuietly(fileOutputStream); return dto; } catch (Exception e) { log.error(e.getMessage(), e); // problem detected - close all streams, mark files for delete on exit and try delete them. IOUtils.closeQuietly(zipOutputStream); IOUtils.closeQuietly(fileOutputStream); FileUtils.deleteOnExitQuietly(tempZipFile); FileUtils.deleteOnExitQuietly(file); FileUtils.deleteQuietly(tempZipFile); FileUtils.deleteQuietly(file); return null; } }
From source file:ezbake.deployer.publishers.EzAzkabanPublisher.java
/** * This will publish the artifact to Azkaban for scheduled running. The artifact should be of the format * <p/>//ww w .j a va2 s . co m * <p/> * The artifact at this point in time will already have included the SSL certs. * <p/> * Its up to the publisher to reorganize the tar file if needed for its PaaS * * @param artifact The artifact to deploy * @param callerToken - The token of the user or application that initiated this call * @throws DeploymentException - On any exceptions */ @Override public void publish(DeploymentArtifact artifact, EzSecurityToken callerToken) throws DeploymentException { File unzippedPack = null; File azkabanZip = null; ZipOutputStream zipOutputStream = null; String flowName; final BatchJobInfo jobInfo = artifact.getMetadata().getManifest().getBatchJobInfo(); // Get the Azkaban authentication token final AuthenticationResult authenticatorResult; try { authenticatorResult = new AuthenticationManager(new URI(azConf.getAzkabanUrl()), azConf.getUsername(), azConf.getPassword()).login(); } catch (URISyntaxException e) { throw new DeploymentException(e.getMessage()); } if (authenticatorResult.hasError()) { log.error("Could not log into Azkaban: " + authenticatorResult.getError()); throw new DeploymentException(authenticatorResult.getError()); } log.info("Successfully logged into Azkaban. Now creating .zip to upload"); try { // Unzip the artifact unzippedPack = UnzipUtil.unzip(new File(unzipDir), ByteBuffer.wrap(artifact.getArtifact())); log.info("Unzipped artifact to: " + unzippedPack.getAbsolutePath()); // Create a .zip file to submit to Azkaban azkabanZip = File.createTempFile("ezbatch_", ".zip"); log.info("Created temporary zip file: " + azkabanZip.getCanonicalPath()); zipOutputStream = new ZipOutputStream(new FileOutputStream(azkabanZip)); // Copy the configs from the artifact to the top level of the zip. This should contain the Azkaban // .jobs and .properties final String configDir = UnzipUtil.getConfDirectory(unzippedPack).get(); final File configDirFile = new File(configDir); for (File f : FileUtils.listFiles(configDirFile, TrueFileFilter.TRUE, TrueFileFilter.TRUE)) { zipOutputStream.putNextEntry(new ZipArchiveEntry(f.getCanonicalPath().replaceFirst(configDir, ""))); IOUtils.copy(new FileInputStream(f), zipOutputStream); zipOutputStream.closeEntry(); } log.info("Copied configs to the .zip"); // Copy the jars from bin/ in the artifact to lib/ in the .zip file and other things to the jar as needed final String dirPrefix = unzippedPack.getAbsolutePath() + "/bin/"; for (File f : FileUtils.listFiles(new File(dirPrefix), TrueFileFilter.TRUE, TrueFileFilter.TRUE)) { zipOutputStream .putNextEntry(new ZipArchiveEntry(f.getCanonicalPath().replaceFirst(dirPrefix, "lib/"))); final JarInputStream jarInputStream = new JarInputStream(new FileInputStream(f)); final JarOutputStream jarOutputStream = new JarOutputStream(zipOutputStream); JarEntry je; while ((je = jarInputStream.getNextJarEntry()) != null) { jarOutputStream.putNextEntry(je); IOUtils.copy(jarInputStream, jarOutputStream); jarOutputStream.closeEntry(); } log.info("Created Jar file"); // Add the SSL certs to the jar final String sslPath = UnzipUtil.getSSLPath(configDirFile).get(); for (File sslFile : FileUtils.listFiles(new File(sslPath), TrueFileFilter.TRUE, TrueFileFilter.TRUE)) { if (sslFile.isFile()) { jarOutputStream.putNextEntry(new JarArchiveEntry("ssl/" + sslFile.getName())); IOUtils.copy(new FileInputStream(sslFile), jarOutputStream); jarOutputStream.closeEntry(); } } log.info("Added SSL certs to jar"); // Add the application.properties to the jar file so the jobs can read it final File appProps = new File(configDir, "application.properties"); final Properties adjustedProperties = new Properties(); adjustedProperties.load(new FileInputStream(appProps)); adjustedProperties.setProperty("ezbake.security.ssl.dir", "/ssl/"); jarOutputStream.putNextEntry(new JarArchiveEntry("application.properties")); adjustedProperties.store(jarOutputStream, null); jarOutputStream.closeEntry(); jarOutputStream.finish(); zipOutputStream.closeEntry(); } // Check to see if there are any .job files. If there aren't, this is an external job and we need to create // one for the .zip file final Collection<File> jobFiles = FileUtils.listFiles(configDirFile, new String[] { "job" }, false); if (jobFiles.isEmpty()) { // If there are no job files present then we need to create one for the user final StringBuilder sb = new StringBuilder( "type=hadoopJava\n" + "job.class=ezbatch.amino.api.EzFrameworkDriver\n" + "classpath=./lib/*\n" + "main.args=-d /ezbatch/amino/config"); for (File xmlConfig : FileUtils.listFiles(configDirFile, new String[] { "xml" }, false)) { sb.append(" -c ").append(xmlConfig.getName()); } zipOutputStream.putNextEntry(new ZipEntry("Analytic.job")); IOUtils.copy(new StringReader(sb.toString()), zipOutputStream); zipOutputStream.closeEntry(); log.info("There was no .job file so one was created for the .zip"); flowName = "Analytic"; } else { flowName = jobInfo.getFlowName(); if (flowName == null) { log.warn("Manifest did not contain flow_name. Guessing what it should be"); flowName = FilenameUtils.getBaseName(jobFiles.toArray(new File[jobFiles.size()])[0].getName()); log.info("Guessing the flow name should be:" + flowName); } } zipOutputStream.finish(); log.info("Finished creating .zip"); // Now that we've created the zip to upload, attempt to create a project for it to be uploaded to. Every .zip // file needs to be uploaded to a project, and the project may or may not already exist. final String projectName = ArtifactHelpers.getAppId(artifact) + "_" + ArtifactHelpers.getServiceId(artifact); final ProjectManager projectManager = new ProjectManager(authenticatorResult.getSessionId(), new URI(azConf.getAzkabanUrl())); final ManagerResult managerResult = projectManager.createProject(projectName, "EzBatch Deployed"); // If the project already exists, it will return an error, but really it's not a problem if (managerResult.hasError()) { if (!managerResult.getMessage().contains("already exists")) { log.error("Could not create project: " + managerResult.getMessage()); throw new DeploymentException(managerResult.getMessage()); } else { log.info("Reusing the existing project: " + projectName); } } else { log.info("Created new project: " + projectName); log.info("Path: " + managerResult.getPath()); } // Upload the .zip file to the project final UploadManager uploader = new UploadManager(authenticatorResult.getSessionId(), azConf.getAzkabanUrl(), projectName, azkabanZip); final UploaderResult uploaderResult = uploader.uploadZip(); if (uploaderResult.hasError()) { log.error("Could not upload the zip file: " + uploaderResult.getError()); throw new DeploymentException(uploaderResult.getError()); } log.info("Successfully submitted zip file to Azkaban"); // Schedule the jar to run. If the start times aren't provided, it will run in 2 minutes final ScheduleManager scheduler = new ScheduleManager(authenticatorResult.getSessionId(), new URI(azConf.getAzkabanUrl())); // Add the optional parameters if they are present if (jobInfo.isSetStartDate()) { scheduler.setScheduleDate(jobInfo.getStartDate()); } if (jobInfo.isSetStartTime()) { scheduler.setScheduleTime(jobInfo.getStartTime()); } if (jobInfo.isSetRepeat()) { scheduler.setPeriod(jobInfo.getRepeat()); } final SchedulerResult schedulerResult = scheduler.scheduleFlow(projectName, flowName, uploaderResult.getProjectId()); if (schedulerResult.hasError()) { log.error("Failure to schedule job: " + schedulerResult.getError()); throw new DeploymentException(schedulerResult.getError()); } log.info("Successfully scheduled flow: " + flowName); } catch (Exception ex) { log.error("No Nos!", ex); throw new DeploymentException(ex.getMessage()); } finally { IOUtils.closeQuietly(zipOutputStream); FileUtils.deleteQuietly(azkabanZip); FileUtils.deleteQuietly(unzippedPack); } }
From source file:com.netpace.aims.controller.application.WapApplicationHelper.java
public static boolean createWapImagesZipFile(File imagesZipFile, String imageFileName, Long wapApplicationID, Long allianceID) throws AimsException, HibernateException { byte[] buf = new byte[1024];// Create a buffer for reading the files boolean zipCreated = false; ZipOutputStream zipOut = null; InputStream imgInput = null;/*from w ww.j a va 2s . co m*/ //general exception for zip creation AimsException aimsException = new AimsException("Error"); aimsException.addException(new AimsException("error.wap.app.zip.create")); String fileExtension = ""; String fName = ""; String[] imagesDirNames = new String[] { "img_174", "img_240", "img_320" }; //if images not found, then this statement throws aims exception AimsTempFile[] wapImageFiles = WapApplicationHelper.getImagesTempFiles(wapApplicationID, allianceID); try { zipOut = new ZipOutputStream(new FileOutputStream(imagesZipFile)); // Compress the files for (int i = 0; i < wapImageFiles.length; i++) { try { imgInput = wapImageFiles[i].getTempFile().getBinaryStream(); //get extension of filename in lower case fName = wapImageFiles[i].getTempFileName(); fileExtension = (fName.substring(fName.lastIndexOf("."))).toLowerCase(); // Add ZIP entry to output stream. dirName/filename+extension zipOut.putNextEntry(new ZipEntry(imagesDirNames[i] + "/" + (imageFileName + fileExtension))); // Transfer bytes from the file to the ZIP file int len; while ((len = imgInput.read(buf)) > 0) { zipOut.write(buf, 0, len); } } //end try catch (SQLException sqle) { System.out.println("Problem in getting image file as stream"); sqle.printStackTrace(); // throw aimsException; } catch (IOException e) { System.out.println("Problem in adding zip entry"); e.printStackTrace(); throw aimsException; } finally { // Complete the entry try { zipOut.closeEntry(); imgInput.close(); } catch (IOException ioe) { System.out.println("Error closing zip entry or input stream for image"); ioe.printStackTrace(); } } //end finally } //end for zipCreated = true; log.debug("wap images zip file created for ftp transfer: " + imagesZipFile.getName() + " in directory: " + imagesZipFile.getAbsolutePath()); } catch (FileNotFoundException e) { System.out.println(imagesZipFile + " not found in temp directory: " + imagesZipFile.getAbsolutePath()); e.printStackTrace();//zip file not found throw aimsException; } finally { try { if (zipOut != null) { zipOut.close(); } } catch (IOException e) { System.out.println("Error closing zip stream"); e.printStackTrace(); System.out.println("deleting zip file: " + imagesZipFile.delete()); throw aimsException; } } return zipCreated; }