Example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry getTime

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry getTime

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry getTime.

Prototype

public long getTime() 

Source Link

Document

Returns the last modification time of the entry.

Usage

From source file:org.apache.ant.compress.taskdefs.Unzip.java

protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
    log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
    ZipFile zf = null;//w w  w  .j av  a  2 s . c  o m
    FileNameMapper mapper = getMapper();
    if (!srcF.exists()) {
        throw new BuildException("Unable to expand " + srcF + " as the file does not exist", getLocation());
    }
    try {
        zf = new ZipFile(srcF, getEncoding(), true);
        boolean empty = true;
        Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            empty = false;
            ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
            if (getSkipUnreadableEntries() && !zf.canReadEntryData(ze)) {
                log(Messages.skippedIsUnreadable(ze));
                continue;
            }
            log("extracting " + ze.getName(), Project.MSG_DEBUG);
            InputStream is = null;
            try {
                extractFile(fileUtils, srcF, dir, is = zf.getInputStream(ze), ze.getName(),
                        new Date(ze.getTime()), ze.isDirectory(), mapper);
            } finally {
                FileUtils.close(is);
            }
        }
        if (empty && getFailOnEmptyArchive()) {
            throw new BuildException("archive '" + srcF + "' is empty");
        }
        log("expand complete", Project.MSG_VERBOSE);
    } catch (IOException ioe) {
        throw new BuildException("Error while expanding " + srcF.getPath() + "\n" + ioe.toString(), ioe);
    } finally {
        ZipFile.closeQuietly(zf);
    }
}

From source file:org.apache.wookie.w3c.util.WidgetPackageUtils.java

/**
 * uses apache commons compress to unpack all the zip entries into a target folder
 * partly adapted from the examples on the apache commons compress site, and an
 * example of generic Zip unpacking. Note this iterates over the ZipArchiveEntry enumeration rather
 * than use the more typical ZipInputStream parsing model, as according to the doco it will
 * more reliably read the entries correctly. More info here: http://commons.apache.org/compress/zip.html
 * @param zipfile the Zip File to unpack
 * @param targetFolder the folder into which to unpack the Zip file
 * @throws IOException//from www.  j  a va  2 s . co m
 */
@SuppressWarnings("unchecked")
public static void unpackZip(ZipFile zipfile, File targetFolder) throws IOException {
    targetFolder.mkdirs();
    BufferedOutputStream out = null;
    InputStream in = null;
    ZipArchiveEntry zipEntry;

    Enumeration entries = zipfile.getEntries();
    try {
        while (entries.hasMoreElements()) {
            zipEntry = (ZipArchiveEntry) entries.nextElement();
            // Don't add directories - use mkdirs instead
            if (!zipEntry.isDirectory()) {
                File outFile = new File(targetFolder, zipEntry.getName());

                // Ensure that the parent Folder exists
                if (!outFile.getParentFile().exists()) {
                    outFile.getParentFile().mkdirs();
                }
                // Read the entry
                in = new BufferedInputStream(zipfile.getInputStream(zipEntry));
                out = new BufferedOutputStream(new FileOutputStream(outFile));
                IOUtils.copy(in, out);
                // Restore time stamp
                outFile.setLastModified(zipEntry.getTime());

                // Close File
                out.close();
                // Close Stream
                in.close();
            }
        }

    }
    // We'll catch this exception to close the file otherwise it remains locked
    catch (IOException ex) {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.flush();
            out.close();
        }
        // And throw it again
        throw ex;
    }
}

From source file:org.codehaus.mojo.unix.maven.zip.ZipPackageTest.java

private void assertDirectory(ZipArchiveEntry entry, String name, LocalDateTime time) throws IOException {
    assertNotNull(name, entry);//from  www  .ja v a2 s. co m
    assertTrue(name + " should be file", entry.isDirectory());
    assertEquals(name + ", name", name, entry.getName());
    assertEquals(name + ", timestamp", time, new LocalDateTime(entry.getTime()));
}

From source file:org.codehaus.mojo.unix.maven.zip.ZipPackageTest.java

