Example usage for java.util.zip ZipEntry getTime

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

Introduction

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

Prototype

public long getTime() 

Source Link

Document

Returns the last modification time of the entry.

Usage

From source file:org.jumpmind.util.AppUtils.java

public static void unzip(InputStream in, File toDir) {
    try {/*from   ww w. j a v a 2  s .c  o  m*/
        ZipInputStream is = new ZipInputStream(in);
        ZipEntry entry = null;
        do {
            entry = is.getNextEntry();
            if (entry != null) {
                if (entry.isDirectory()) {
                    File dir = new File(toDir, entry.getName());
                    dir.mkdirs();
                    dir.setLastModified(entry.getTime());
                } else {
                    File file = new File(toDir, entry.getName());
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                        file.getParentFile().setLastModified(entry.getTime());
                    }
                    FileOutputStream fos = new FileOutputStream(file);
                    try {
                        IOUtils.copy(is, fos);
                        file.setLastModified(entry.getTime());
                    } finally {
                        IOUtils.closeQuietly(fos);
                    }
                }
            }
        } while (entry != null);
    } catch (IOException e) {
        throw new IoException(e);
    }

}

From source file:com.opoopress.maven.plugins.plugin.zip.ZipUtils.java

public static void unzipFileToDirectory(File file, File destDir, boolean keepTimestamp) throws IOException {
    // create output directory if it doesn't exist
    if (!destDir.exists())
        destDir.mkdirs();/*ww  w .ja  v  a 2 s.  co  m*/

    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            new File(destDir, entry.getName()).mkdirs();
            continue;
        }

        InputStream inputStream = zipFile.getInputStream(entry);
        File destFile = new File(destDir, entry.getName());

        System.out.println("Unzipping to " + destFile.getAbsolutePath());
        FileUtils.copyInputStreamToFile(inputStream, destFile);
        IOUtils.closeQuietly(inputStream);

        if (keepTimestamp) {
            long time = entry.getTime();
            if (time > 0) {
                destFile.setLastModified(time);
            }
        }
    }

    zipFile.close();
}

From source file:org.apache.kylin.common.util.ZipFileUtils.java

