Example usage for java.io File setLastModified

List of usage examples for java.io File setLastModified

Introduction

In this page you can find the example usage for java.io File setLastModified.

Prototype

public boolean setLastModified(long time) 

Source Link

Document

Sets the last-modified time of the file or directory named by this abstract pathname.

Usage

From source file:org.robovm.maven.resolver.Archiver.java

public static void unarchive(Logger logger, File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;/*from  w  w w  .  java 2  s .c  o m*/
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                logger.debug(f.getAbsolutePath());
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            f.setLastModified(entry.getLastModifiedDate().getTime());
            if (entry instanceof TarArchiveEntry) {
                int mode = ((TarArchiveEntry) entry).getMode();
                if ((mode & 00100) > 0) {
                    // Preserve execute permissions
                    f.setExecutable(true, (mode & 00001) == 0);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.parse.ParseKeyValueCache.java

static String loadFromKeyValueCache(final String key, final long maxAgeMilliseconds) {
    synchronized (MUTEX_IO) {
        File file = getKeyValueCacheFile(key);
        if (file == null) {
            return null;
        }/*w w w. jav a  2 s  .c  om*/

        Date now = new Date();
        long oldestAcceptableAge = Math.max(0, now.getTime() - maxAgeMilliseconds);
        if (getKeyValueCacheAge(file) < oldestAcceptableAge) {
            return null;
        }

        // Update mtime to make the LRU work
        file.setLastModified(now.getTime());

        try {
            RandomAccessFile f = new RandomAccessFile(file, "r");
            byte[] bytes = new byte[(int) f.length()];
            f.readFully(bytes);
            f.close();
            return new String(bytes, "UTF-8");
        } catch (IOException e) {
            PLog.e(TAG, "error reading from cache", e);
            return null;
        }
    }
}

From source file:com.twinsoft.convertigo.engine.util.ZipUtils.java

public static void expandZip(String zipFileName, String rootDir, String prefixDir)
        throws FileNotFoundException, IOException {
    Engine.logEngine.debug("Expanding the zip file " + zipFileName);

    // Creating the root directory
    File ftmp = new File(rootDir);
    if (!ftmp.exists()) {
        ftmp.mkdirs();/*  ww w. j av a  2s  . c om*/
        Engine.logEngine.debug("Root directory created");
    }

    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));

    try {
        ZipEntry entry;
        int prefixSize = prefixDir != null ? prefixDir.length() : 0;

        while ((entry = zis.getNextEntry()) != null) {
            // Ignoring directories
            if (entry.isDirectory()) {

            } else {

                String entryName = entry.getName();
                Engine.logEngine.debug("+ Analyzing the entry: " + entryName);

                try {
                    // Ignore entry if does not belong to the project directory
                    if ((prefixDir == null) || entryName.startsWith(prefixDir)) {

                        // Ignore entry from _data or _private directory
                        if ((entryName.indexOf("/_data/") != prefixSize)
                                && (entryName.indexOf("/_private/") != prefixSize)) {
                            Engine.logEngine.debug("  The entry is accepted");
                            String s1 = rootDir + "/" + entryName;
                            String dir = s1.substring(0, s1.lastIndexOf('/'));

                            // Creating the directory if needed
                            ftmp = new File(dir);
                            if (!ftmp.exists()) {
                                ftmp.mkdirs();
                                Engine.logEngine.debug("  Directory created");
                            }

                            // Writing the files to the disk
                            File file = new File(rootDir + "/" + entryName);
                            FileOutputStream fos = new FileOutputStream(file);
                            try {
                                IOUtils.copy(zis, fos);
                            } finally {
                                fos.close();
                            }
                            file.setLastModified(entry.getTime());
                            Engine.logEngine.debug("  File written to: " + rootDir + "/" + entryName);
                        }
                    }
                } catch (IOException e) {
                    Engine.logEngine.error(
                            "Unable to expand the ZIP entry \"" + entryName + "\": " + e.getMessage(), e);
                }
            }
        }
    } finally {
        zis.close();
    }
}

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  w w .j av a2s .  co 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:au.org.ala.biocache.util.ParamsCache.java

/**
 * load disk stored ParamsCacheObject//  www.j av a  2 s.c o  m
 *
 * @param key
 * @return
 * @throws ParamsCacheMissingException
 */
static ParamsCacheObject load(long key) throws ParamsCacheMissingException {
    ParamsCacheObject value = null;
    File file = getFile(key);
    if (file.exists()) {
        try {
            value = jsonMapper.readValue(file, ParamsCacheObject.class);

            if (value != null && value.getSize() < LARGEST_CACHEABLE_SIZE) {
                file.setLastModified(System.currentTimeMillis());

                while (!put(key, value)) {
                    //cache cleaner has been run, safe to try again
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        throw new ParamsCacheMissingException(key);
    }

    return value;
}

From source file:org.sakaiproject.kernel.util.FileUtil.java

public static void unpack(InputStream source, File destination) throws IOException {
    ZipInputStream zin = new ZipInputStream(source);
    ZipEntry zipEntry = null;//from  w  ww. jav  a 2  s .  c o  m
    FileOutputStream fout = null;
    try {
        byte[] buffer = new byte[4096];
        while ((zipEntry = zin.getNextEntry()) != null) {

            long ts = zipEntry.getTime();
            // the zip entry needs to be a full path from the
            // searchIndexDirectory... hence this is correct

            File f = new File(destination, zipEntry.getName());
            LOG.info("         Unpack " + f.getAbsolutePath());
            f.getParentFile().mkdirs();

            if (!zipEntry.isDirectory()) {
                fout = new FileOutputStream(f);
                int len;
                while ((len = zin.read(buffer)) > 0) {
                    fout.write(buffer, 0, len);
                }
                zin.closeEntry();
                fout.close();
            }
            f.setLastModified(ts);
        }
    } finally {
        try {
            fout.close();
        } catch (Exception ex) {
            LOG.warn("Exception closing file output stream", ex);
        }
        try {
            zin.close();
        } catch (Exception ex) {
            LOG.warn("Exception closing file input stream", ex);
        }
    }
}

From source file:org.rapidcontext.util.FileUtil.java

/**
 * Copies a file or a directory. Directories are copied
 * recursively and file modification dates are kept. If the
 * executable bit is set on the source file, that bit will also
 * be set on the destination file.// w  ww  . ja v a2s.c o m
 *
 * @param src            the source file or directory
 * @param dst            the destination file or directory
 *
 * @throws IOException if some file couldn't be copied
 */
@SuppressWarnings("resource")
public static void copy(File src, File dst) throws IOException {
    if (src.isDirectory()) {
        dst.mkdirs();
        File[] files = src.listFiles();
        for (int i = 0; i < files.length; i++) {
            copy(files[i], new File(dst, files[i].getName()));
        }
    } else {
        copy(new FileInputStream(src), dst);
        dst.setExecutable(src.canExecute());
    }
    dst.setLastModified(src.lastModified());
}

From source file:org.eclipse.smila.utils.compression.CompressionHelper.java

/**
 * /** Unzip./*from w w  w  . jav  a 2  s  . com*/
 * 
 * @param dest
 *          the dest
 * @param inputStream
 *          the input stream
 * 
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
public static void unzip(final File dest, final InputStream inputStream) throws IOException {
    ZipInputStream zis = null;
    FileOutputStream fileOutputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    try {
        zis = new ZipInputStream(inputStream);
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                new File(dest, entry.getName()).mkdir();
                continue;
            }
            final File destFile = new File(dest, entry.getName());
            if (!destFile.getParentFile().exists()) {
                destFile.getParentFile().mkdirs();
            }
            try {
                fileOutputStream = new FileOutputStream(destFile);
                bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                IOUtils.copy(zis, bufferedOutputStream);
            } finally {
                IOUtils.closeQuietly(fileOutputStream);
                IOUtils.closeQuietly(bufferedOutputStream);
            }
            destFile.setLastModified(entry.getTime());
        }
    } finally {
        IOUtils.closeQuietly(zis);
    }
}

From source file:nl.strohalm.cyclos.utils.WebImageHelper.java

/**
 * Update an image or thumbnail//from  w  w  w  . j  ava2 s . c om
 * @param overrite
 */
private static void updateImage(final ServletContext context, final boolean isThumbnail, final Image image,
        final File dir, final boolean overrite) {
    // Update nothing if no path is given
    if (dir == null) {
        return;
    }
    InputStream in = null;
    OutputStream out = null;
    final File file = new File(dir, image.getName());
    try {
        dir.mkdirs();
        final long lastModified = image.getLastModified() == null ? System.currentTimeMillis()
                : image.getLastModified().getTimeInMillis();
        if (overrite || file.lastModified() != lastModified) {
            out = new FileOutputStream(file);
            in = (isThumbnail ? image.getThumbnail() : image.getImage()).getBinaryStream();
            IOUtils.copy(in, out);
            file.setLastModified(lastModified);
        }
    } catch (final IOException e) {
        context.log("Error writing image file " + file, e);
    } catch (final SQLException e) {
        context.log("Error writing image file " + file + " from db", e);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:gr.abiss.calipso.util.AttachmentUtils.java

private static void writeToFile(Attachment attachment, FileUpload fileUpload, File file, String home,
        boolean makeThumbs) {
    if (fileUpload == null) {
        return;// w w w  .  j  a va 2 s . co  m
    }
    try {
        // create directory hierarchy if non-existent
        ensureFileExists(file);
        fileUpload.writeTo(file);
        file.setLastModified(new Date().getTime());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}