private void assertFile(ZipFile file, ZipArchiveEntry entry, String name, int size, LocalDateTime time,
        String content, UnixFileMode mode) throws IOException {
    InputStream in = file.getInputStream(entry);

    assertFalse(name + " should be file", entry.isDirectory());
    assertEquals(name + ", name", name, entry.getName());
    assertEquals(name + ", timestamp", time, new LocalDateTime(entry.getTime()));
    // wtf: http://vimalathithen.blogspot.no/2006/06/using-zipentrygetsize.html
    // assertEquals( name + ", size", size, entry.getSize() );

    byte[] bytes = new byte[1000];
    assertEquals(size, in.read(bytes, 0, bytes.length));
    assertEquals(content, new String(bytes, 0, size, charset));

    assertEquals(ZipArchiveEntry.PLATFORM_UNIX, entry.getPlatform());
    assertEquals(name + ", mode", mode.toString(), UnixFileMode.fromInt(entry.getUnixMode()).toString());
}

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

protected void execute() throws ArchiverException {
    getLogger().debug("Expanding: " + getSourceFile() + " into " + getDestDirectory());
    org.apache.commons.compress.archivers.zip.ZipFile zf = null;
    try {/*  w  ww  .j av a2  s . co  m*/
        zf = new org.apache.commons.compress.archivers.zip.ZipFile(getSourceFile(), encoding);
        final Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
            final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo(zf, ze);
            if (isSelected(ze.getName(), fileInfo)) {
                InputStream in = zf.getInputStream(ze);
                extractFileIfIncluded(getSourceFile(), getDestDirectory(), in, ze.getName(),
                        new Date(ze.getTime()), ze.isDirectory(),
                        ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink(zf, ze));
                IOUtil.close(in);
            }

        }

        getLogger().debug("expand complete");
    } catch (final IOException ioe) {
        throw new ArchiverException("Error while expanding " + getSourceFile().getAbsolutePath(), ioe);
    } finally {
        IOUtils.closeQuietly(zf);
    }
}

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

protected void execute(final String path, final File outputDirectory) throws ArchiverException {
    org.apache.commons.compress.archivers.zip.ZipFile zipFile = null;

    try {// www  .ja  va 2 s .c o  m
        zipFile = new org.apache.commons.compress.archivers.zip.ZipFile(getSourceFile(), encoding);

        final Enumeration e = zipFile.getEntries();

        while (e.hasMoreElements()) {
            final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
            final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo(zipFile, ze);
            if (!isSelected(ze.getName(), fileInfo)) {
                continue;
            }

            if (ze.getName().startsWith(path)) {
                final InputStream inputStream = zipFile.getInputStream(ze);
                extractFileIfIncluded(getSourceFile(), outputDirectory, inputStream, ze.getName(),
                        new Date(ze.getTime()), ze.isDirectory(),
                        ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink(zipFile, ze));
                IOUtil.close(inputStream);
            }
        }
    } catch (final IOException ioe) {
        throw new ArchiverException("Error while expanding " + getSourceFile().getAbsolutePath(), ioe);
    } finally {
        IOUtils.closeQuietly(zipFile);
    }
}

From source file:org.jboss.qa.jenkins.test.executor.utils.unpack.UnZipper.java

@Override
public void unpack(File archive, File destination) throws IOException {
    try (ZipFile zip = new ZipFile(archive)) {
        final Set<ZipArchiveEntry> entries = new HashSet<>(Collections.list(zip.getEntries()));

        if (ignoreRootFolders) {
            pathSegmentsToTrim = countRootFolders(entries);
        }//from   w ww .  java 2 s  . c o  m

        for (ZipArchiveEntry entry : entries) {
            if (entry.isDirectory()) {
                continue;
            }
            final String zipPath = trimPathSegments(entry.getName(), pathSegmentsToTrim);
            final File file = new File(destination, zipPath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs(); // Create parent folders if not exist
            }

            try (InputStream is = zip.getInputStream(entry); OutputStream fos = new FileOutputStream(file)) {
                IOUtils.copy(is, fos);
                // check for user-executable bit on entry and apply to file
                if ((entry.getUnixMode() & 0100) != 0) {
                    file.setExecutable(true);
                }
            }
            file.setLastModified(entry.getTime());
        }
    }
}

From source file:org.opengion.fukurou.util.ZipArchive.java

