Example usage for java.util.zip ZipEntry getExtra

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

Introduction

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

Prototype

public byte[] getExtra() 

Source Link

Document

Returns the extra field data for the entry.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String zipname = "data.zip";
    ZipFile zipFile = new ZipFile(zipname);
    Enumeration enumeration = zipFile.entries();
    while (enumeration.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
        System.out.println(zipEntry.getExtra());
    }//from  ww w  .j  a  va  2  s  .  c  o  m
}

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// w  w w .  java  2  s.  co  m
 */
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.ambraproject.article.service.XslIngestArchiveProcessor.java

/**
 * Generate a description for a single zip-entry.
 *
 * @param ze  the zip entry to describe.
 * @param buf the buffer to place the description into
 *//*from   w w w  . jav a  2  s.  co m*/
private static void entry2xml(ZipEntry ze, StringBuilder buf) {
    buf.append("<ZipEntry name=\"").append(attrEscape(ze.getName())).append("\"");

    if (ze.isDirectory())
        buf.append(" isDirectory=\"true\"");
    if (ze.getCrc() >= 0)
        buf.append(" crc=\"").append(ze.getCrc()).append("\"");
    if (ze.getSize() >= 0)
        buf.append(" size=\"").append(ze.getSize()).append("\"");
    if (ze.getCompressedSize() >= 0)
        buf.append(" compressedSize=\"").append(ze.getCompressedSize()).append("\"");
    if (ze.getTime() >= 0)
        buf.append(" time=\"").append(ze.getTime()).append("\"");

    if (ze.getComment() != null || ze.getExtra() != null) {
        buf.append(">\n");

        if (ze.getComment() != null)
            buf.append("<Comment>").append(xmlEscape(ze.getComment())).append("</Comment>\n");
        if (ze.getExtra() != null)
            buf.append("<Extra>").append(base64Encode(ze.getExtra())).append("</Extra>\n");

        buf.append("</ZipEntry>\n");
    } else {
        buf.append("/>\n");
    }
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

public void testLookAtExtraZipFields_from_macos() throws IOException {
    InputStream fis = Streams.fileInputStream(new File("src/test/resources/zip-timestamp/macOsZipFile.zip"));
    ZipInputStream zis = new ZipInputStream(fis);
    final java.util.zip.ZipEntry evenEntry = zis.getNextEntry();
    final ZipExtraField[] parse = ExtraFieldUtils.parse(evenEntry.getExtra());
    System.out.println(Arrays.asList(parse));
    final java.util.zip.ZipEntry oddEntry = zis.getNextEntry();

    System.out.println(Arrays.asList(ExtraFieldUtils.parse(oddEntry.getExtra())));

    System.out.println("oddEntry.getTime() = " + new Date(oddEntry.getTime()).toString());
    System.out.println("oddEntry.getName() = " + oddEntry.getName());
    System.out.println("new String(oddEntry.getExtra()) = " + new String(oddEntry.getExtra()));
    System.out.println("evenEntry.getName() = " + evenEntry.getName());
    System.out.println("evenEntry.getTime() = " + new Date(evenEntry.getTime()).toString());
    System.out.println("new String(evenEntry.getExtra()) = " + new String(evenEntry.getExtra()));

}

From source file:org.dbgl.util.FileUtils.java

public static void extractEntry(final ZipFile zf, final ZipEntry srcEntry, final File dstFile,
        final ProgressNotifyable prog) throws IOException {
    File foundDstFile = null, temporarilyRenamedFile = null;
    if (PlatformUtils.IS_WINDOWS && dstFile.getName().contains("~")) {
        foundDstFile = dstFile.getCanonicalFile();
        if (!foundDstFile.getName().equals(dstFile.getName()) && foundDstFile.exists()) {
            temporarilyRenamedFile = getUniqueFileName(foundDstFile);
            foundDstFile.renameTo(temporarilyRenamedFile);
        }/*from  w  w  w.jav  a 2 s .  c  om*/
    }

    if (dstFile.exists())
        throw new IOException(
                Settings.getInstance().msg("general.error.filetobeextractedexists", new Object[] { dstFile }));
    if (srcEntry.isDirectory()) {
        if (!dstFile.exists())
            createDir(dstFile);
    } else {
        if (dstFile.getParentFile() != null)
            createDir(dstFile.getParentFile());
        FileOutputStream fos = new FileOutputStream(dstFile);
        InputStream is = zf.getInputStream(srcEntry);
        byte[] readBuffer = new byte[ZIP_BUFFER];
        int bytesIn;
        while ((bytesIn = is.read(readBuffer)) != -1)
            fos.write(readBuffer, 0, bytesIn);
        fos.flush();
        fos.close();
        is.close();
        byte[] extra = srcEntry.getExtra();
        if ((extra != null) && (extra.length == 1) && (extra[0] == 1))
            fileSetReadOnly(dstFile);
    }
    fileSetLastModified(dstFile, srcEntry.getTime());

    if (foundDstFile != null && temporarilyRenamedFile != null)
        temporarilyRenamedFile.renameTo(foundDstFile);

    prog.incrProgress((int) (srcEntry.getSize() / 1024));
}

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

private static int getFilesCount(ZipEntry entry) throws IOException {
    byte[] bytes = entry.getExtra();
    if (bytes == null) {
        return 0;
    } else if (bytes.length != 4) {
        throw new IOException("Invalid Nuxeo Archive");
    } else {/*from  ww w .j a  va 2  s.  c  o m*/
        return new DWord(bytes).getInt();
    }
}

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

/**
 * Replacer for the postprocess-jar Ant macro.
 * //from w  w w.ja v a2  s .  c  o m
 * @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);
    }
}