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.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.  j a  v a 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:uk.ac.ebi.metabolights.controller.FileDispatcherController.java

private File createZipFile(File[] files, String studyId) throws IOException {

    String zipFile = "";

    // If there is only one file, and it matches the studyId...
    if (files.length == 1 && files[0].getName().equals(studyId)) {
        // User wanrts all the data...keep it in the zipondemand location for future requests
        zipFile = zipOnDemandLocation + files[0].getName() + ".zip";

        // Change File[] with the list of files.
        files = files[0].listFiles();/*from www.j  a v a2 s.c o m*/

    } else {

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());

        zipFile = studyId + "_" + timeStamp;

        zipFile = zipOnDemandLocation + zipFile + ".zip";

    }

    if (!FileUtil.fileExists(zipFile)) // Just to be sure that the file *doesn't already* exist
        Zipper.zip(files, zipFile);

    File file = new File(zipFile);

    //Set the last access timestamp, this will stop the cron job removing the file as being old
    Date date = new Date();
    file.setLastModified(date.getTime());

    return file;

}

From source file:de.uzk.hki.da.pkg.TarGZArchiveBuilder.java

public void unarchiveFolder(File srcTar, File destFolder) throws Exception {

    FileInputStream fin = new FileInputStream(srcTar);
    BufferedInputStream in = new BufferedInputStream(fin);

    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
    TarArchiveInputStream tIn = new TarArchiveInputStream(gzIn);

    HashMap<String, Long> modDateMap = new HashMap<String, Long>();
    TarArchiveEntry entry;/*from  w  w  w  .  j  a  v  a 2  s . c  om*/
    do {
        entry = tIn.getNextTarEntry();
        if (entry == null)
            break;
        logger.debug(entry.getName());

        String dstName = destFolder.getAbsolutePath() + "/" + entry.getName();
        File entryFile = new File(dstName);
        if (entry.isDirectory()) {
            entryFile.mkdirs();
            modDateMap.put(dstName, new Long(entry.getModTime().getTime()));
        } else {
            new File(entryFile.getAbsolutePath().substring(0, entryFile.getAbsolutePath().lastIndexOf('/')))
                    .mkdirs();

            FileOutputStream out = new FileOutputStream(entryFile);
            IOUtils.copy(tIn, out);
            out.close();
            entryFile.setLastModified(entry.getModTime().getTime());
        }
    } while (true);

    tIn.close();
    gzIn.close();
    in.close();
    fin.close();

    for (Map.Entry<String, Long> moddi : modDateMap.entrySet()) {
        String key = moddi.getKey();
        Long value = moddi.getValue();
        (new File(key)).setLastModified(value);
    }
}

From source file:net.sourceforge.vulcan.web.ProjectFileServletTest.java

private void check304(final Date modifiedSince) throws ServletException, IOException {
    final File testFile = new File(TEST_DIR, "file.txt");
    assertTrue("Cannot set last modified date for unit test", testFile.setLastModified(modDate.getTime()));

    request.setHeader("If-Modified-Since", ProjectFileServlet.HTTP_DATE_FORMAT.format(modifiedSince));

    servlet.init(servletConfig);//from w ww. j  ava  2s. co m

    request.setPathInfo("/myProject/38/file.txt");

    expect(mgr.getProjectConfig("myProject")).andReturn(projectConfig);
    expect(buildManager.getStatusByBuildNumber("myProject", 38)).andReturn(latestStatus);

    replay();

    servlet.doGet(request, response);

    verify();

    assertEquals(304, response.getStatusCode());

    assertEquals("", os.toString().trim());
    assertEquals("text/plain", response.getContentType());
    assertEquals(0, response.getContentLength());
}

From source file:net.sourceforge.vulcan.web.ProjectFileServletTest.java

public void testSendsFile() throws Exception {
    final File testFile = new File(TEST_DIR, "file.txt");
    assertTrue("Cannot set last modified date for unit test", testFile.setLastModified(modDate.getTime()));

    servlet.init(servletConfig);//w  w  w . ja  va  2s.  co m

    request.setPathInfo("/myProject/42/file.txt");

    expect(mgr.getProjectConfig("myProject")).andReturn(projectConfig);
    expect(buildManager.getStatusByBuildNumber("myProject", 42)).andReturn(latestStatus);

    replay();

    servlet.doGet(request, response);

    verify();

    assertEquals(200, response.getStatusCode());

    assertEquals("sometext", os.toString().trim());
    assertEquals("text/plain", response.getContentType());

    final String lastModifiedHeader = response.getHeader("Last-Modified");

    assertNotNull(lastModifiedHeader);
    assertEquals(modDate, ProjectFileServlet.HTTP_DATE_FORMAT.parse(lastModifiedHeader));

    assertEquals(9, response.getContentLength());
    assertTrue(closeCalled);
}

