List of usage examples for java.util.zip ZipOutputStream putNextEntry
public void putNextEntry(ZipEntry e) throws IOException
From source file:com.aurel.track.admin.customize.category.report.ReportBL.java
/** * Zips a directory content into a zip stream * @param dirZip/*from w w w. j av a 2 s . c o m*/ * @param zipOut * @param rootPath */ public static void zipFiles(File dirZip, ZipOutputStream zipOut, String rootPath) { try { // get a listing of the directory content File[] dirList = dirZip.listFiles(); byte[] readBuffer = new byte[2156]; int bytesIn; // loop through dirList, and zip the files for (int i = 0; i < dirList.length; i++) { File f = dirList[i]; if (f.isDirectory()) { // if the File object is a directory, call this // function again to add its content recursively String filePath = f.getAbsolutePath(); zipFiles(new File(filePath), zipOut, rootPath); // loop again continue; } // if we reached here, the File object f was not a directory // create a FileInputStream on top of f FileInputStream fis = new FileInputStream(f); // create a new zip entry ZipEntry anEntry = new ZipEntry( f.getAbsolutePath().substring(rootPath.length() + 1, f.getAbsolutePath().length())); // place the zip entry in the ZipOutputStream object zipOut.putNextEntry(anEntry); // now write the content of the file to the ZipOutputStream while ((bytesIn = fis.read(readBuffer)) != -1) { zipOut.write(readBuffer, 0, bytesIn); } // close the Stream fis.close(); } } catch (Exception e) { LOGGER.error("Zip the " + dirZip.getName() + " failed with " + e.getMessage()); if (LOGGER.isDebugEnabled()) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } }
From source file:org.tonguetied.datatransfer.DataServiceImpl.java
public void createArchive(File directory) throws ExportException, IllegalArgumentException { if (!directory.isDirectory()) throw new IllegalArgumentException("expecting a directory"); ZipOutputStream zos = null; try {/*from ww w .ja va2s .c o m*/ File[] files = directory.listFiles(); if (files.length > 0) { final File archive = new File(directory, directory.getName() + ".zip"); zos = new ZipOutputStream(new FileOutputStream(archive)); for (File file : files) { zos.putNextEntry(new ZipEntry(file.getName())); IOUtils.write(FileUtils.readFileToByteArray(file), zos); zos.closeEntry(); } if (logger.isDebugEnabled()) logger.debug("archived " + files.length + " files to " + archive.getPath()); } } catch (IOException ioe) { throw new ExportException(ioe); } finally { IOUtils.closeQuietly(zos); } }
From source file:com.ibm.liberty.starter.ProjectZipConstructor.java
public void createZipFromMap(ZipOutputStream zos) throws IOException { System.out.println("Entering method ProjectZipConstructor.createZipFromMap()"); Enumeration<String> en = fileMap.keys(); while (en.hasMoreElements()) { String path = en.nextElement(); byte[] byteArray = fileMap.get(path); ZipEntry entry = new ZipEntry(path); entry.setSize(byteArray.length); entry.setCompressedSize(-1);/* www . j a v a 2 s . co m*/ try { zos.putNextEntry(entry); zos.write(byteArray); } catch (IOException e) { throw new IOException(e); } } }
From source file:cdr.forms.SwordDepositHandler.java
private File makeZipFile(gov.loc.mets.DocumentRoot metsDocumentRoot, IdentityHashMap<DepositFile, String> filenames) { // Get the METS XML String metsXml = serializeMets(metsDocumentRoot); // Create the zip file File zipFile;/*from ww w .j av a 2 s.com*/ try { zipFile = File.createTempFile("tmp", ".zip"); } catch (IOException e) { throw new Error(e); } FileOutputStream fileOutput; try { fileOutput = new FileOutputStream(zipFile); } catch (FileNotFoundException e) { throw new Error(e); } ZipOutputStream zipOutput = new ZipOutputStream(fileOutput); try { ZipEntry entry; // Write the METS entry = new ZipEntry("mets.xml"); zipOutput.putNextEntry(entry); PrintStream xmlPrintStream = new PrintStream(zipOutput); xmlPrintStream.print(metsXml); // Write files for (DepositFile file : filenames.keySet()) { if (!file.isExternal()) { entry = new ZipEntry(filenames.get(file)); zipOutput.putNextEntry(entry); FileInputStream fileInput = new FileInputStream(file.getFile()); byte[] buffer = new byte[1024]; int length; while ((length = fileInput.read(buffer)) != -1) zipOutput.write(buffer, 0, length); fileInput.close(); } } zipOutput.finish(); zipOutput.close(); fileOutput.close(); } catch (IOException e) { throw new Error(e); } return zipFile; }
From source file:com.pr7.logging.CustomDailyRollingFileAppender.java
/** * Compresses the passed file to a .zip file, stores the .zip in the same * directory as the passed file, and then deletes the original, leaving only * the .zipped archive.//from w w w .j a v a2 s .co m * * @param file */ private void zipAndDelete(File file) throws IOException { if (!file.getName().endsWith(".zip")) { System.out.println("zipAndDelete file = " + file.getName()); String folderName = "archive"; File folder = new File(file.getParent(), folderName); if (!folder.exists()) { if (folder.mkdir()) { System.out.println("Create " + folderName + " success."); } else { System.out.println("Create " + folderName + " failed."); } } File zipFile = new File(folder, file.getName() + ".zip"); FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); ZipEntry zipEntry = new ZipEntry(file.getName()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[4096]; while (true) { int bytesRead = fis.read(buffer); if (bytesRead == -1) break; else { zos.write(buffer, 0, bytesRead); } } zos.closeEntry(); fis.close(); zos.close(); file.delete(); } }
From source file:es.gob.afirma.signers.ooxml.be.fedict.eid.applet.service.signer.ooxml.AbstractOOXMLSignatureService.java
private ZipOutputStream copyOOXMLContent(final String signatureZipEntryName, final OutputStream signedOOXMLOutputStream) throws IOException, ParserConfigurationException, SAXException, TransformerException { final ZipOutputStream zipOutputStream = new ZipOutputStream(signedOOXMLOutputStream); final ZipInputStream zipInputStream = new ZipInputStream( new ByteArrayInputStream(this.getOfficeOpenXMLDocument())); ZipEntry zipEntry;// ww w.j a v a 2 s . c om boolean hasOriginSigsRels = false; while (null != (zipEntry = zipInputStream.getNextEntry())) { zipOutputStream.putNextEntry(new ZipEntry(zipEntry.getName())); if ("[Content_Types].xml".equals(zipEntry.getName())) { //$NON-NLS-1$ final Document contentTypesDocument = loadDocumentNoClose(zipInputStream); final Element typesElement = contentTypesDocument.getDocumentElement(); // We need to add an Override element. final Element overrideElement = contentTypesDocument.createElementNS( "http://schemas.openxmlformats.org/package/2006/content-types", "Override"); //$NON-NLS-1$ //$NON-NLS-2$ overrideElement.setAttribute("PartName", "/" + signatureZipEntryName); //$NON-NLS-1$ //$NON-NLS-2$ overrideElement.setAttribute("ContentType", //$NON-NLS-1$ "application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml"); //$NON-NLS-1$ typesElement.appendChild(overrideElement); final Element nsElement = contentTypesDocument.createElement("ns"); //$NON-NLS-1$ nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:tns", //$NON-NLS-1$ "http://schemas.openxmlformats.org/package/2006/content-types"); //$NON-NLS-1$ final NodeList nodeList = XPathAPI.selectNodeList(contentTypesDocument, "/tns:Types/tns:Default[@Extension='sigs']", nsElement); //$NON-NLS-1$ if (0 == nodeList.getLength()) { // Add Default element for 'sigs' extension. final Element defaultElement = contentTypesDocument.createElementNS( "http://schemas.openxmlformats.org/package/2006/content-types", "Default"); //$NON-NLS-1$ //$NON-NLS-2$ defaultElement.setAttribute("Extension", "sigs"); //$NON-NLS-1$ //$NON-NLS-2$ defaultElement.setAttribute("ContentType", //$NON-NLS-1$ "application/vnd.openxmlformats-package.digital-signature-origin"); //$NON-NLS-1$ typesElement.appendChild(defaultElement); } writeDocumentNoClosing(contentTypesDocument, zipOutputStream, false); } else if ("_rels/.rels".equals(zipEntry.getName())) { //$NON-NLS-1$ final Document relsDocument = loadDocumentNoClose(zipInputStream); final Element nsElement = relsDocument.createElement("ns"); //$NON-NLS-1$ nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:tns", RELATIONSHIPS_SCHEMA); //$NON-NLS-1$ final NodeList nodeList = XPathAPI.selectNodeList(relsDocument, "/tns:Relationships/tns:Relationship[@Type='http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/origin']", //$NON-NLS-1$ nsElement); if (0 == nodeList.getLength()) { final Element relationshipElement = relsDocument.createElementNS(RELATIONSHIPS_SCHEMA, "Relationship"); //$NON-NLS-1$ relationshipElement.setAttribute("Id", "rel-id-" + UUID.randomUUID().toString()); //$NON-NLS-1$ //$NON-NLS-2$ relationshipElement.setAttribute("Type", //$NON-NLS-1$ "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/origin"); //$NON-NLS-1$ relationshipElement.setAttribute("Target", "_xmlsignatures/origin.sigs"); //$NON-NLS-1$ //$NON-NLS-2$ relsDocument.getDocumentElement().appendChild(relationshipElement); } writeDocumentNoClosing(relsDocument, zipOutputStream, false); } else if (zipEntry.getName().startsWith("_xmlsignatures/_rels/") //$NON-NLS-1$ && zipEntry.getName().endsWith(".rels")) { //$NON-NLS-1$ hasOriginSigsRels = true; final Document originSignRelsDocument = loadDocumentNoClose(zipInputStream); final Element relationshipElement = originSignRelsDocument.createElementNS(RELATIONSHIPS_SCHEMA, "Relationship"); //$NON-NLS-1$ relationshipElement.setAttribute("Id", "rel-" + UUID.randomUUID().toString()); //$NON-NLS-1$ //$NON-NLS-2$ relationshipElement.setAttribute("Type", //$NON-NLS-1$ "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/signature"); //$NON-NLS-1$ relationshipElement.setAttribute("Target", FilenameUtils.getName(signatureZipEntryName)); //$NON-NLS-1$ originSignRelsDocument.getDocumentElement().appendChild(relationshipElement); writeDocumentNoClosing(originSignRelsDocument, zipOutputStream, false); } else { IOUtils.copy(zipInputStream, zipOutputStream); } } if (!hasOriginSigsRels) { // Add signature relationships document. addOriginSigsRels(signatureZipEntryName, zipOutputStream); addOriginSigs(zipOutputStream); } // Return. zipInputStream.close(); return zipOutputStream; }
From source file:com.simiacryptus.mindseye.lang.Layer.java
/** * Write zip./*from www . jav a2 s. c o m*/ * * @param out the out * @param precision the precision */ default void writeZip(@Nonnull ZipOutputStream out, SerialPrecision precision) { try { @Nonnull HashMap<CharSequence, byte[]> resources = new HashMap<>(); JsonObject json = getJson(resources, precision); out.putNextEntry(new ZipEntry("model.json")); @Nonnull JsonWriter writer = new JsonWriter(new OutputStreamWriter(out)); writer.setIndent(" "); writer.setHtmlSafe(true); writer.setSerializeNulls(false); new GsonBuilder().setPrettyPrinting().create().toJson(json, writer); writer.flush(); out.closeEntry(); resources.forEach((name, data) -> { try { out.putNextEntry(new ZipEntry(String.valueOf(name))); IOUtils.write(data, out); out.flush(); out.closeEntry(); } catch (IOException e) { throw new RuntimeException(e); } }); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:it.isislab.sof.core.engine.hadoop.sshclient.connection.SofManager.java
private static void addDir(File dirObj, ZipOutputStream out, String dirToZip) throws IOException { File[] files = dirObj.listFiles(); byte[] tmpBuf = new byte[1024]; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { addDir(files[i], out, dirToZip); continue; }//from ww w.ja va 2 s .c o m FileInputStream in = new FileInputStream(files[i].getAbsolutePath()); System.out.println("Zipping: " + files[i].getName()); String filename = files[i].getAbsolutePath().substring(dirToZip.length()); out.putNextEntry(new ZipEntry(filename)); int len; while ((len = in.read(tmpBuf)) > 0) { out.write(tmpBuf, 0, len); } out.closeEntry(); in.close(); } }
From source file:com.googlecode.clearnlp.component.AbstractStatisticalComponent.java
/** Called by {@link AbstractStatisticalComponent#saveModels(ZipOutputStream)}}. */ protected void saveFeatureTemplates(ZipOutputStream zout, String entryName) throws Exception { int i, size = f_xmls.length; PrintStream fout;//from w ww .j ava2 s. c om LOG.info("Saving feature templates.\n"); for (i = 0; i < size; i++) { zout.putNextEntry(new ZipEntry(entryName + i)); fout = UTOutput.createPrintBufferedStream(zout); IOUtils.copy(UTInput.toInputStream(f_xmls[i].toString()), fout); fout.flush(); zout.closeEntry(); } }
From source file:edu.harvard.iq.dvn.core.analysis.NetworkDataServiceBean.java
private void addZipEntry(ZipOutputStream zout, String inputFileName, String outputFileName) throws IOException { FileInputStream tmpin = new FileInputStream(inputFileName); byte[] dataBuffer = new byte[8192]; int i = 0;// w ww .ja va2 s. co m ZipEntry e = new ZipEntry(outputFileName); zout.putNextEntry(e); while ((i = tmpin.read(dataBuffer)) > 0) { zout.write(dataBuffer, 0, i); zout.flush(); } tmpin.close(); zout.closeEntry(); }