List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream closeArchiveEntry
public void closeArchiveEntry() throws IOException
From source file:org.opengion.fukurou.util.ZipArchive.java
/** * ZIP????/*from www. j ava 2 s. com*/ * ???File????????? * ??????????????? * ?????? * * @param list ZIP??? * @param zos ZIPOutputStream * @param prefix ? * @param files ?? * @throws IOException ???? * @og.rev 4.1.0.2 (2008/02/01) ? * @og.rev 5.1.9.0 (2010/08/01) ? ?BufferedInputStream ?????? */ private static void addZipEntry(final List<File> list, final ZipArchiveOutputStream zos, final String prefix, final File[] files) { File tmpFile = null; try { for (File fi : files) { tmpFile = fi; // ? list.add(fi); if (fi.isDirectory()) { String entryName = prefix + fi.getName() + "/"; ZipArchiveEntry zae = new ZipArchiveEntry(entryName); zos.putArchiveEntry(zae); zos.closeArchiveEntry(); addZipEntry(list, zos, entryName, fi.listFiles()); } else { String entryName = prefix + fi.getName(); ZipArchiveEntry zae = new ZipArchiveEntry(entryName); zos.putArchiveEntry(zae); InputStream is = new BufferedInputStream(new FileInputStream(fi)); IOUtils.copy(is, zos); zos.closeArchiveEntry(); Closer.ioClose(is); } } } catch (FileNotFoundException ex) { String errMsg = "??????[??=" + tmpFile + "]"; throw new RuntimeException(errMsg, ex); } catch (IOException ex) { String errMsg = "ZIP?????[??=" + tmpFile + "]"; throw new RuntimeException(errMsg, ex); } }
From source file:org.orderofthebee.addons.support.tools.repo.LogFilesZIPPost.java
/** * * {@inheritDoc}/*from w w w. j a v a 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.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 {//from ww w .j a va 2 s .c om 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 . j a v a2 s.c om*/ */ @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.owasp.dependencytrack.util.ZipUtil.java
/** * Creates a zip entry for the path specified with a name built from the base passed in and the file/directory * name. If the path is a directory, a recursive call is made such that the full directory is added to the zip. * * @param zOut The zip file's output stream * @param path The filesystem path of the file/directory being added * @param base The base prefix to for the name of the zip file entry * * @throws IOException If anything goes wrong *//*from w w w . ja va 2s . c o m*/ private static void addFileToZip(ZipArchiveOutputStream zOut, String path, String base) throws IOException { final File f = new File(path); final String entryName = base + f.getName(); final ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName); zOut.putArchiveEntry(zipEntry); if (f.isFile()) { FileInputStream fInputStream = null; try { fInputStream = new FileInputStream(f); IOUtils.copy(fInputStream, zOut); zOut.closeArchiveEntry(); } finally { IOUtils.closeQuietly(fInputStream); } } else { zOut.closeArchiveEntry(); final File[] children = f.listFiles(); if (children != null) { for (File child : children) { addFileToZip(zOut, child.getAbsolutePath(), entryName + "/"); } } } }
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 {/*w w w. ja va 2s. 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 w ww . j a v a 2s . c om*/ * * @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 final void zip(File directory, File base, ZipArchiveOutputStream zos) throws IOException { File[] files = directory.listFiles(); for (int i = 0, n = files.length; i < n; i++) { if (files[i].isDirectory()) { zip(files[i], base, zos);/* w ww . ja va 2s.c o m*/ } else { FileInputStream in = null; try { in = new FileInputStream(files[i]); ZipArchiveEntry entry = new ZipArchiveEntry( files[i].getPath().substring(base.getPath().length() + 1)); zos.putArchiveEntry(entry); IOUtils.copy(in, zos); zos.closeArchiveEntry(); } catch (IOException e) { throw e; } finally { if (in != null) { in.close(); } } } } }
From source file:org.pepstock.jem.util.ZipUtil.java
/** * Creates a zip entry for all files and/or directories of main folder * // w w w . jav a2 s . c o m * @param zipArchiveOutputStream zip output stream * @param file The file being added * @param path is relative path from main folder * * @throws IOException if any error occurs */ private static void addFileToZip(ZipArchiveOutputStream zipArchiveOutputStream, File file, String path) throws IOException { // at first call it is the folder, otherwise is the relative path String entryName = (path != null) ? path + file.getName() : file.getName(); ZipArchiveEntry zipEntry = new ZipArchiveEntry(file, entryName); zipArchiveOutputStream.putArchiveEntry(zipEntry); // if is a file, add the content to zip file if (file.isFile()) { FileInputStream fInputStream = null; try { fInputStream = new FileInputStream(file); IOUtils.copy(fInputStream, zipArchiveOutputStream); zipArchiveOutputStream.closeArchiveEntry(); } finally { IOUtils.closeQuietly(fInputStream); } } else { // is a directory so it calls recursively all files in folder zipArchiveOutputStream.closeArchiveEntry(); File[] children = file.listFiles(); if (children != null) { for (File child : children) { addFileToZip(zipArchiveOutputStream, child, entryName + "/"); } } } }
From source file:org.sigmah.server.file.impl.BackupArchiveJob.java
/** * <p>/*ww w. ja va2s. c o m*/ * Recursively browses the given {@code root} repository elements to populate the given {@code zipOutputStream} with * corresponding files. * </p> * <p> * If a referenced file cannot be found in the storage folder, it will be ignored (a {@code WARN} log is generated). * </p> * * @param root * The root repository element. * @param zipOutputStream * The stream to populate with files hierarchy. * @param actualPath * The current repository path. */ private void zipRepository(final RepositoryElement root, final ZipArchiveOutputStream zipOutputStream, final String actualPath) { final String path = (actualPath.equals("") ? root.getName() : actualPath + "/" + root.getName()); if (root instanceof FileElement) { final FileElement file = (FileElement) root; final String fileStorageId = file.getStorageId(); if (fileStorageProvider.exists(fileStorageId)) { try (final InputStream is = new BufferedInputStream(fileStorageProvider.open(fileStorageId), ResponseHelper.BUFFER_SIZE)) { zipOutputStream.putArchiveEntry(new ZipArchiveEntry(path)); final byte data[] = new byte[ResponseHelper.BUFFER_SIZE]; while ((is.read(data)) != -1) { zipOutputStream.write(data); } zipOutputStream.closeArchiveEntry(); } catch (final IOException e) { LOG.warn("File '" + fileStorageId + "' cannot be found ; continuing with next file.", e); } } else { LOG.warn("File '{0}' does not exists on the server ; continuing with next file.", fileStorageId); } } else if (root instanceof FolderElement) { final FolderElement folder = (FolderElement) root; for (final RepositoryElement element : folder.getChildren()) { zipRepository(element, zipOutputStream, path); } } }