Example usage for java.util.zip ZipEntry setExtra

List of usage examples for java.util.zip ZipEntry setExtra

Introduction

In this page you can find the example usage for java.util.zip ZipEntry setExtra.

Prototype

public void setExtra(byte[] extra) 

Source Link

Document

Sets the optional extra field data for the entry.

Usage

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/* ww w.j  a  v  a  2s .  com*/
 */
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:org.dbgl.util.FileUtils.java

public static void zipEntry(final File orgFile, final File fileEntry, final ZipOutputStream zos)
        throws IOException {
    ZipEntry anEntry = new ZipEntry(PlatformUtils.toArchivePath(fileEntry, orgFile.isDirectory()));
    anEntry.setTime(orgFile.lastModified());
    if (orgFile.isFile() && !orgFile.canWrite())
        anEntry.setExtra(new byte[] { 1 });
    zos.putNextEntry(anEntry);//from  w  w w .j a va2 s .  co  m

    if (orgFile.isFile()) {
        byte[] readBuffer = new byte[ZIP_BUFFER];
        int bytes = 0;
        FileInputStream is = new FileInputStream(orgFile);
        while ((bytes = is.read(readBuffer)) != -1)
            zos.write(readBuffer, 0, bytes);
        is.close();
    }
    zos.closeEntry();
}

From source file:org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveWriter.java

protected void writeDocument(String path, ExportedDocument doc) throws IOException {

    if (path.equals("/") || path.length() == 0) {
        path = "";
    } else { // avoid adding a root entry
        path += '/';
        ZipEntry entry = new ZipEntry(path);
        // store the number of child as an extra info on the entry
        entry.setExtra(new DWord(doc.getFilesCount()).getBytes());
        out.putNextEntry(entry);/*from   w ww  . j a  v a2  s  .  c  o  m*/
        out.closeEntry();
        // System.out.println(">> add entry: "+entry.getName());
    }

    // write metadata
    ZipEntry entry = new ZipEntry(path + ExportConstants.DOCUMENT_FILE);
    out.putNextEntry(entry);
    try {
        writeXML(doc.getDocument(), out);
    } finally {
        out.closeEntry();
        // System.out.println(">> add entry: "+entry.getName());
    }

    // write external documents
    for (Map.Entry<String, Document> ext : doc.getDocuments().entrySet()) {
        String fileName = ext.getKey() + ".xml";
        entry = new ZipEntry(path + fileName);
        out.putNextEntry(entry);
        try {
            writeXML(ext.getValue(), out);
        } finally {
            out.closeEntry();
        }
    }

    // write blobs
    Map<String, Blob> blobs = doc.getBlobs();
    for (Map.Entry<String, Blob> blobEntry : blobs.entrySet()) {
        String fileName = blobEntry.getKey();
        entry = new ZipEntry(path + fileName);
        out.putNextEntry(entry);
        try (InputStream in = blobEntry.getValue().getStream()) {
            IOUtils.copy(in, out);
        }
        // DO NOT CALL out.close(), we want to keep writing to it
        out.closeEntry();
    }
}

From source file:org.signserver.anttasks.PostProcessModulesTask.java

/**
 * Replacer for the postprocess-jar Ant macro.
 * /*from  w  ww.java2  s. c om*/
 * @param replaceincludes Ant list of all files in the jar to replace in
 * @param src Source jar file
 * @param destfile Destination jar file
 * @param properties Properties to replace from
 * @param self The Task (used for logging)
 * @throws IOException in case of error
 */
protected void replaceInJar(String replaceincludes, String src, String destfile, Map properties, Task self)
        throws IOException {
    try {
        self.log("Replace " + replaceincludes + " in " + src + " to " + destfile, Project.MSG_VERBOSE);

        File srcFile = new File(src);
        if (!srcFile.exists()) {
            throw new FileNotFoundException(srcFile.getAbsolutePath());
        }

        // Expand properties of all files in replaceIncludes
        HashSet<String> replaceFiles = new HashSet<String>();
        String[] rfiles = replaceincludes.split(",");
        for (int i = 0; i < rfiles.length; i++) {
            rfiles[i] = rfiles[i].trim();
        }
        replaceFiles.addAll(Arrays.asList(rfiles));
        self.log("Files to replace: " + replaceFiles, Project.MSG_INFO);

        // Open source zip file
        ZipFile zipSrc = new ZipFile(srcFile);
        ZipOutputStream zipDest = new ZipOutputStream(new FileOutputStream(destfile));

        // For each entry in the source file copy them to dest file and postprocess if necessary
        Enumeration<? extends ZipEntry> entries = zipSrc.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();

            if (entry.isDirectory()) {
                // Just put the directory
                zipDest.putNextEntry(entry);
            } else {
                // If we should postprocess the entry
                if (replaceFiles.contains(name)) {
                    name += (" [REPLACE]");
                    self.log(name, Project.MSG_VERBOSE);

                    // Create a new zip entry for the file
                    ZipEntry newEntry = new ZipEntry(entry.getName());
                    newEntry.setComment(entry.getComment());
                    newEntry.setExtra(entry.getExtra());
                    zipDest.putNextEntry(newEntry);

                    // Read the old document
                    StringBuffer oldDocument = stringBufferFromFile(zipSrc.getInputStream(entry));
                    self.log("Before replace ********\n" + oldDocument.toString() + "\n", Project.MSG_DEBUG);

                    // Do properties substitution
                    StrSubstitutor sub = new StrSubstitutor(properties);
                    StringBuffer newerDocument = commentReplacement(oldDocument, properties);
                    String newDocument = sub.replace(newerDocument);
                    self.log("After replace ********\n" + newDocument.toString() + "\n", Project.MSG_DEBUG);

                    // Write the new document
                    byte[] newBytes = newDocument.getBytes("UTF-8");
                    entry.setSize(newBytes.length);
                    copy(new ByteArrayInputStream(newBytes), zipDest);
                } else {
                    // Just copy the entry to dest zip file
                    name += (" []");
                    self.log(name, Project.MSG_VERBOSE);
                    zipDest.putNextEntry(entry);
                    copy(zipSrc.getInputStream(entry), zipDest);
                }
                zipDest.closeEntry();
            }
        }
        zipSrc.close();
        zipDest.close();
    } catch (IOException ex) {
        throw new BuildException(ex);
    }
}