List of usage examples for java.util.zip ZipOutputStream close
public void close() throws IOException
From source file:com.joliciel.talismane.parser.ParsingConstrainerImpl.java
@Override public void onCompleteParse() { if (this.transitionSystem == null) this.transitionSystem = TalismaneSession.getTransitionSystem(); if (this.file != null) { try {/*from www .j av a2 s. com*/ ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file, false)); zos.putNextEntry(new ZipEntry("ParsingConstrainer.obj")); ObjectOutputStream oos = new ObjectOutputStream(zos); try { oos.writeObject(this); } finally { oos.flush(); } zos.flush(); zos.close(); } catch (IOException ioe) { LogUtils.logError(LOG, ioe); throw new RuntimeException(ioe); } } }
From source file:org.cloudfoundry.client.lib.io.DynamicZipInputStreamTest.java
@Test public void shouldCreateValidZipContent() throws Exception { byte[] f1 = newRandomBytes(10000); byte[] f2 = newRandomBytes(10000); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(bos); zipOutputStream.putNextEntry(new ZipEntry("a/b/c")); zipOutputStream.write(f1);/*from w w w .j a va2 s . c o m*/ zipOutputStream.closeEntry(); zipOutputStream.putNextEntry(new ZipEntry("a/b/c/d/")); zipOutputStream.closeEntry(); zipOutputStream.putNextEntry(new ZipEntry("d/e/f")); zipOutputStream.write(f2); zipOutputStream.closeEntry(); zipOutputStream.flush(); zipOutputStream.close(); byte[] expected = bos.toByteArray(); List<DynamicZipInputStream.Entry> entries = new ArrayList<DynamicZipInputStream.Entry>(); entries.add(newEntry("a/b/c", f1)); entries.add(newEntry("a/b/c/d/", null)); entries.add(newEntry("d/e/f", f2)); DynamicZipInputStream inputStream = new DynamicZipInputStream(entries); bos.reset(); FileCopyUtils.copy(inputStream, bos); byte[] actual = bos.toByteArray(); assertThat(actual, is(equalTo(expected))); }
From source file:org.cloudfoundry.tools.io.zip.ZipArchiveTest.java
@Test public void shouldReloadIfChanged() throws Exception { File file = this.zip.getFile("a/b/c.txt"); assertThat(file.getContent().asString(), is("c")); ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(this.zipFile)); try {//www. jav a 2 s . c o m zipOutputStream.putNextEntry(new ZipEntry("/a/b/c.txt")); zipOutputStream.write("c2".getBytes()); } finally { zipOutputStream.close(); } assertThat(file.getContent().asString(), is("c2")); }
From source file:it.cnr.icar.eric.client.xml.registry.RegistryFacadeImpl.java
@SuppressWarnings({ "unused", "static-access" }) public ExtrinsicObject publishFilesAsZip(String baseDirectory, String[] relativeFilePaths, Map<?, ?> metadata) throws JAXRException { ExtrinsicObjectImpl eo = null;// ww w .j ava 2 s .c o m if (getLifeCycleManager() == null) { throw new JAXRException("setEndpoint MUST be called before setCredentials is called."); } try { //Create the zip file File zipFile = File.createTempFile("eric-RegistryFacadeImpl", ".zip"); zipFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = Utility.createZipOutputStream(baseDirectory, relativeFilePaths, fos); zos.close(); javax.activation.DataSource ds = new javax.activation.FileDataSource(zipFile); javax.activation.DataHandler dh = new javax.activation.DataHandler(ds); if (dh == null) { throw new JAXRException("Error processing zip file" + zipFile.getAbsolutePath()); } eo = (ExtrinsicObjectImpl) getLifeCycleManager().createExtrinsicObject(dh); String id = (String) metadata.remove(bu.CANONICAL_SLOT_IDENTIFIABLE_ID); String name = (String) metadata.remove(bu.CANONICAL_SLOT_REGISTRY_OBJECT_NAME); String description = (String) metadata.remove(bu.CANONICAL_SLOT_REGISTRY_OBJECT_DESCRIPTION); String objectType = (String) metadata.remove(bu.CANONICAL_SLOT_REGISTRY_OBJECT_OBJECTTYPE); //If id is specified then use it. if (id != null) { eo.setKey(lcm.createKey(id)); eo.setLid(id); } String eoId = eo.getKey().getId(); //If name is specified then use it. if (name != null) { eo.setName(lcm.createInternationalString(name)); } //If description is specified then use it. if (description != null) { eo.setDescription(lcm.createInternationalString(description)); } if (objectType == null) { throw new InvalidRequestException( JAXRResourceBundle.getInstance().getString("message.error.missingMetadata", new Object[] { bu.CANONICAL_SLOT_REGISTRY_OBJECT_OBJECTTYPE })); } eo.setMimeType("application/zip"); eo.setObjectTypeRef(new RegistryObjectRef(getLifeCycleManager(), objectType)); ArrayList<RegistryObject> objects = new ArrayList<RegistryObject>(); objects.add(eo); //??Turn of versioning on save as it creates problems BulkResponse br = getLifeCycleManager().saveObjects(objects, dontVersionSlotsMap); if (br.getExceptions() != null) { throw new JAXRException("Error publishing to registry", (Exception) (br.getExceptions().toArray()[0])); } if (br.getStatus() != BulkResponse.STATUS_SUCCESS) { throw new JAXRException("Error publishing to registry. See server logs for detils."); } } catch (IOException e) { throw new JAXRException(e); } return eo; }
From source file:com.polyvi.xface.extension.zip.XZipExt.java
/** * ?/* w ww .j a va 2 s . c om*/ * * @param srcFileURL * URL * @param zipFileURL * ?zip??URL * @throws FileNotFoundException * @throws IOException * @throws IllegalArgumentException */ private void zipDir(String srcFileURL, String zipFileURL) throws FileNotFoundException, IOException, IllegalArgumentException { Uri srcFileUri = getFileUri(srcFileURL); Uri dstFileUri = getFileUri(zipFileURL); if (areFilesSame(srcFileUri, dstFileUri)) { throw new IllegalArgumentException(); } prepareForZipDir(dstFileUri); ZipOutputStream zos = new ZipOutputStream(mResourceApi.openOutputStream(dstFileUri)); compressDir(srcFileUri, zos, ""); zos.close(); }
From source file:com.coinblesk.client.backup.BackupDialogFragment.java
private void backup() { final File backupFile = newWalletBackupFile(); final String password = txtPassword.getText().toString(); Preconditions.checkState(password.equals(txtPasswordAgain.getText().toString()) && password.length() > 0); clearPasswordInput();/* w w w.j a v a 2 s .co m*/ Writer fileOut = null; try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ZipOutputStream zos = new ZipOutputStream(baos); Log.i(TAG, "Backup file: " + backupFile); // assemble all content to backup addFilesToZip(zos); // encrypt zip bytes and write to file zos.close(); byte[] plainBackup = baos.toByteArray(); String encryptedBackup = EncryptionUtils.encrypt(plainBackup, password.toCharArray()); fileOut = new OutputStreamWriter(new FileOutputStream(backupFile), Charsets.UTF_8); fileOut.write(encryptedBackup); fileOut.flush(); Log.i(TAG, "Wallet backup finished. File = [" + backupFile + "], size = " + backupFile.length() + "bytes, exists = " + backupFile.exists()); // ask user whether he wants backup file as mail attachment showBackupCompletedDialog(backupFile); } catch (Exception e) { Log.w(TAG, "Could not write to file", e); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.fragment_backup_failed_title) .setMessage(getString(R.string.fragment_backup_failed_message) + ": " + e.getMessage()) .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }).create().show(); } finally { if (fileOut != null) { try { fileOut.close(); } catch (IOException e) { Log.i(TAG, "Could not close output stream"); } } } }
From source file:edu.isi.wings.catalog.component.api.impl.oodt.ComponentCreationFM.java
private File zipDirectory(File srcFile) { try {//www. jav a 2 s . c om File zipFile = File.createTempFile("comp-", ".zip"); FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); this.addDirToArchive(zos, srcFile); zos.close(); return zipFile; } catch (IOException ioe) { ioe.printStackTrace(); } return null; }
From source file:com.eviware.soapui.impl.wsdl.support.FileAttachment.java
public void cacheFileLocally(File file) throws FileNotFoundException, IOException { // write attachment-data to tempfile ByteArrayOutputStream data = new ByteArrayOutputStream(); ZipOutputStream out = new ZipOutputStream(data); out.putNextEntry(new ZipEntry(config.getName())); InputStream in = new FileInputStream(file); long sz = file.length(); config.setSize(sz);/* ww w . ja v a 2 s . com*/ Tools.writeAll(out, in); in.close(); out.closeEntry(); out.finish(); out.close(); data.close(); config.setData(data.toByteArray()); }
From source file:net.sourceforge.fenixedu.presentationTier.Action.externalServices.epfl.ExportPhdIndividualProgramProcessesInHtml.java
static byte[] createZip(final PhdProgramPublicCandidacyHashCode hashCode) { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = null; try {//from www .ja va 2 s. co m zip = new ZipOutputStream(outputStream); int count = 1; for (final PhdProgramProcessDocument document : hashCode.getIndividualProgramProcess() .getCandidacyProcessDocuments()) { final ZipEntry zipEntry = new ZipEntry(count + "-" + document.getFilename()); zip.putNextEntry(zipEntry); // TODO: use in local context copy(new ByteArrayInputStream(new // byte[20]), zip); copy(document.getStream(), zip); zip.closeEntry(); count++; } } catch (FileNotFoundException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error(e.getMessage(), e); } finally { if (zip != null) { try { zip.flush(); zip.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } } } return outputStream.toByteArray(); }
From source file:ZipImploder.java
protected void process(ZipOutputStream zos, File dir) throws IOException { try {/*from w w w. ja v a2 s . c o m*/ processDir(zos, dir); } finally { zos.close(); } }