/**
 * ???ZIP??????/*from   w w  w.  j  a  v a 2s.co  m*/
 * ?(targetPath)???ZIP(zipFile)????
 * ?????????????????
 * <p>
 * ???????????????
 *
 * @param targetPath ??
 * @param zipFile    ??ZIP
 * @param encording  ?(Windows???"Windows-31J" ???)
 * @return ???ZIP?
 * @og.rev 4.1.0.2 (2008/02/01) ?
 * @og.rev 4.3.1.1 (2008/08/23) mkdirs ?
 * @og.rev 4.3.3.3 (2008/10/22) mkdirs????
 * @og.rev 5.1.9.0 (2010/08/01) ?
 * @og.rev 5.7.1.2 (2013/12/20) org.apache.commons.compress ?(??)
 */
public static List<File> unCompress(final File targetPath, final File zipFile, final String encording) {
    List<File> list = new ArrayList<File>();

    // ???'/'??'\'????
    //      String tmpPrefix = targetPath;
    //      if( File.separatorChar != targetPath.charAt( targetPath.length() - 1 ) ) {
    //              tmpPrefix = tmpPrefix + File.separator;
    //      }

    ZipArchiveInputStream zis = null;
    File tmpFile = null;
    //      String fileName = null;

    try {
        zis = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile)), encording);

        ZipArchiveEntry entry = null;
        while ((entry = zis.getNextZipEntry()) != null) {
            //                      fileName = tmpPrefix + entry.getName().replace( '/', File.separatorChar );
            tmpFile = new File(targetPath, entry.getName());
            list.add(tmpFile);

            // ??????
            if (entry.isDirectory()) {
                if (!tmpFile.exists() && !tmpFile.mkdirs()) {
                    String errMsg = "??????[??="
                            + tmpFile + "]";
                    System.err.println(errMsg);
                    continue;
                }
            }
            // ????????
            else {
                // 4.3.3.3 (2008/10/22) ?????
                if (!tmpFile.getParentFile().exists() && !tmpFile.getParentFile().mkdirs()) {
                    String errMsg = "??????[??="
                            + tmpFile + "]";
                    System.err.println(errMsg);
                    continue;
                }

                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tmpFile));
                try {
                    IOUtils.copy(zis, out);
                } catch (IOException zex) {
                    String errMsg = "ZIP??(copy)?????[??="
                            + tmpFile + "]";
                    System.err.println(errMsg);
                    continue;
                } finally {
                    Closer.ioClose(out);
                }
            }
            // 5.1.9.0 (2010/08/01) ?
            long lastTime = entry.getTime();
            if (lastTime >= 0 && !tmpFile.setLastModified(lastTime)) {
                String errMsg = "ZIP??????[??=" + tmpFile
                        + "]";
                System.err.println(errMsg);
            }
        }
    } catch (FileNotFoundException ex) {
        String errMsg = "????????[??=" + tmpFile + "]";
        throw new RuntimeException(errMsg, ex);
    } catch (IOException ex) {
        String errMsg = "ZIP???????[??=" + tmpFile + "]";
        throw new RuntimeException(errMsg, ex);
    } finally {
        Closer.ioClose(zis);
    }

    return list;
}

From source file:org.zuinnote.hadoop.office.format.common.writer.msexcel.internal.EncryptedZipEntrySource.java

public void setInputStream(InputStream is) throws IOException {
    this.tmpFile = TempFile.createTempFile("hadoopoffice-protected", ".zip");

    ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
    FileOutputStream fos = new FileOutputStream(tmpFile);
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);
    ZipArchiveEntry ze;
    while ((ze = (ZipArchiveEntry) zis.getNextEntry()) != null) {
        // rewrite zip entries to match the size of the encrypted data (with padding)
        ZipArchiveEntry zeNew = new ZipArchiveEntry(ze.getName());
        zeNew.setComment(ze.getComment());
        zeNew.setExtra(ze.getExtra());/*w  w  w. jav  a2  s.  c  o m*/
        zeNew.setTime(ze.getTime());
        zos.putArchiveEntry(zeNew);
        FilterOutputStream fos2 = new FilterOutputStream(zos) {
            // do not close underlyzing ZipOutputStream
            @Override
            public void close() {
            }
        };
        OutputStream nos;
        if (this.ciEncoder != null) { // encrypt if needed
            nos = new CipherOutputStream(fos2, this.ciEncoder);
        } else { // do not encrypt
            nos = fos2;
        }
        IOUtils.copy(zis, nos);
        nos.close();
        if (fos2 != null) {
            fos2.close();
        }
        zos.closeArchiveEntry();

    }
    zos.close();
    fos.close();
    zis.close();
    IOUtils.closeQuietly(is);
    this.zipFile = new ZipFile(this.tmpFile);

}