List of usage examples for java.util.zip ZipEntry setComment
public void setComment(String comment)
From source file:com.diffplug.gradle.ZipMisc.java
/** * Modifies only the specified entries in a zip file. * * @param input a source from a zip file * @param output an output to a zip file * @param toModify a map from path to an input stream for the entries you'd like to change * @param toOmit a set of entries you'd like to leave out of the zip * @throws IOException//from w w w . j av a 2 s . c om */ public static void modify(ByteSource input, ByteSink output, Map<String, Function<byte[], byte[]>> toModify, Predicate<String> toOmit) throws IOException { try (ZipInputStream zipInput = new ZipInputStream(input.openBufferedStream()); ZipOutputStream zipOutput = new ZipOutputStream(output.openBufferedStream())) { while (true) { // read the next entry ZipEntry entry = zipInput.getNextEntry(); if (entry == null) { break; } Function<byte[], byte[]> replacement = toModify.get(entry.getName()); if (replacement != null) { byte[] clean = ByteStreams.toByteArray(zipInput); byte[] modified = replacement.apply(clean); // if it's the entry being modified, enter the modified stuff try (InputStream replacementStream = new ByteArrayInputStream(modified)) { ZipEntry newEntry = new ZipEntry(entry.getName()); newEntry.setComment(entry.getComment()); newEntry.setExtra(entry.getExtra()); newEntry.setMethod(entry.getMethod()); newEntry.setTime(entry.getTime()); zipOutput.putNextEntry(newEntry); copy(replacementStream, zipOutput); } } else if (!toOmit.test(entry.getName())) { // if it isn't being modified, just copy the file stream straight-up ZipEntry newEntry = new ZipEntry(entry); newEntry.setCompressedSize(-1); zipOutput.putNextEntry(newEntry); copy(zipInput, zipOutput); } // close the entries zipInput.closeEntry(); zipOutput.closeEntry(); } } }
From source file:com.stehno.oxy.ZipBuilder.java
/** * Used to add an entry with the given parameters. * * @param name the entry name/* w ww . j av a2 s. c o m*/ * @param comment the entry comment * @param bytes the entry data * @return a reference to the builder * @throws IOException if there is a problem writing the entry data */ public ZipBuilder addEntry(final String name, final String comment, final byte[] bytes) throws IOException { final ZipEntry entry = new ZipEntry(name); if (isNotBlank(comment)) { entry.setComment(comment); } return (addEntry(entry, bytes)); }
From source file:com.stehno.oxy.ZipBuilder.java
/** * Used to add an entry with the given parameters. The input stream will be read and closed in this * method.//w w w .j ava 2 s . c om * * @param name the entry name * @param comment the entry comment * @param in the stream containing the entry data * @return a reference to the builder * @throws IOException if there is a problem writing the entry data */ public ZipBuilder addEntry(final String name, final String comment, final InputStream in) throws IOException { final ZipEntry entry = new ZipEntry(name); if (isNotBlank(comment)) { entry.setComment(comment); } return (addEntry(entry, in)); }
From source file:com.panet.imeta.trans.steps.xmloutput.XMLOutput.java
public boolean openNewFile() { boolean retval = false; data.writer = null;/* w w w . j ava2 s. com*/ try { FileObject file = KettleVFS.getFileObject(buildFilename(true)); if (meta.isAddToResultFiles()) { // Add this to the result file names... ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, file, getTransMeta().getName(), getStepname()); resultFile.setComment("This file was created with a xml output step"); addResultFile(resultFile); } OutputStream outputStream; if (meta.isZipped()) { OutputStream fos = KettleVFS.getOutputStream(file, false); data.zip = new ZipOutputStream(fos); File entry = new File(buildFilename(false)); ZipEntry zipentry = new ZipEntry(entry.getName()); zipentry.setComment("Compressed by Kettle"); data.zip.putNextEntry(zipentry); outputStream = data.zip; } else { OutputStream fos = KettleVFS.getOutputStream(file, false); outputStream = fos; } if (meta.getEncoding() != null && meta.getEncoding().length() > 0) { log.logBasic(toString(), "Opening output stream in encoding: " + meta.getEncoding()); data.writer = new OutputStreamWriter(outputStream, meta.getEncoding()); data.writer.write(XMLHandler.getXMLHeader(meta.getEncoding()).toCharArray()); } else { log.logBasic(toString(), "Opening output stream in default encoding : " + Const.XML_ENCODING); data.writer = new OutputStreamWriter(outputStream); data.writer.write(XMLHandler.getXMLHeader(Const.XML_ENCODING).toCharArray()); } // Add the name space if defined StringBuffer nameSpace = new StringBuffer(); if ((meta.getNameSpace() != null) && (!"".equals(meta.getNameSpace()))) { nameSpace.append(" xmlns=\""); nameSpace.append(meta.getNameSpace()); nameSpace.append("\""); } // OK, write the header & the parent element: data.writer.write(("<" + meta.getMainElement() + nameSpace.toString() + ">" + Const.CR).toCharArray()); retval = true; } catch (Exception e) { logError("Error opening new file : " + e.toString()); } // System.out.println("end of newFile(), splitnr="+splitnr); data.splitnr++; return retval; }
From source file:org.broad.igv.feature.genome.GenomeManager.java
/** * Rewrite the {@link Globals#GENOME_ARCHIVE_SEQUENCE_FILE_LOCATION_KEY} property to equal * the specified {@code newSequencePath}. Works by creating a temp file and renaming * * @param targetFile A .genome file, in zip format * @param newSequencePath/*from w w w . j av a2 s .com*/ * @return boolean indicating success or failure. * @throws IOException */ static boolean rewriteSequenceLocation(File targetFile, String newSequencePath) throws IOException { ZipFile targetZipFile = new ZipFile(targetFile); boolean success = false; File tmpZipFile = File.createTempFile("tmpGenome", ".zip"); ZipEntry propEntry = targetZipFile.getEntry(Globals.GENOME_ARCHIVE_PROPERTY_FILE_NAME); InputStream propertyInputStream = null; ZipOutputStream zipOutputStream = null; Properties inputProperties = new Properties(); try { propertyInputStream = targetZipFile.getInputStream(propEntry); BufferedReader reader = new BufferedReader(new InputStreamReader(propertyInputStream)); //Copy over property.txt, only replacing a few properties inputProperties.load(reader); inputProperties.put(Globals.GENOME_ARCHIVE_SEQUENCE_FILE_LOCATION_KEY, newSequencePath); inputProperties.put(Globals.GENOME_ARCHIVE_CUSTOM_SEQUENCE_LOCATION_KEY, Boolean.TRUE.toString()); ByteArrayOutputStream propertyBytes = new ByteArrayOutputStream(); PrintWriter propertyFileWriter = new PrintWriter(new OutputStreamWriter(propertyBytes)); inputProperties.store(propertyFileWriter, null); propertyFileWriter.flush(); byte[] newPropertyBytes = propertyBytes.toByteArray(); Enumeration<? extends ZipEntry> entries = targetZipFile.entries(); zipOutputStream = new ZipOutputStream(new FileOutputStream(tmpZipFile)); while (entries.hasMoreElements()) { ZipEntry curEntry = entries.nextElement(); ZipEntry writeEntry = null; if (curEntry.getName().equals(Globals.GENOME_ARCHIVE_PROPERTY_FILE_NAME)) { writeEntry = new ZipEntry(Globals.GENOME_ARCHIVE_PROPERTY_FILE_NAME); writeEntry.setSize(newPropertyBytes.length); zipOutputStream.putNextEntry(writeEntry); zipOutputStream.write(newPropertyBytes); continue; } else { //Because the compressed size can vary, //we generate a new ZipEntry and copy some attributes writeEntry = new ZipEntry(curEntry.getName()); writeEntry.setSize(curEntry.getSize()); writeEntry.setComment(curEntry.getComment()); writeEntry.setTime(curEntry.getTime()); } zipOutputStream.putNextEntry(writeEntry); InputStream tmpIS = null; try { tmpIS = targetZipFile.getInputStream(writeEntry); int bytes = IOUtils.copy(tmpIS, zipOutputStream); log.debug(bytes + " bytes written to " + targetFile); } finally { if (tmpIS != null) tmpIS.close(); } } } catch (Exception e) { tmpZipFile.delete(); throw new RuntimeException(e.getMessage(), e); } finally { if (propertyInputStream != null) propertyInputStream.close(); if (zipOutputStream != null) { zipOutputStream.flush(); zipOutputStream.finish(); zipOutputStream.close(); } zipOutputStream = null; System.gc(); success = true; } //This is a hack. I don't know why it's necessary, //but for some reason the output zip file seems to be corrupt //at least when called from GenomeManager.refreshArchive try { Thread.sleep(1500); } catch (InterruptedException e) { // } //Rename tmp file if (success) { targetFile.delete(); FileUtils.copyFile(tmpZipFile, targetFile); success = targetFile.exists(); tmpZipFile.delete(); } return success; }
From source file:org.digidoc4j.impl.bdoc.asic.AsicContainerCreator.java
private void writeZipEntry(ZipEntry zipEntry, byte[] entryBytes) { zipEntry.setComment(zipComment); writeZipEntryWithoutComment(zipEntry, entryBytes); }
From source file:org.lockss.exporter.ZipExporter.java
protected void writeCu(CachedUrl cu) throws IOException { ensureOpenZip();/*from w ww . j a v a 2 s . c o m*/ CIProperties props = cu.getProperties(); ZipEntry ent = new ZipEntry(xlateFilename(cu.getUrl())); // Store HTTP response headers into entry comment ent.setComment(getHttpResponseString(cu)); String lastMod = props.getProperty(CachedUrl.PROPERTY_LAST_MODIFIED); if (!StringUtil.isNullString(lastMod)) { try { long cuLastModified = dateAsLong(lastMod); ent.setTime(cuLastModified); } catch (RuntimeException e) { } } InputStream ins = cu.getUnfilteredInputStream(); try { zip.putNextEntry(ent); StreamUtil.copy(ins, zip); zip.closeEntry(); // zip.flush(); } finally { IOUtil.safeClose(ins); } }
From source file:org.paxle.core.doc.impl.BasicDocumentFactoryTest.java
public void testStoreMarshalledCommand() throws IOException, ParseException { // Create the ZIP file final File outFile = File.createTempFile("command", ".zip"); outFile.deleteOnExit();//from w w w .j a va 2 s.c o m ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(outFile)); // creating a test command final ICommand cmd = this.createTestCommand(); // marshal command final ZipEntry commandEntry = new ZipEntry("command.xml"); commandEntry.setComment("command.xml"); zipOut.putNextEntry(commandEntry); final TeeOutputStream out = new TeeOutputStream(System.out, zipOut); final Map<String, DataHandler> attachments = this.docFactory.marshal(cmd, out); zipOut.closeEntry(); // write attachments if (attachments != null) { for (Entry<String, DataHandler> attachment : attachments.entrySet()) { final String cid = attachment.getKey(); final DataHandler data = attachment.getValue(); final ZipEntry zipEntry = new ZipEntry(cid); zipEntry.setComment(data.getName()); zipOut.putNextEntry(zipEntry); IOUtils.copy(data.getInputStream(), zipOut); zipOut.closeEntry(); } } zipOut.close(); System.out.println("Command written into file: " + outFile.toString()); // print content final ZipFile zf = new ZipFile(outFile); for (Enumeration<? extends ZipEntry> entries = zf.entries(); entries.hasMoreElements();) { ZipEntry entry = entries.nextElement(); System.out.println(entry.getName() + ": " + entry.getComment()); } zf.close(); }
From source file:org.pentaho.di.resource.ResourceUtil.java
/** * Serializes the referenced resource export interface (Job, Transformation, Mapping, Step, Job Entry, etc) to a ZIP * file.//w w w .j a v a 2 s. c o m * * @param zipFilename * The ZIP file to put the content in * @param resourceExportInterface * the interface to serialize * @param space * the space to use for variable replacement * @param repository * the repository to load objects from (or null if not used) * @param injectXML * The XML to inject into the resulting ZIP archive (optional, can be null) * @param injectFilename * The name of the file for the XML to inject in the ZIP archive (optional, can be null) * @return The full VFS filename reference to the serialized export interface XML file in the ZIP archive. * @throws KettleException * in case anything goes wrong during serialization */ public static final TopLevelResource serializeResourceExportInterface(String zipFilename, ResourceExportInterface resourceExportInterface, VariableSpace space, Repository repository, IMetaStore metaStore, String injectXML, String injectFilename) throws KettleException { ZipOutputStream out = null; try { Map<String, ResourceDefinition> definitions = new HashMap<String, ResourceDefinition>(); // In case we want to add an extra pay-load to the exported ZIP file... // if (injectXML != null) { ResourceDefinition resourceDefinition = new ResourceDefinition(injectFilename, injectXML); definitions.put(injectFilename, resourceDefinition); } ResourceNamingInterface namingInterface = new SequenceResourceNaming(); String topLevelResource = resourceExportInterface.exportResources(space, definitions, namingInterface, repository, metaStore); if (topLevelResource != null && !definitions.isEmpty()) { // Create the ZIP file... // FileObject fileObject = KettleVFS.getFileObject(zipFilename, space); // Store the XML in the definitions in a ZIP file... // out = new ZipOutputStream(KettleVFS.getOutputStream(fileObject, false)); for (String filename : definitions.keySet()) { ResourceDefinition resourceDefinition = definitions.get(filename); ZipEntry zipEntry = new ZipEntry(resourceDefinition.getFilename()); String comment = BaseMessages.getString(PKG, "ResourceUtil.SerializeResourceExportInterface.ZipEntryComment.OriginatingFile", filename, Const.NVL(resourceDefinition.getOrigin(), "-")); zipEntry.setComment(comment); out.putNextEntry(zipEntry); out.write(resourceDefinition.getContent().getBytes()); out.closeEntry(); } String zipURL = fileObject.getName().toString(); return new TopLevelResource(topLevelResource, zipURL, "zip:" + zipURL + "!" + topLevelResource); } else { throw new KettleException( BaseMessages.getString(PKG, "ResourceUtil.Exception.NoResourcesFoundToExport")); } } catch (Exception e) { throw new KettleException(BaseMessages.getString(PKG, "ResourceUtil.Exception.ErrorSerializingExportInterface", resourceExportInterface.toString()), e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { throw new KettleException(BaseMessages.getString(PKG, "ResourceUtil.Exception.ErrorClosingZipStream", zipFilename)); } } } }
From source file:org.pentaho.di.trans.steps.xmloutput.XMLOutput.java
public boolean openNewFile() { boolean retval = false; data.writer = null;/*from w w w . j ava 2 s . com*/ try { if (meta.isServletOutput()) { data.writer = getTrans().getServletPrintWriter(); if (meta.getEncoding() != null && meta.getEncoding().length() > 0) { data.writer.write(XMLHandler.getXMLHeader(meta.getEncoding()).toCharArray()); } else { data.writer.write(XMLHandler.getXMLHeader(Const.XML_ENCODING).toCharArray()); } } else { FileObject file = KettleVFS.getFileObject(buildFilename(true), getTransMeta()); if (meta.isAddToResultFiles()) { // Add this to the result file names... ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, file, getTransMeta().getName(), getStepname()); resultFile.setComment("This file was created with a xml output step"); addResultFile(resultFile); } OutputStream outputStream; if (meta.isZipped()) { OutputStream fos = KettleVFS.getOutputStream(file, false); data.zip = new ZipOutputStream(fos); File entry = new File(buildFilename(false)); ZipEntry zipentry = new ZipEntry(entry.getName()); zipentry.setComment("Compressed by Kettle"); data.zip.putNextEntry(zipentry); outputStream = data.zip; } else { OutputStream fos = KettleVFS.getOutputStream(file, false); outputStream = fos; } if (meta.getEncoding() != null && meta.getEncoding().length() > 0) { logBasic("Opening output stream in encoding: " + meta.getEncoding()); data.writer = new OutputStreamWriter(outputStream, meta.getEncoding()); data.writer.write(XMLHandler.getXMLHeader(meta.getEncoding()).toCharArray()); } else { logBasic("Opening output stream in default encoding : " + Const.XML_ENCODING); data.writer = new OutputStreamWriter(outputStream); data.writer.write(XMLHandler.getXMLHeader(Const.XML_ENCODING).toCharArray()); } } // Add the name space if defined StringBuffer nameSpace = new StringBuffer(); if ((meta.getNameSpace() != null) && (!"".equals(meta.getNameSpace()))) { nameSpace.append(" xmlns=\""); nameSpace.append(meta.getNameSpace()); nameSpace.append("\""); } // OK, write the header & the parent element: data.writer.write(("<" + meta.getMainElement() + nameSpace.toString() + ">" + Const.CR).toCharArray()); retval = true; } catch (Exception e) { logError("Error opening new file : " + e.toString()); } // System.out.println("end of newFile(), splitnr="+splitnr); data.splitnr++; return retval; }