public static void decompressZipfileToDirectory(String zipFileName, File outputFolder) throws IOException {
    ZipInputStream zipInputStream = null;
    try {//  ww  w  .j  a v a2s  .c om
        zipInputStream = new ZipInputStream(new FileInputStream(zipFileName));
        ZipEntry zipEntry = null;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            logger.info("decompressing " + zipEntry.getName() + " is directory:" + zipEntry.isDirectory()
                    + " available: " + zipInputStream.available());

            File temp = new File(outputFolder, zipEntry.getName());
            if (zipEntry.isDirectory()) {
                temp.mkdirs();
            } else {
                temp.getParentFile().mkdirs();
                temp.createNewFile();
                temp.setLastModified(zipEntry.getTime());
                FileOutputStream outputStream = new FileOutputStream(temp);
                try {
                    IOUtils.copy(zipInputStream, outputStream);
                } finally {
                    IOUtils.closeQuietly(outputStream);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }
}

From source file:org.eclipse.thym.core.internal.util.FileUtils.java

public static boolean isNewer(URL file, URL reference) {
    if (file == null || reference == null)
        throw new IllegalArgumentException("null file value");
    file = getFileURL(file);/* w  w w  .j  a va2s  . c o m*/
    reference = getFileURL(reference);
    if (!isFile(file) && !isFile(reference))
        throw new IllegalArgumentException("destination is not a file URL");

    long fileLastModified = 0;
    long referenceLastModified = 0;

    if ("file".equals(file.getProtocol())) {
        File srcFile = new File(file.getFile());
        fileLastModified = srcFile.lastModified();
    }
    if ("file".equals(reference.getProtocol())) {
        File referenceFile = new File(reference.getFile());
        referenceLastModified = referenceFile.lastModified();
    }

    if ("jar".equals(file.getProtocol())) {
        ZipFile zipFile = getZipFile(file);
        ZipEntry zipEntry = getZipEntry(file, zipFile);
        if (zipEntry == null) {
            throw new IllegalArgumentException(file + " can not be found on the zip file");
        }
        fileLastModified = zipEntry.getTime();
    }
    if ("jar".equals(reference.getProtocol())) {
        ZipFile zipFile = getZipFile(reference);
        ZipEntry zipEntry = getZipEntry(reference, zipFile);
        if (zipEntry == null) {
            throw new IllegalArgumentException(reference + " can not be found on the zip file");
        }
        referenceLastModified = zipEntry.getTime();
    }

    return fileLastModified >= referenceLastModified;
}

From source file:org.apache.batchee.cli.zip.Zips.java

public static void unzip(final File zipFile, final File destination) throws IOException {
    ZipInputStream in = null;/*from   w  ww .  j a  v a  2 s  . c o  m*/
    try {
        mkdir(destination);

        in = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile), FILE_BUFFER_SIZE));

        ZipEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            final String path = entry.getName();
            final File file = new File(destination, path);

            if (entry.isDirectory()) {
                continue;
            }

            mkdir(file.getParentFile());

            final OutputStream out = new BufferedOutputStream(new FileOutputStream(file), FILE_BUFFER_SIZE);
            try {
                copy(in, out);
            } finally {
                IOUtils.closeQuietly(out);
            }

            final long lastModified = entry.getTime();
            if (lastModified > 0) {
                file.setLastModified(lastModified);
            }

        }
    } catch (final IOException e) {
        throw new IOException("Unable to unzip " + zipFile.getAbsolutePath(), e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.ibm.jaggr.core.util.ZipUtil.java

/**
 * Extracts the directory entry to the location specified by {@code dir}
 *
 * @param entry/*from   ww w  .j  a va 2 s .  co  m*/
 *            the {@link ZipEntry} object for the directory being extracted
 * @param dir
 *            the {@link File} object for the target directory
 *
 * @throws IOException
 */
private static void extractDirectory(ZipEntry entry, File dir) throws IOException {
    final String sourceMethod = "extractFile"; //$NON-NLS-1$
    final boolean isTraceLogging = log.isLoggable(Level.FINER);
    if (isTraceLogging) {
        log.entering(sourceClass, sourceMethod, new Object[] { entry, dir });
    }

    dir.mkdir(); // May fail if the directory has already been created
    if (!dir.setLastModified(entry.getTime())) {
        throw new IOException("Failed to set last modified time for " + dir.getAbsolutePath()); //$NON-NLS-1$
    }

    if (isTraceLogging) {
        log.exiting(sourceClass, sourceMethod);
    }
}

From source file:it.mb.whatshare.Dialogs.java

private static String getBuildDate(final Activity activity) {
    String buildDate = "";
    ZipFile zf = null;/*  ww w.jav  a  2  s  .c  o  m*/
    try {
        ApplicationInfo ai = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), 0);
        zf = new ZipFile(ai.sourceDir);
        ZipEntry ze = zf.getEntry("classes.dex");
        long time = ze.getTime();
        buildDate = SimpleDateFormat.getInstance().format(new java.util.Date(time));

    } catch (Exception e) {
    } finally {
        if (zf != null) {
            try {
                zf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return buildDate;
}

From source file:com.mgmtp.perfload.perfalyzer.util.IoUtilities.java

private static void extractEntry(final ZipFile zf, final ZipEntry entry, final File destDir)
        throws IOException {
    File file = new File(destDir, entry.getName());

    if (entry.isDirectory()) {
        file.mkdirs();/*from  w ww .j  av a2 s .co m*/
    } else {
        new File(file.getParent()).mkdirs();

        try (InputStream is = zf.getInputStream(entry); FileOutputStream os = new FileOutputStream(file)) {
            copy(Channels.newChannel(is), os.getChannel());
        }
        // preserve modification time; must be set after the stream is closed
        file.setLastModified(entry.getTime());
    }
}

From source file:cat.ereza.customactivityoncrash.CustomActivityOnCrash.java

/**
 * INTERNAL method that returns the build date of the current APK as a string, or null if unable to determine it.
 *
 * @param context    A valid context. Must not be null.
 * @param dateFormat DateFormat to use to convert from Date to String
 * @return The formatted date, or "Unknown" if unable to determine it.
 *//*from   w w w. j a  va  2  s.  co  m*/
private static String getBuildDateAsString(Context context, DateFormat dateFormat) {
    String buildDate;
    try {
        ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
        ZipFile zf = new ZipFile(ai.sourceDir);
        ZipEntry ze = zf.getEntry("classes.dex");
        long time = ze.getTime();
        buildDate = dateFormat.format(new Date(time));
        zf.close();
    } catch (Exception e) {
        buildDate = "Unknown";
    }
    return buildDate;
}

From source file:com.ibm.jaggr.core.util.ZipUtil.java

/**
 * Extracts the file entry to the location specified by {@code file}
 *
 * @param entry/*from   ww w .  j a v  a 2s  .  c  o m*/
 *            the {@link ZipEntry} object for the directory being extracted
 * @param zipIn
 *            the zip input stream to read from
 * @param file
 *            the {@link File} object for the target file
 * @throws IOException
 */
private static void extractFile(ZipEntry entry, ZipInputStream zipIn, File file) throws IOException {
    final String sourceMethod = "extractFile"; //$NON-NLS-1$
    final boolean isTraceLogging = log.isLoggable(Level.FINER);
    if (isTraceLogging) {
        log.entering(sourceClass, sourceMethod, new Object[] { entry, zipIn, file });
    }

    Files.createParentDirs(file);
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
    try {
        IOUtils.copy(zipIn, bos);
    } finally {
        bos.close();
    }
    if (!file.setLastModified(entry.getTime())) {
        throw new IOException("Failed to set last modified time for " + file.getAbsolutePath()); //$NON-NLS-1$
    }

    if (isTraceLogging) {
        log.exiting(sourceClass, sourceMethod);
    }
}