From source file:nl.edia.sakai.tool.skinmanager.impl.SkinFileSystemServiceImpl.java

@Override
public void updateSkin(String name, Date date, InputStream data) throws SkinException, IOException {
    File file = createTempZip(data);
    try {//from   w  w  w  .ja v a 2s. c  o m
        validateSkinZip(file);
        File mySkinDir = prepareSkinDir(name, date, true);
        installSkin(mySkinDir, file);
        mySkinDir.setLastModified(date.getTime());
    } finally {
        if (!file.delete()) {
            LOG.warn("Unable to delete tmp file: " + file);
        }
    }
}

From source file:org.betaconceptframework.astroboa.test.engine.service.RepositoryServiceTest.java

@Test
public void testCreateNewRepository() throws NamingException, Exception {

    final String repositoryId = "newRepositoryAddedAtRuntime";
    File newRepositoryConfigurationFile = new ClassPathResource("new-repositories-conf.xml").getFile();

    File configuration = retrieveConfigurationFile();

    //Copy File/* ww w .jav  a  2s.c  o m*/
    FileUtils.copyFile(newRepositoryConfigurationFile, configuration, false);

    //Sleep some seconds to be able to get a new date 
    //instance
    Thread.sleep(7000);

    //update last modified date to force registry to reload configuration
    Calendar lastModified = Calendar.getInstance();
    configuration.setLastModified(lastModified.getTimeInMillis());

    //Clear files in test context
    AstroboaTestContext.INSTANCE.removeRepositoryResources(repositoryId);

    //Login to the repository. We expect to load the new repository at runtime
    loginToRepository(repositoryId, "SYSTEM", "betaconcept", false);

    CmsRepository newRepository = repositoryService.getCmsRepository(repositoryId);

    Assert.assertNotNull(newRepository);

    Assert.assertEquals(newRepository.getId(), repositoryId);

    loginToTestRepositoryAsSystem();

    checkExpression("objectType=\"" + TEST_CONTENT_TYPE + "Type\"", CriterionFactory.equals(
            CmsBuiltInItem.ContentObjectTypeName.getJcrName(), Condition.OR,
            Arrays.asList(EXTENDED_TEST_CONTENT_TYPE, DIRECT_EXTENDED_TEST_CONTENT_TYPE, TEST_CONTENT_TYPE)));

    checkExpression("objectType=\"" + EXTENDED_TEST_CONTENT_TYPE + "Type\"",
            CriterionFactory.equals(CmsBuiltInItem.ContentObjectTypeName.getJcrName(), Condition.OR,
                    Arrays.asList(EXTENDED_TEST_CONTENT_TYPE, DIRECT_EXTENDED_TEST_CONTENT_TYPE)));

}

From source file:org.apache.maven.plugin.dependency.fromConfiguration.TestUnpackMojo.java

public void assertUnpacked(ArtifactItem item, boolean overWrite) throws Exception {

    File unpackedFile = getUnpackedFile(item);

    Thread.sleep(100);//from w  ww .  j  a  v  a2  s.com
    // round down to the last second
    long time = System.currentTimeMillis();
    time = time - (time % 1000);
    unpackedFile.setLastModified(time);

    assertEquals(time, unpackedFile.lastModified());
    mojo.execute();

    if (overWrite) {
        assertTrue(time != unpackedFile.lastModified());
    } else {
        assertEquals(time, unpackedFile.lastModified());
    }
}

From source file:com.frostwire.ImageCache.java

private void saveToCache(URL url, BufferedImage image, long date) {
    try {/*from w  w  w .  j  a v  a2  s .c o  m*/
        File file = getCacheFile(url);

        if (file.exists()) {
            file.delete();
        }

        String filename = file.getName();
        int dotIndex = filename.lastIndexOf('.');
        String ext = filename.substring(dotIndex + 1);

        String formatName = ImageIO.getImageReadersBySuffix(ext).next().getFormatName();

        if (!file.getParentFile().exists()) {
            file.mkdirs();
        }
        ImageIO.write(image, formatName, file);
        file.setLastModified(date);
    } catch (Throwable e) {
        LOG.error("Failed to save image to cache: " + url, e);
    }
}

From source file:com.alu.e3.logger.LogCollector.java

/**
 * Copy the modification time from one local file to another.
 * //from   w  w  w . ja  va2s  .com
 * @param sourceFile   The file to copy the mod time from
 * @param destFile   The file to copy the mod time to
 * @throws IOException
 */
public static void copyModificationTime(File sourceFile, File destFile) throws IOException {
    destFile.setLastModified(sourceFile.lastModified());
}