List of usage examples for java.nio.file.attribute FileTime fromMillis
public static FileTime fromMillis(long value)
From source file:org.duracloud.retrieval.mgmt.RetrievalWorker.java
private FileTime convertDateToFileTime(String strDate) { FileTime time = null;/*from ww w . jav a2 s .co m*/ if (null != strDate) { Date date = null; try { date = DateUtil.convertToDate(strDate, DateUtil.DateFormat.LONG_FORMAT); } catch (ParseException e) { date = null; } if (null != date) { time = FileTime.fromMillis(date.getTime()); } } return time; }
From source file:org.dataconservancy.packaging.tool.impl.AnnotationDrivenPackageStateSerializer.java
/** * Serializes the identified stream from the package state to the supplied archive output stream. * * @param state the package state object containing the identified stream * @param streamId the stream identifier for the content being serialized * @param aos the archive output stream to serialize the stream to *///from www .j av a 2 s.c o m void serializeToArchive(PackageState state, StreamId streamId, ArchiveOutputStream aos) { // when writing to an archive file: // 1. read the stream to be serialized, to get its properties // 2. create the archive entry using the properties and write it to the archive stream // 3. write the stream to be serialized to the archive stream // 4. close the entry final FileTime now = FileTime.fromMillis(Calendar.getInstance().getTimeInMillis()); ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 4); CRC32CalculatingOutputStream crc = new CRC32CalculatingOutputStream(baos); StreamResult result = new StreamResult(crc); serializeToResult(state, streamId, result); ArchiveEntry arxEntry = arxStreamFactory.newArchiveEntry(streamId.name(), baos.size(), now, now, 0644, crc.resetCrc()); try { aos.putArchiveEntry(arxEntry); baos.writeTo(aos); aos.closeArchiveEntry(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:com.facebook.buck.io.filesystem.impl.DefaultProjectFilesystemTest.java
@Test public void testSortedDirectoryContents() throws IOException { tmp.newFolder("foo"); Path a = tmp.newFile("foo/a.txt"); Files.setLastModifiedTime(a, FileTime.fromMillis(1000)); Path b = tmp.newFile("foo/b.txt"); Files.setLastModifiedTime(b, FileTime.fromMillis(2000)); Path c = tmp.newFile("foo/c.txt"); Files.setLastModifiedTime(c, FileTime.fromMillis(3000)); tmp.newFile("foo/non-matching"); assertEquals(ImmutableSet.of(c, b, a), filesystem.getMtimeSortedMatchingDirectoryContents(Paths.get("foo"), "*.txt")); }
From source file:org.mycore.common.MCRUtils.java
/** * Extracts files in a tar archive. Currently works only on uncompressed tar files. * /*from w w w.j a v a2 s . c om*/ * @param source * the uncompressed tar to extract * @param expandToDirectory * the directory to extract the tar file to * @throws IOException * if the source file does not exists */ public static void untar(Path source, Path expandToDirectory) throws IOException { try (TarArchiveInputStream tain = new TarArchiveInputStream(Files.newInputStream(source))) { TarArchiveEntry tarEntry; FileSystem targetFS = expandToDirectory.getFileSystem(); HashMap<Path, FileTime> directoryTimes = new HashMap<>(); while ((tarEntry = tain.getNextTarEntry()) != null) { Path target = MCRPathUtils.getPath(targetFS, tarEntry.getName()); Path absoluteTarget = expandToDirectory.resolve(target).normalize().toAbsolutePath(); if (tarEntry.isDirectory()) { Files.createDirectories(expandToDirectory.resolve(absoluteTarget)); directoryTimes.put(absoluteTarget, FileTime.fromMillis(tarEntry.getLastModifiedDate().getTime())); } else { if (Files.notExists(absoluteTarget.getParent())) { Files.createDirectories(absoluteTarget.getParent()); } Files.copy(tain, absoluteTarget, StandardCopyOption.REPLACE_EXISTING); Files.setLastModifiedTime(absoluteTarget, FileTime.fromMillis(tarEntry.getLastModifiedDate().getTime())); } } //restore directory dates Files.walkFileTree(expandToDirectory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Path absolutePath = dir.normalize().toAbsolutePath(); Files.setLastModifiedTime(absolutePath, directoryTimes.get(absolutePath)); return super.postVisitDirectory(dir, exc); } }); } }
From source file:net.mozq.picto.core.ProcessCore.java
private static ProcessDataStatus process(ProcessCondition processCondition, ProcessData processData, Function<ProcessData, ProcessDataStatus> overwriteConfirm) throws IOException { ProcessDataStatus status;//from w ww . j ava 2 s . com Path destParentPath = processData.getDestPath().getParent(); if (destParentPath != null) { Files.createDirectories(destParentPath); } if (processCondition.isCheckDigest() || (processCondition.isChangeExifDate() && processData.getBaseDate() != null) || processCondition.isRemveExifTagsGps() || processCondition.isRemveExifTagsAll()) { Path destTempPath = null; try { destTempPath = Files.createTempFile(processData.getDestPath().getParent(), processData.getDestPath().getFileName().toString(), null); if (processCondition.isCheckDigest()) { String algorithm = FILE_DIGEST_ALGORITHM; MessageDigest srcMD = newMessageDigest(algorithm); try (InputStream is = new DigestInputStream( new BufferedInputStream(Files.newInputStream(processData.getSrcPath())), srcMD)) { Files.copy(is, destTempPath, OPTIONS_COPY_REPLACE); } byte[] srcDigest = srcMD.digest(); MessageDigest destMD = newMessageDigest(algorithm); try (InputStream is = new DigestInputStream( new BufferedInputStream(Files.newInputStream(destTempPath)), destMD)) { byte[] b = new byte[1024]; while (is.read(b) != -1) { } } byte[] destDigest = destMD.digest(); if (!isSame(srcDigest, destDigest)) { throw new PictoFileDigestMismatchException( Messages.getString("message.error.digest.mismatch")); } } else if (processCondition.isRemveExifTagsAll()) { ExifRewriter exifRewriter = new ExifRewriter(); try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(destTempPath))) { exifRewriter.removeExifMetadata(processData.getSrcPath().toFile(), os); } catch (ImageReadException | ImageWriteException e) { throw new PictoFileChangeException(Messages.getString("message.error.edit.file"), e); } } else if (processCondition.isChangeExifDate() || processCondition.isRemveExifTagsGps()) { ImageMetadata imageMetadata = getImageMetadata(processData.getSrcPath()); TiffOutputSet outputSet = getOutputSet(imageMetadata); if (outputSet == null) { Files.copy(processData.getSrcPath(), destTempPath, OPTIONS_COPY_REPLACE); } else { if (processCondition.isChangeExifDate()) { SimpleDateFormat exifDateFormat = new SimpleDateFormat(EXIF_DATE_PATTERN); exifDateFormat.setTimeZone(processCondition.getTimeZone()); String exifBaseDate = exifDateFormat.format(processData.getBaseDate()); DecimalFormat exifSubsecFormat = new DecimalFormat(EXIF_SUBSEC_PATTERN); String exifBaseSubsec = exifSubsecFormat .format((int) (processData.getBaseDate().getTime() / 10) % 100); try { TiffOutputDirectory rootDirectory = outputSet.getRootDirectory(); TiffOutputDirectory exifDirectory = outputSet.getExifDirectory(); if (rootDirectory != null) { rootDirectory.removeField(TiffTagConstants.TIFF_TAG_DATE_TIME); rootDirectory.add(TiffTagConstants.TIFF_TAG_DATE_TIME, exifBaseDate); } if (exifDirectory != null) { exifDirectory.removeField(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME); exifDirectory.add(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME, exifBaseSubsec); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL); exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, exifBaseDate); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME_ORIGINAL); exifDirectory.add(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME_ORIGINAL, exifBaseSubsec); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED); exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED, exifBaseDate); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME_DIGITIZED); exifDirectory.add(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME_DIGITIZED, exifBaseSubsec); } } catch (ImageWriteException e) { throw new PictoFileChangeException(Messages.getString("message.error.edit.file"), e); } } if (processCondition.isRemveExifTagsGps()) { outputSet.removeField(ExifTagConstants.EXIF_TAG_GPSINFO); } ExifRewriter exifRewriter = new ExifRewriter(); try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(destTempPath))) { exifRewriter.updateExifMetadataLossless(processData.getSrcPath().toFile(), os, outputSet); } catch (ImageReadException | ImageWriteException e) { throw new PictoFileChangeException(Messages.getString("message.error.edit.file"), e); } } } Path destPath; if (processCondition.getOperationType() == OperationType.Overwrite) { destPath = processData.getSrcPath(); } else { destPath = processData.getDestPath(); } try { Files.move(destTempPath, destPath, OPTIONS_MOVE); if (processCondition.getOperationType() == OperationType.Move) { Files.deleteIfExists(processData.getSrcPath()); } status = ProcessDataStatus.Success; } catch (FileAlreadyExistsException e) { status = confirmOverwrite(processCondition, processData, overwriteConfirm); if (status == ProcessDataStatus.Processing) { // Overwrite Files.move(destTempPath, destPath, OPTIONS_MOVE_REPLACE); if (processCondition.getOperationType() == OperationType.Move) { Files.deleteIfExists(processData.getSrcPath()); } status = ProcessDataStatus.Success; } } } finally { if (destTempPath != null) { Files.deleteIfExists(destTempPath); } } } else { switch (processCondition.getOperationType()) { case Copy: try { Files.copy(processData.getSrcPath(), processData.getDestPath(), OPTIONS_COPY); status = ProcessDataStatus.Success; } catch (FileAlreadyExistsException e) { status = confirmOverwrite(processCondition, processData, overwriteConfirm); if (status == ProcessDataStatus.Processing) { Files.copy(processData.getSrcPath(), processData.getDestPath(), OPTIONS_COPY_REPLACE); status = ProcessDataStatus.Success; } } break; case Move: try { Files.move(processData.getSrcPath(), processData.getDestPath(), OPTIONS_MOVE); status = ProcessDataStatus.Success; } catch (FileAlreadyExistsException e) { status = confirmOverwrite(processCondition, processData, overwriteConfirm); if (status == ProcessDataStatus.Processing) { Files.move(processData.getSrcPath(), processData.getDestPath(), OPTIONS_MOVE_REPLACE); status = ProcessDataStatus.Success; } } break; case Overwrite: // NOP status = ProcessDataStatus.Success; break; default: throw new IllegalStateException(processCondition.getOperationType().toString()); } } if (status == ProcessDataStatus.Success) { FileTime creationFileTime = processData.getSrcFileAttributes().creationTime(); FileTime modifiedFileTime = processData.getSrcFileAttributes().lastModifiedTime(); FileTime accessFileTime = processData.getSrcFileAttributes().lastAccessTime(); if (processCondition.isChangeFileCreationDate() || processCondition.isChangeFileModifiedDate() || processCondition.isChangeFileAccessDate()) { if (processData.getBaseDate() != null) { FileTime baseFileTime = FileTime.fromMillis(processData.getBaseDate().getTime()); if (processCondition.isChangeFileCreationDate()) { creationFileTime = baseFileTime; } if (processCondition.isChangeFileModifiedDate()) { modifiedFileTime = baseFileTime; } if (processCondition.isChangeFileAccessDate()) { accessFileTime = baseFileTime; } } } BasicFileAttributeView attributeView = Files.getFileAttributeView(processData.getDestPath(), BasicFileAttributeView.class); attributeView.setTimes(modifiedFileTime, accessFileTime, creationFileTime); } return status; }
From source file:de.blizzy.backup.restore.RestoreDialog.java
private void restoreEntry(Entry entry, File parentFolder, String outputFolder, int backupId, IProgressMonitor monitor, Shell shell) throws IOException, InterruptedException { if (monitor.isCanceled()) { throw new InterruptedException(); }// w w w .j a v a2 s . co m boolean isFolder = entry.type == EntryType.FOLDER; if (entry.type == EntryType.FAILED_FILE) { if (alwaysRestoreFromOlderBackups == null) { alwaysRestoreFromOlderBackups = Boolean.valueOf(promptRestoreFromOlderBackups(shell)); } if (alwaysRestoreFromOlderBackups.booleanValue()) { entry = findInOlderBackups(entry); } } if ((entry != null) && (entry.type != EntryType.FAILED_FILE)) { Path outputPath; if (entry.type == EntryType.FOLDER) { File newFolder = new File(parentFolder, escapeFileName(entry.name)); FileUtils.forceMkdir(newFolder); Cursor<Record> cursor = getEntriesCursor(backupId, entry.id, null, -1); try { for (Entry e : getEntries(cursor, false)) { restoreEntry(e, newFolder, outputFolder, backupId, monitor, shell); } } finally { database.closeQuietly(cursor); } outputPath = newFolder.toPath(); } else { File inputFile = Utils.toBackupFile(entry.backupPath, outputFolder); File outputFile = new File(parentFolder, escapeFileName(entry.name)); outputPath = outputFile.toPath(); InputStream in = null; try { InputStream fileIn = new BufferedInputStream(new FileInputStream(inputFile)); InputStream interceptIn = fileIn; for (IStorageInterceptor interceptor : storageInterceptors) { interceptIn = interceptor.interceptInputStream(interceptIn, entry.length); } InputStream compressIn = entry.compression.getInputStream(interceptIn); in = compressIn; Files.copy(in, outputPath); } finally { IOUtils.closeQuietly(in); } } FileAttributes fileAttributes = FileAttributes.get(outputPath); if (entry.hidden) { fileAttributes.setHidden(entry.hidden); } FileTime createTime = (entry.creationTime != null) ? FileTime.fromMillis(entry.creationTime.getTime()) : null; FileTime modTime = (entry.modificationTime != null) ? FileTime.fromMillis(entry.modificationTime.getTime()) : null; fileAttributes.setTimes(createTime, modTime); } if (!isFolder) { monitor.worked(1); } }