List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream close
public void close() throws IOException
From source file:org.ngrinder.common.util.CompressionUtils.java
/** * Zip the given src into the given output stream. * * @param src src to be zipped/*w w w.j ava 2 s . co m*/ * @param os output stream * @param charsetName character set to be used * @param includeSrc true if src will be included. * @throws IOException IOException */ public static void zip(File src, OutputStream os, String charsetName, boolean includeSrc) throws IOException { ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setEncoding(charsetName); FileInputStream fis = null; int length; ZipArchiveEntry ze; byte[] buf = new byte[8 * 1024]; String name; Stack<File> stack = new Stack<File>(); File root; if (src.isDirectory()) { if (includeSrc) { stack.push(src); root = src.getParentFile(); } else { File[] fs = checkNotNull(src.listFiles()); for (int i = 0; i < fs.length; i++) { stack.push(fs[i]); } root = src; } } else { stack.push(src); root = src.getParentFile(); } while (!stack.isEmpty()) { File f = stack.pop(); name = toPath(root, f); if (f.isDirectory()) { File[] fs = checkNotNull(f.listFiles()); for (int i = 0; i < fs.length; i++) { if (fs[i].isDirectory()) { stack.push(fs[i]); } else { stack.add(0, fs[i]); } } } else { ze = new ZipArchiveEntry(name); zos.putArchiveEntry(ze); try { fis = new FileInputStream(f); while ((length = fis.read(buf, 0, buf.length)) >= 0) { zos.write(buf, 0, length); } } finally { IOUtils.closeQuietly(fis); } zos.closeArchiveEntry(); } } zos.close(); }
From source file:org.ngrinder.script.util.CompressionUtil.java
public void zip(File src, OutputStream os, String charsetName, boolean includeSrc) throws IOException { ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setEncoding(charsetName);//from w w w .j av a 2s .com FileInputStream fis; int length; ZipArchiveEntry ze; byte[] buf = new byte[8 * 1024]; String name; Stack<File> stack = new Stack<File>(); File root; if (src.isDirectory()) { if (includeSrc) { stack.push(src); root = src.getParentFile(); } else { File[] fs = src.listFiles(); for (int i = 0; i < fs.length; i++) { stack.push(fs[i]); } root = src; } } else { stack.push(src); root = src.getParentFile(); } while (!stack.isEmpty()) { File f = stack.pop(); name = toPath(root, f); if (f.isDirectory()) { File[] fs = f.listFiles(); for (int i = 0; i < fs.length; i++) { if (fs[i].isDirectory()) stack.push(fs[i]); else stack.add(0, fs[i]); } } else { ze = new ZipArchiveEntry(name); zos.putArchiveEntry(ze); fis = new FileInputStream(f); while ((length = fis.read(buf, 0, buf.length)) >= 0) { zos.write(buf, 0, length); } fis.close(); zos.closeArchiveEntry(); } } zos.close(); }
From source file:org.openengsb.connector.git.internal.GitServiceImpl.java
@Override public File export() { try {// w ww . j av a 2s . c om if (repository == null) { initRepository(); } LOGGER.debug("Exporting repository to archive"); File tmp = File.createTempFile("repository", ".zip"); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(tmp); packRepository(localWorkspace, zos); zos.close(); return tmp; } catch (IOException e) { throw new ScmException(e); } }
From source file:org.openengsb.connector.git.internal.GitServiceImpl.java
@Override public File export(CommitRef ref) { RevWalk rw = null;/* w w w .j a v a 2 s.c o m*/ File tmp = null; try { if (repository == null) { initRepository(); } LOGGER.debug("Resolving HEAD and reference [{}]", ref.getStringRepresentation()); AnyObjectId headId = repository.resolve(Constants.HEAD); AnyObjectId refId = repository.resolve(ref.getStringRepresentation()); if (headId == null || refId == null) { throw new ScmException("HEAD or reference [" + ref.getStringRepresentation() + "] doesn't exist."); } rw = new RevWalk(repository); RevCommit head = rw.parseCommit(headId); RevCommit commit = rw.parseCommit(refId); LOGGER.debug("Checking out working copy of revision"); checkoutIndex(commit); tmp = File.createTempFile("repository", ".zip"); LOGGER.debug("Exporting repository to archive"); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(tmp); packRepository(localWorkspace, zos); zos.close(); LOGGER.debug("Checking out working copy of former HEAD revision"); checkoutIndex(head); } catch (IOException e) { throw new ScmException(e); } finally { if (rw != null) { rw.release(); } } return tmp; }
From source file:org.orderofthebee.addons.support.tools.repo.LogFilesZIPPost.java
/** * * {@inheritDoc}//from w w w . ja v a2 s . com */ @Override public void execute(final WebScriptRequest req, final WebScriptResponse res) throws IOException { final Map<String, Object> model = new HashMap<>(); final Status status = new Status(); final Cache cache = new Cache(this.getDescription().getRequiredCache()); model.put("status", status); model.put("cache", cache); final Object parsedContent = req.parseContent(); if (!(parsedContent instanceof FormData)) { throw new WebScriptException(Status.STATUS_BAD_REQUEST, "No or invalid request data provided - only form data is supported"); } final FormData rqData = (FormData) parsedContent; final List<String> filePaths = new ArrayList<>(); final String[] paths = rqData.getParameters().get("paths"); filePaths.addAll(Arrays.asList(paths)); final List<File> files = this.validateFilePaths(filePaths); final File logFileZip = TempFileProvider.createTempFile("ootbee-support-tools-logFiles", "zip"); try { try { final ZipArchiveOutputStream zipOS = new ZipArchiveOutputStream(logFileZip); try { for (final File logFile : files) { final ArchiveEntry archiveEntry = zipOS.createArchiveEntry(logFile, logFile.getName()); zipOS.putArchiveEntry(archiveEntry); final FileInputStream fis = new FileInputStream(logFile); try { final byte[] buf = new byte[10240]; while (fis.read(buf) != -1) { zipOS.write(buf); } } finally { fis.close(); } zipOS.closeArchiveEntry(); } } finally { zipOS.close(); } } catch (final IOException ioEx) { throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Error creating ZIP file", ioEx); } this.delegate.streamContent(req, res, logFileZip, logFileZip.lastModified(), false, "log-files.zip", model); } finally { // eager cleanup if (!logFileZip.delete()) { logFileZip.deleteOnExit(); } } }
From source file:org.orderofthebee.addons.support.tools.share.LogFileHandler.java
protected void createZip(final List<File> files, final File logFileZip) { try {/* w ww . jav a2s . c o m*/ final ZipArchiveOutputStream zipOS = new ZipArchiveOutputStream(logFileZip); try { for (final File logFile : files) { final ArchiveEntry archiveEntry = zipOS.createArchiveEntry(logFile, logFile.getName()); zipOS.putArchiveEntry(archiveEntry); final FileInputStream fis = new FileInputStream(logFile); try { final byte[] buf = new byte[10240]; while (fis.read(buf) != -1) { zipOS.write(buf); } } finally { fis.close(); } zipOS.closeArchiveEntry(); } } finally { zipOS.close(); } } catch (final IOException ioEx) { throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Error creating ZIP file", ioEx); } }
From source file:org.orderofthebee.addons.support.tools.share.LogFilesZIPPost.java
/** * * {@inheritDoc}// www. ja va 2 s . co m */ @Override public void execute(final WebScriptRequest req, final WebScriptResponse res) throws IOException { final Map<String, Object> model = new HashMap<>(); final Status status = new Status(); final Cache cache = new Cache(this.getDescription().getRequiredCache()); model.put("status", status); model.put("cache", cache); final Object parsedContent = req.parseContent(); if (!(parsedContent instanceof FormData)) { throw new WebScriptException(Status.STATUS_BAD_REQUEST, "No or invalid request data provided - only form data is supported"); } final FormData rqData = (FormData) parsedContent; final List<String> filePaths = new ArrayList<>(); final String[] paths = rqData.getParameters().get("paths"); filePaths.addAll(Arrays.asList(paths)); final List<File> files = this.validateFilePaths(filePaths); final File logFileZip = TempFileProvider.createTempFile("ootbee-support-tools-logFiles", "zip"); try { try { final ZipArchiveOutputStream zipOS = new ZipArchiveOutputStream(logFileZip); try { for (final File logFile : files) { final ArchiveEntry archiveEntry = zipOS.createArchiveEntry(logFile, logFile.getName()); zipOS.putArchiveEntry(archiveEntry); final FileInputStream fis = new FileInputStream(logFile); try { final byte[] buf = new byte[10240]; while (fis.read(buf) != -1) { zipOS.write(buf); } } finally { fis.close(); } zipOS.closeArchiveEntry(); } } finally { zipOS.close(); } } catch (final IOException ioEx) { throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Error creating ZIP file", ioEx); } this.streamContent(req, res, logFileZip, logFileZip.lastModified(), false, "log-files.zip", model, "application/zip"); } finally { // eager cleanup if (!logFileZip.delete()) { logFileZip.deleteOnExit(); } } }
From source file:org.panbox.core.identitymgmt.VCardProtector.java
public static void protectVCF(File targetFile, File vCardFile, char[] password) throws Exception { ZipArchiveOutputStream out = null; try {//from w w w. j a v a 2 s .c o m out = new ZipArchiveOutputStream(new FileOutputStream(targetFile)); byte[] vCardData = IOUtils.toByteArray(new FileInputStream(vCardFile)); byte[] passwordBytes = Utils.toBytes(password); vcfMac.init(new SecretKeySpec(passwordBytes, KeyConstants.VCARD_HMAC)); byte[] hmac = vcfMac.doFinal(vCardData); String fileName = Utils.bytesToHex(hmac); // first entry is the vcard itself ZipArchiveEntry entry = new ZipArchiveEntry(vCardFile.getName()); entry.setSize(vCardData.length); out.putArchiveEntry(entry); out.write(vCardData); out.flush(); out.closeArchiveEntry(); // second entry is the hmac value entry = new ZipArchiveEntry(fileName); entry.setSize(fileName.length()); out.putArchiveEntry(entry); out.closeArchiveEntry(); out.flush(); } catch (IOException | InvalidKeyException e) { logger.error("Could not create protected VCF export file!", e); throw e; } finally { if (out != null) { out.flush(); out.close(); } } }
From source file:org.panbox.core.pairing.file.PanboxFilePairingUtils.java
/** * Stores a pairing file at the specified path for the specified device and * type/*from www .j a v a 2s .co m*/ * * @param outputFile * Pairing file to be saved * @param devicename * Name of the device that should be paired * @param password * Password of the identity */ public static PanboxFilePairingWriteReturnContainer storePairingFile(File outputFile, String devicename, char[] password, PairingType type, DeviceType devType, String eMail, String firstName, String lastName, PrivateKey privEncKey, X509Certificate encCert, PrivateKey privSignKey, X509Certificate signCert, Map<String, X509Certificate> devices, Collection<VCard> contacts) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException { logger.debug("PanboxFilePairingUtils : storePairingFile : Storing pairing container to: " + outputFile.getAbsolutePath()); ZipArchiveOutputStream out = new ZipArchiveOutputStream(new FileOutputStream(outputFile)); // 1. add device name to pairing file ZipArchiveEntry entry = new ZipArchiveEntry("devicename"); entry.setSize(devicename.getBytes().length); out.putArchiveEntry(entry); out.write(devicename.getBytes()); out.flush(); out.closeArchiveEntry(); // 2. add device name to pairing file entry = new ZipArchiveEntry("email"); entry.setSize(eMail.getBytes().length); out.putArchiveEntry(entry); out.write(eMail.getBytes()); out.flush(); out.closeArchiveEntry(); // 3. add device name to pairing file entry = new ZipArchiveEntry("firstname"); entry.setSize(firstName.getBytes().length); out.putArchiveEntry(entry); out.write(firstName.getBytes()); out.flush(); out.closeArchiveEntry(); // 4. add device name to pairing file entry = new ZipArchiveEntry("lastname"); entry.setSize(lastName.getBytes().length); out.putArchiveEntry(entry); out.write(lastName.getBytes()); out.flush(); out.closeArchiveEntry(); // 5. generate and add a new device key + cert for the newly device KeyPair devKey = CryptCore.generateKeypair(); X509Certificate devCert = CryptCore.createSelfSignedX509Certificate(devKey.getPrivate(), devKey.getPublic(), new PairingIPersonDummy(eMail, firstName, lastName)); KeyStore devKeyStore = KeyStore.getInstance("PKCS12"); devKeyStore.load(null, null); devKeyStore.setKeyEntry(devicename, (Key) devKey.getPrivate(), password, new Certificate[] { devCert }); ByteArrayOutputStream baos = new ByteArrayOutputStream(); devKeyStore.store(baos, password); baos.flush(); byte[] data = baos.toByteArray(); entry = new ZipArchiveEntry("devicekey.p12"); entry.setSize(data.length); out.putArchiveEntry(entry); out.write(data); out.flush(); out.closeArchiveEntry(); // 6. add device certs and names for all known devices baos = new ByteArrayOutputStream(); ByteArrayOutputStream deviceNamesFile = new ByteArrayOutputStream(); KeyStore deviceKeyStore = KeyStore.getInstance("BKS"); deviceKeyStore.load(null, null); int i = 0; for (Entry<String, X509Certificate> device : devices.entrySet()) { deviceKeyStore.setCertificateEntry("device" + i, device.getValue()); deviceNamesFile.write(("device" + i + DELIMITER + device.getKey() + "\n").getBytes()); ++i; } deviceKeyStore.store(baos, password); baos.flush(); deviceNamesFile.flush(); byte[] data2 = deviceNamesFile.toByteArray(); entry = new ZipArchiveEntry("knownDevices.list"); entry.setSize(data2.length); out.putArchiveEntry(entry); out.write(data2); out.flush(); data = baos.toByteArray(); entry = new ZipArchiveEntry("knownDevices.bks"); entry.setSize(data.length); out.putArchiveEntry(entry); out.write(data); out.flush(); // 7. add vcard for all known contacts File tempContacts = File.createTempFile("panboxContacts", null); AbstractAddressbookManager.exportContacts(contacts, tempContacts); FileInputStream fis = new FileInputStream(tempContacts); data = new byte[(int) tempContacts.length()]; fis.read(data); fis.close(); tempContacts.delete(); entry = new ZipArchiveEntry("contacts.vcard"); entry.setSize(data.length); out.putArchiveEntry(entry); out.write(data); out.flush(); // 8. add owner certs or keys in case of main/restricted KeyStore ownerKeyStore = null; if (type == PairingType.MASTER) { ownerKeyStore = KeyStore.getInstance("PKCS12"); ownerKeyStore.load(null, null); ownerKeyStore.setKeyEntry("ownerEncKey", privEncKey, password, new Certificate[] { encCert }); ownerKeyStore.setKeyEntry("ownerSignKey", privSignKey, password, new Certificate[] { signCert }); entry = new ZipArchiveEntry("ownerKeys.p12"); } else { ownerKeyStore = KeyStore.getInstance("BKS"); ownerKeyStore.load(null, null); ownerKeyStore.setCertificateEntry("ownerEncCert", encCert); ownerKeyStore.setCertificateEntry("ownerSignCert", signCert); entry = new ZipArchiveEntry("ownerCerts.bks"); } baos = new ByteArrayOutputStream(); ownerKeyStore.store(baos, password); baos.flush(); data = baos.toByteArray(); entry.setSize(data.length); out.putArchiveEntry(entry); out.write(data); out.flush(); out.closeArchiveEntry(); out.flush(); out.close(); logger.debug("PanboxFilePairingUtils : storePairingFile : Storing pairing container finished."); return new PanboxFilePairingWriteReturnContainer(devicename, devCert, devType); }
From source file:org.pepstock.jem.commands.CreateNode.java
private static void zipDirectory(File directoryPath, File zipPath) throws IOException { FileOutputStream fOut = new FileOutputStream(zipPath); BufferedOutputStream bOut = new BufferedOutputStream(fOut); ZipArchiveOutputStream tOut = new ZipArchiveOutputStream(bOut); zip(directoryPath, directoryPath, tOut); tOut.finish();/*from w w w. jav a 2 s . co m*/ tOut.close(); bOut.close(); fOut.close(); }